Skip to content
This repository has been archived by the owner on Aug 30, 2019. It is now read-only.

Commit

Permalink
config: remove ini logic and use legacy package
Browse files Browse the repository at this point in the history
  • Loading branch information
gbbr committed Nov 22, 2018
1 parent 2b70985 commit 19befbc
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 725 deletions.
29 changes: 14 additions & 15 deletions config/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/config/legacy"
"github.com/DataDog/datadog-trace-agent/flags"
"github.com/DataDog/datadog-trace-agent/osutil"
writerconfig "github.com/DataDog/datadog-trace-agent/writer/config"
Expand Down Expand Up @@ -144,22 +146,12 @@ func New() *AgentConfig {
MaxConnections: 200, // in practice, rarely goes over 20
WatchdogInterval: time.Minute,

Ignore: make(map[string][]string),
Ignore: make(map[string][]string),
AnalyzedRateByServiceLegacy: make(map[string]float64),
AnalyzedSpansByService: make(map[string]map[string]float64),
}
}

// LoadIni reads the contents of the given INI file into the config.
func (c *AgentConfig) LoadIni(path string) error {
conf, err := NewIni(path)
if err != nil {
return err
}
c.loadIniConfig(conf)
return nil
}

// Validate validates if the current configuration is good for the agent to start with.
func (c *AgentConfig) validate() error {
if len(c.Endpoints) == 0 || c.Endpoints[0].APIKey == "" {
Expand Down Expand Up @@ -213,19 +205,20 @@ func (c *AgentConfig) acquireHostname() error {
// and a valid configuration can be returned based on defaults and environment variables. If a
// valid configuration can not be obtained, an error is returned.
func Load(path string) (*AgentConfig, error) {
cfg, err := loadFile(path)
cfg, err := loadConfig(path)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
} else {
log.Infof("Loaded configuration: %s", cfg.ConfigPath)
}
cfg.applyConfig()
cfg.loadEnv()
return cfg, cfg.validate()
}

func loadFile(path string) (*AgentConfig, error) {
func loadConfig(path string) (*AgentConfig, error) {
cfgPath := path
if cfgPath == flags.DefaultConfigPath && !osutil.Exists(cfgPath) && osutil.Exists(agent5Config) {
// attempting to load inexistent default path, but found existing Agent 5
Expand All @@ -236,13 +229,19 @@ func loadFile(path string) (*AgentConfig, error) {
cfg := New()
switch filepath.Ext(cfgPath) {
case ".ini", ".conf":
if err := cfg.LoadIni(cfgPath); err != nil {
ac, err := legacy.GetAgentConfig(cfgPath)
if err != nil {
return cfg, err
}
if err := legacy.FromAgentConfig(ac); err != nil {
return cfg, err
}
case ".yaml":
if err := cfg.loadYamlConfig(cfgPath); err != nil {
config.Datadog.SetConfigFile(cfgPath)
if err := config.Load(); err != nil {
return cfg, err
}
cfg.DDAgentBin = defaultDDAgentBin
default:
return cfg, errors.New("unrecognised file extension (need .yaml, .ini or .conf)")
}
Expand Down
59 changes: 32 additions & 27 deletions config/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ import (
"github.com/stretchr/testify/assert"
)

func cleanConfig() func() {
oldConfig := config.Datadog
config.Datadog = config.NewConfig("datadog", "DD", strings.NewReplacer(".", "_"))
return func() { config.Datadog = oldConfig }
}

func TestConfigHostname(t *testing.T) {
t.Run("nothing", func(t *testing.T) {
defer cleanConfig()()
assert := assert.New(t)
fallbackHostnameFunc = func() (string, error) {
return "", nil
Expand All @@ -27,6 +34,7 @@ func TestConfigHostname(t *testing.T) {
})

t.Run("fallback", func(t *testing.T) {
defer cleanConfig()()
host, err := os.Hostname()
if err != nil || host == "" {
// can't say
Expand All @@ -39,13 +47,15 @@ func TestConfigHostname(t *testing.T) {
})

t.Run("file", func(t *testing.T) {
defer cleanConfig()()
assert := assert.New(t)
cfg, err := Load("./testdata/full.yaml")
assert.NoError(err)
assert.Equal("mymachine", cfg.Hostname)
})

t.Run("env", func(t *testing.T) {
defer cleanConfig()()
// hostname from env
assert := assert.New(t)
err := os.Setenv(envHostname, "onlyenv")
Expand All @@ -57,6 +67,7 @@ func TestConfigHostname(t *testing.T) {
})

t.Run("file+env", func(t *testing.T) {
defer cleanConfig()()
// hostname from file, overwritten from env
assert := assert.New(t)
err := os.Setenv(envHostname, "envoverride")
Expand All @@ -79,6 +90,7 @@ func TestSite(t *testing.T) {
"override": {"./testdata/site_override.yaml", "some.other.datadoghq.eu"},
} {
t.Run(name, func(t *testing.T) {
defer cleanConfig()()
cfg, err := Load(tt.file)
assert.NoError(t, err)
assert.Equal(t, tt.url, cfg.Endpoints[0].Host)
Expand Down Expand Up @@ -114,10 +126,12 @@ func TestOnlyEnvConfig(t *testing.T) {
}

func TestOnlyDDAgentConfig(t *testing.T) {
defer cleanConfig()()
assert := assert.New(t)

c, err := loadFile("./testdata/no_apm_config.ini")
c, err := loadConfig("./testdata/no_apm_config.ini")
assert.NoError(err)
assert.NoError(c.applyConfig())

assert.Equal("thing", c.Hostname)
assert.Equal("apikey_12", c.Endpoints[0].APIKey)
Expand All @@ -127,21 +141,25 @@ func TestOnlyDDAgentConfig(t *testing.T) {
}

func TestDDAgentMultiAPIKeys(t *testing.T) {
defer cleanConfig()()
// old feature Datadog Agent feature, got dropped since
// TODO: at some point, expire this case
assert := assert.New(t)

c, err := loadFile("./testdata/multi_api_keys.ini")
c, err := loadConfig("./testdata/multi_api_keys.ini")
assert.NoError(err)
assert.NoError(c.applyConfig())

assert.Equal("foo", c.Endpoints[0].APIKey)
}

func TestFullIniConfig(t *testing.T) {
defer cleanConfig()()
assert := assert.New(t)

c, err := loadFile("./testdata/full.ini")
c, err := loadConfig("./testdata/full.ini")
assert.NoError(err)
assert.NoError(c.applyConfig())

assert.Equal("api_key_test", c.Endpoints[0].APIKey)
assert.Equal("mymachine", c.Hostname)
Expand All @@ -155,7 +173,7 @@ func TestFullIniConfig(t *testing.T) {
assert.Equal(5.0, c.MaxTPS)
assert.Equal(50.0, c.MaxEPS)
assert.Equal("0.0.0.0", c.ReceiverHost)
assert.Equal("0.0.0.0", c.StatsdHost)
assert.Equal("host.ip", c.StatsdHost)
assert.Equal("/path/to/file", c.LogFilePath)
assert.Equal("debug", c.LogLevel)
assert.False(c.LogThrottlingEnabled)
Expand Down Expand Up @@ -234,6 +252,7 @@ func TestFullIniConfig(t *testing.T) {
}

func TestFullYamlConfig(t *testing.T) {
defer cleanConfig()()
origcfg := config.Datadog
config.Datadog = config.NewConfig("datadog", "DD", strings.NewReplacer(".", "_"))
defer func() {
Expand All @@ -242,8 +261,9 @@ func TestFullYamlConfig(t *testing.T) {

assert := assert.New(t)

c, err := loadFile("./testdata/full.yaml")
c, err := loadConfig("./testdata/full.yaml")
assert.NoError(err)
assert.NoError(c.applyConfig())

assert.Equal("mymachine", c.Hostname)
assert.Equal("https://user:password@proxy_for_https:1234", c.ProxyURL.String())
Expand Down Expand Up @@ -315,15 +335,17 @@ func TestFullYamlConfig(t *testing.T) {
}

func TestUndocumentedYamlConfig(t *testing.T) {
defer cleanConfig()()
origcfg := config.Datadog
config.Datadog = config.NewConfig("datadog", "DD", strings.NewReplacer(".", "_"))
defer func() {
config.Datadog = origcfg
}()
assert := assert.New(t)

c, err := loadFile("./testdata/undocumented.yaml")
c, err := loadConfig("./testdata/undocumented.yaml")
assert.NoError(err)
assert.NoError(c.applyConfig())

assert.Equal("/path/to/bin", c.DDAgentBin)
assert.Equal("thing", c.Hostname)
Expand Down Expand Up @@ -368,25 +390,6 @@ func TestUndocumentedYamlConfig(t *testing.T) {
assert.Equal(0.05, c.AnalyzedSpansByService["db"]["intake"])
}

func TestConfigNewIfExists(t *testing.T) {
// The file does not exist: no error returned
conf, err := NewIni("/does-not-exist")
assert.True(t, os.IsNotExist(err))
assert.Nil(t, conf)

// The file exists but cannot be read for another reason: an error is
// returned.
filename := "/tmp/trace-agent-test-config.ini"
os.Remove(filename)
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0200) // write only
assert.Nil(t, err)
f.Close()
conf, err = NewIni(filename)
assert.NotNil(t, err)
assert.Nil(t, conf)
os.Remove(filename)
}

func TestAcquireHostname(t *testing.T) {
c := New()
err := c.acquireHostname()
Expand All @@ -396,10 +399,12 @@ func TestAcquireHostname(t *testing.T) {
}

func TestUndocumentedIni(t *testing.T) {
defer cleanConfig()()
assert := assert.New(t)

c, err := loadFile("./testdata/undocumented.ini")
c, err := loadConfig("./testdata/undocumented.ini")
assert.NoError(err)
assert.NoError(c.applyConfig())

// analysis legacy
assert.Equal(0.8, c.AnalyzedRateByServiceLegacy["web"])
Expand All @@ -408,7 +413,7 @@ func TestUndocumentedIni(t *testing.T) {
assert.Len(c.AnalyzedSpansByService, 2)
assert.Len(c.AnalyzedSpansByService["web"], 2)
assert.Len(c.AnalyzedSpansByService["db"], 1)
assert.Equal(0.8, c.AnalyzedSpansByService["web"]["request"])
assert.Equal(0.8, c.AnalyzedSpansByService["web"]["http.request"])
assert.Equal(0.9, c.AnalyzedSpansByService["web"]["django.request"])
assert.Equal(0.05, c.AnalyzedSpansByService["db"]["intake"])
}
Expand Down
118 changes: 0 additions & 118 deletions config/ini_file.go

This file was deleted.

Loading

0 comments on commit 19befbc

Please sign in to comment.