Skip to content

Commit

Permalink
Apply defaults to undefined sections (grafana#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfratto authored Apr 16, 2021
1 parent 1ab3d31 commit e874ac4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ cross-compilation issue, but will return in v0.13.0.

# Main (unreleased)

- [BUGFIX] Ensure defaults are applied to undefined sections in config file.
This fixes a problem where integrations didn't work if `prometheus:` wasn't
configured. (@rfratto)

# 0.14.0-rc.3 (2021-04-15)

- [ENHANCEMENT] Add `headers` field in `remote_write` config for Tempo. `headers`
specifies HTTP headers to forward to the remote endpoint. (@alexbiehl)
- [CHANGE] Add `tempo_spanmetrics` namespace in spanmetrics (@mapno)

- [BUGFIX] Grafana Agent running as a Windows service should start automatically on startup
(@mattdurham)
(@mattdurham)

- [BUGFIX] Validate that incoming scraped metrics do not have an empty label
set or a label set with duplicate labels, mirroring the behavior of
Expand Down
17 changes: 16 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import (
"gopkg.in/yaml.v2"
)

// DefaultConfig holds default settings for all the subsystems.
var DefaultConfig = Config{
// All subsystems with a DefaultConfig should be listed here.
Prometheus: prom.DefaultConfig,
Integrations: integrations.DefaultManagerConfig,
}

// Config contains underlying configurations for the agent
type Config struct {
Server server.Config `yaml:"server,omitempty"`
Expand All @@ -33,6 +40,13 @@ type Config struct {
ReloadPort int `yaml:"-"`
}

// UnmarshalYAML implements yaml.Unmarshaler.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig
type config Config
return unmarshal((*config)(c))
}

// ApplyDefaults sets default values in the config
func (c *Config) ApplyDefaults() error {
if err := c.Prometheus.ApplyDefaults(); err != nil {
Expand Down Expand Up @@ -103,8 +117,9 @@ func Load(fs *flag.FlagSet, args []string) (*Config, error) {
// doesn't require having a literal file on disk.
func load(fs *flag.FlagSet, args []string, loader func(string, bool, *Config) error) (*Config, error) {
var (
cfg = DefaultConfig

printVersion bool
cfg Config
file string
configExpandEnv bool
)
Expand Down
11 changes: 11 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"
"time"

"github.com/grafana/agent/pkg/integrations"
"github.com/grafana/agent/pkg/prom"
"github.com/grafana/agent/pkg/prom/instance"
"github.com/prometheus/common/model"
promCfg "github.com/prometheus/prometheus/config"
Expand Down Expand Up @@ -120,3 +122,12 @@ prometheus:
require.Error(t, err)
})
}

func TestConfig_Defaults(t *testing.T) {
var c Config
err := LoadBytes([]byte(`{}`), false, &c)
require.NoError(t, err)

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

0 comments on commit e874ac4

Please sign in to comment.