Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply defaults to undefined sections #546

Merged
merged 2 commits into from
Apr 16, 2021
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ 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)

- [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)
}