Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ for specific instructions.
`loki_tag` is now `logs_instance_tag`, and `backend: loki` is now
`backend: logs_instance`. (@rfratto)

- [DEPRECATION] The `prometheus` key at the root of the config file has been
deprecated in favor of `metrics`. Flag names starting with `prometheus.` have
also been deprecated in favor of the same flags with the `metrics.` prefix.
(@rfratto)

# v0.18.2 (2021-08-12)

- [BUGFIX] Honor the prefix and remove prefix from consul list results (@mattdurham)
Expand Down
4 changes: 2 additions & 2 deletions cmd/agent/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewEntrypoint(logger *util.Logger, cfg *config.Config, reloader Reloader) (

ep.srv = server.New(prometheus.DefaultRegisterer, logger)

ep.promMetrics, err = metrics.New(prometheus.DefaultRegisterer, cfg.Prometheus, logger)
ep.promMetrics, err = metrics.New(prometheus.DefaultRegisterer, cfg.Metrics, logger)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func (ep *Entrypoint) ApplyConfig(cfg config.Config) error {
}

// Go through each component and update it.
if err := ep.promMetrics.ApplyConfig(cfg.Prometheus); err != nil {
if err := ep.promMetrics.ApplyConfig(cfg.Metrics); err != nil {
level.Error(ep.log).Log("msg", "failed to update prometheus", "err", err)
failed = true
}
Expand Down
45 changes: 45 additions & 0 deletions docs/upgrade-guide/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,51 @@ rules:
verbs: [get, list, watch]
```

### Metrics: Deprecation of "prometheus" in config. (Deprecation)

The term `prometheus` in the config has been deprecated of favor of `metrics`. This
change is to make it clearer when referring to Prometheus or another
Prometheus-like database, and configuration of Grafana Agent to send metrics to
one of those systems.

Old configs will continue to work until it is fully deprecated. To migrate your
config, change the `prometheus` key to `metrics`.

Example old config:

```yaml
prometheus:
configs:
- name: default
host_filter: false
scrape_configs:
- job_name: local_scrape
static_configs:
- targets: ['127.0.0.1:12345']
labels:
cluster: 'localhost'
remote_write:
- url: http://localhost:9009/api/prom/push
```

Example new config:

```yaml
metrics:
configs:
- name: default
host_filter: false
scrape_configs:
- job_name: local_scrape
static_configs:
- targets: ['127.0.0.1:12345']
labels:
cluster: 'localhost'
remote_write:
- url: http://localhost:9009/api/prom/push
```


### Logs: Deprecation of "loki" in config. (Deprecation)

The term `loki` in the config has been deprecated of favor of `logs`. This
Expand Down
78 changes: 51 additions & 27 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,78 +25,102 @@ import (
// DefaultConfig holds default settings for all the subsystems.
var DefaultConfig = Config{
// All subsystems with a DefaultConfig should be listed here.
Prometheus: metrics.DefaultConfig,
Metrics: metrics.DefaultConfig,
Integrations: integrations.DefaultManagerConfig,
}

// Config contains underlying configurations for the agent
type Config struct {
Server server.Config `yaml:"server,omitempty"`
Prometheus metrics.Config `yaml:"prometheus,omitempty"`
Metrics metrics.Config `yaml:"metrics,omitempty"`
Integrations integrations.ManagerConfig `yaml:"integrations,omitempty"`
Tempo tempo.Config `yaml:"tempo,omitempty"`

Logs *logs.Config `yaml:"logs,omitempty"`
Loki *logs.Config `yaml:"loki,omitempty"` // Deprecated: use Logs instead
UsedDeprecatedLoki bool `yaml:"-"`
Logs *logs.Config `yaml:"logs,omitempty"`

// We support a secondary server just for the /-/reload endpoint, since
// invoking /-/reload against the primary server can cause the server
// to restart.
ReloadAddress string `yaml:"-"`
ReloadPort int `yaml:"-"`

// Deprecated fields user has used. Generated during UnmarshalYAML.
Deprecations []string `yaml:"-"`
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Apply defaults to the config from our struct and any defaults inherited
// from flags.
// from flags before unmarshaling.
*c = DefaultConfig
util.DefaultConfigFromFlags(c)

type config Config
return unmarshal((*config)(c))
type baseConfig Config

type config struct {
baseConfig `yaml:",inline"`

// Deprecated field names:
Prometheus *metrics.Config `yaml:"prometheus,omitempty"`
Loki *logs.Config `yaml:"loki,omitempty"`
}

var fc config
fc.baseConfig = baseConfig(*c)

if err := unmarshal(&fc); err != nil {
return err
}

// Migrate old fields to the new name
if fc.Prometheus != nil && fc.Metrics.Unmarshaled && fc.Prometheus.Unmarshaled {
return fmt.Errorf("at most one of prometheus and metrics should be specified")
} else if fc.Prometheus != nil && fc.Prometheus.Unmarshaled {
fc.Deprecations = append(fc.Deprecations, "`prometheus` has been deprecated in favor of `metrics`")
fc.Metrics = *fc.Prometheus
fc.Prometheus = nil
}

if fc.Logs != nil && fc.Loki != nil {
return fmt.Errorf("at most one of loki and logs should be specified")
} else if fc.Logs == nil && fc.Loki != nil {
fc.Deprecations = append(fc.Deprecations, "`loki` has been deprecated in favor of `logs`")
fc.Logs = fc.Loki
fc.Loki = nil
}

*c = Config(fc.baseConfig)
return nil
}

// LogDeprecations will log use of any deprecated fields to l as warn-level
// messages.
func (c *Config) LogDeprecations(l log.Logger) {
if c.UsedDeprecatedLoki {
level.Warn(l).Log("msg", "DEPRECATION NOTICE: `loki` is deprecated in favor of `logs`")
for _, d := range c.Deprecations {
level.Warn(l).Log("msg", fmt.Sprintf("DEPRECATION NOTICE: %s", d))
}
}

// ApplyDefaults sets default values in the config
func (c *Config) ApplyDefaults() error {
if err := c.Prometheus.ApplyDefaults(); err != nil {
if err := c.Metrics.ApplyDefaults(); err != nil {
return err
}

if c.Logs != nil && c.Loki != nil {
return fmt.Errorf("at most one of loki and logs should be specified")
}

if c.Logs == nil && c.Loki != nil {
c.Logs = c.Loki
c.Loki = nil
c.UsedDeprecatedLoki = true
}

if err := c.Integrations.ApplyDefaults(&c.Prometheus); err != nil {
if err := c.Integrations.ApplyDefaults(&c.Metrics); err != nil {
return err
}

c.Prometheus.ServiceConfig.Lifecycler.ListenPort = c.Server.GRPCListenPort
c.Metrics.ServiceConfig.Lifecycler.ListenPort = c.Server.GRPCListenPort
c.Integrations.ListenPort = c.Server.HTTPListenPort
c.Integrations.ListenHost = c.Server.HTTPListenAddress

c.Integrations.ServerUsingTLS = c.Server.HTTPTLSConfig.TLSKeyPath != "" && c.Server.HTTPTLSConfig.TLSCertPath != ""

if len(c.Integrations.PrometheusRemoteWrite) == 0 {
c.Integrations.PrometheusRemoteWrite = c.Prometheus.Global.RemoteWrite
c.Integrations.PrometheusRemoteWrite = c.Metrics.Global.RemoteWrite
}

c.Integrations.PrometheusGlobalConfig = c.Prometheus.Global.Prometheus
c.Integrations.PrometheusGlobalConfig = c.Metrics.Global.Prometheus

// since the Tempo config might rely on an existing Loki config
// this check is made here to look for cross config issues before we attempt to load
Expand All @@ -111,7 +135,7 @@ func (c *Config) ApplyDefaults() error {
func (c *Config) RegisterFlags(f *flag.FlagSet) {
c.Server.MetricsNamespace = "agent"
c.Server.RegisterInstrumentation = true
c.Prometheus.RegisterFlags(f)
c.Metrics.RegisterFlags(f)
c.Server.RegisterFlags(f)

f.StringVar(&c.ReloadAddress, "reload-addr", "127.0.0.1", "address to expose a secondary server for /-/reload on.")
Expand Down
60 changes: 51 additions & 9 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ prometheus:
return LoadBytes([]byte(cfg), false, c)
})
require.NoError(t, err)
require.NotEmpty(t, c.Prometheus.ServiceConfig.Lifecycler.InfNames)
require.NotZero(t, c.Prometheus.ServiceConfig.Lifecycler.NumTokens)
require.NotZero(t, c.Prometheus.ServiceConfig.Lifecycler.HeartbeatPeriod)
require.NotEmpty(t, c.Metrics.ServiceConfig.Lifecycler.InfNames)
require.NotZero(t, c.Metrics.ServiceConfig.Lifecycler.NumTokens)
require.NotZero(t, c.Metrics.ServiceConfig.Lifecycler.HeartbeatPeriod)
require.True(t, c.Server.RegisterInstrumentation)
}

Expand All @@ -55,7 +55,7 @@ prometheus:
return LoadBytes([]byte(cfg), false, c)
})
require.NoError(t, err)
require.Equal(t, expect, c.Prometheus.Global)
require.Equal(t, expect, c.Metrics.Global)
}

func TestConfig_OverrideByEnvironmentOnLoad(t *testing.T) {
Expand All @@ -78,7 +78,7 @@ prometheus:
return LoadBytes([]byte(cfg), true, c)
})
require.NoError(t, err)
require.Equal(t, expect, c.Prometheus.Global)
require.Equal(t, expect, c.Metrics.Global)
}

func TestConfig_OverrideByEnvironmentOnLoad_NoDigits(t *testing.T) {
Expand All @@ -95,7 +95,7 @@ prometheus:
return LoadBytes([]byte(cfg), true, c)
})
require.NoError(t, err)
require.Equal(t, expect, c.Prometheus.Global.Prometheus.ExternalLabels)
require.Equal(t, expect, c.Metrics.Global.Prometheus.ExternalLabels)
}

func TestConfig_FlagsAreAccepted(t *testing.T) {
Expand All @@ -115,7 +115,7 @@ prometheus:
return LoadBytes([]byte(cfg), false, c)
})
require.NoError(t, err)
require.Equal(t, "/tmp/wal", c.Prometheus.WALDir)
require.Equal(t, "/tmp/wal", c.Metrics.WALDir)
}

func TestConfig_StrictYamlParsing(t *testing.T) {
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestConfig_Defaults(t *testing.T) {
err := LoadBytes([]byte(`{}`), false, &c)
require.NoError(t, err)

require.Equal(t, metrics.DefaultConfig, c.Prometheus)
require.Equal(t, metrics.DefaultConfig, c.Metrics)
require.Equal(t, integrations.DefaultManagerConfig, c.Integrations)
}

Expand Down Expand Up @@ -216,9 +216,51 @@ loki:
require.NoError(t, LoadBytes([]byte(input), false, &cfg))
require.NoError(t, cfg.ApplyDefaults())

require.Nil(t, cfg.Loki)
require.NotNil(t, cfg.Logs)
require.Equal(t, "foo", cfg.Logs.Configs[0].Name)
require.Equal(t, []string{"`loki` has been deprecated in favor of `logs`"}, cfg.Deprecations)
}

func TestConfig_PrometheusNonNil(t *testing.T) {
tt := []struct {
name string
input string
}{
{
name: "missing",
input: `{}`,
},
{
name: "null",
input: `prometheus: null`,
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var cfg Config
require.NoError(t, LoadBytes([]byte(tc.input), false, &cfg))
require.NoError(t, cfg.ApplyDefaults())

require.NotNil(t, cfg.Metrics)
})
}
}

func TestConfig_PrometheusNameMigration(t *testing.T) {
input := util.Untab(`
prometheus:
wal_directory: /tmp
configs:
- name: default
`)
var cfg Config
require.NoError(t, LoadBytes([]byte(input), false, &cfg))
require.NoError(t, cfg.ApplyDefaults())

require.Equal(t, "default", cfg.Metrics.Configs[0].Name)
require.Equal(t, "/tmp", cfg.Metrics.WALDir)
require.Equal(t, []string{"`prometheus` has been deprecated in favor of `metrics`"}, cfg.Deprecations)
}

func TestConfig_TempoLokiFailsValidation(t *testing.T) {
Expand Down
25 changes: 19 additions & 6 deletions pkg/metrics/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ type Config struct {
Configs []instance.Config `yaml:"configs,omitempty,omitempty"`
InstanceRestartBackoff time.Duration `yaml:"instance_restart_backoff,omitempty"`
InstanceMode instance.Mode `yaml:"instance_mode,omitempty"`

// Unmarshaled is true when the Config was unmarshaled from YAML.
Unmarshaled bool `yaml:"-"`
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig
util.DefaultConfigFromFlags(c)
c.Unmarshaled = true

type plain Config
err := unmarshal((*plain)(c))
Expand Down Expand Up @@ -97,13 +102,21 @@ func (c *Config) ApplyDefaults() error {

// RegisterFlags defines flags corresponding to the Config.
func (c *Config) RegisterFlags(f *flag.FlagSet) {
f.StringVar(&c.WALDir, "prometheus.wal-directory", "", "base directory to store the WAL in")
f.DurationVar(&c.WALCleanupAge, "prometheus.wal-cleanup-age", DefaultConfig.WALCleanupAge, "remove abandoned (unused) WALs older than this")
f.DurationVar(&c.WALCleanupPeriod, "prometheus.wal-cleanup-period", DefaultConfig.WALCleanupPeriod, "how often to check for abandoned WALs")
f.DurationVar(&c.InstanceRestartBackoff, "prometheus.instance-restart-backoff", DefaultConfig.InstanceRestartBackoff, "how long to wait before restarting a failed Prometheus instance")
c.RegisterFlagsWithPrefix("metrics.", f)

// Register deprecated flag names.
c.RegisterFlagsWithPrefix("prometheus.", f)
}

// RegisterFlagsWithPrefix defines flags with the provided prefix.
func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
f.StringVar(&c.WALDir, prefix+"wal-directory", "", "base directory to store the WAL in")
f.DurationVar(&c.WALCleanupAge, prefix+"wal-cleanup-age", DefaultConfig.WALCleanupAge, "remove abandoned (unused) WALs older than this")
f.DurationVar(&c.WALCleanupPeriod, prefix+"wal-cleanup-period", DefaultConfig.WALCleanupPeriod, "how often to check for abandoned WALs")
f.DurationVar(&c.InstanceRestartBackoff, prefix+"instance-restart-backoff", DefaultConfig.InstanceRestartBackoff, "how long to wait before restarting a failed Prometheus instance")

c.ServiceConfig.RegisterFlagsWithPrefix("prometheus.service.", f)
c.ServiceClientConfig.RegisterFlags(f)
c.ServiceConfig.RegisterFlagsWithPrefix(prefix+"service.", f)
c.ServiceClientConfig.RegisterFlagsWithPrefix(prefix, f)
}

// Agent is an agent for collecting Prometheus metrics. It acts as a
Expand Down
8 changes: 7 additions & 1 deletion pkg/metrics/cluster/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {

// RegisterFlags registers flags to the provided flag set.
func (c *Config) RegisterFlags(f *flag.FlagSet) {
c.GRPCClientConfig.RegisterFlagsWithPrefix("prometheus.service-client", f)
c.RegisterFlagsWithPrefix("prometheus.", f)
}

// RegisterFlagsWithPrefix registers flags to the provided flag set with the
// specified prefix.
func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
c.GRPCClientConfig.RegisterFlagsWithPrefix(prefix+"service-client", f)
}

// New returns a new scraping service client.
Expand Down