feat(agent): Watch default config files if none specified (#13774)

This commit is contained in:
Joshua Powers 2023-08-28 02:23:54 -06:00 committed by GitHub
parent 6f6d5b5be8
commit 0b7ec728b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 26 deletions

View File

@ -232,10 +232,13 @@ func (t *Telegraf) loadConfiguration() (*config.Config, error) {
configFiles = append(configFiles, files...)
}
// providing no "config" or "config-directory" flag(s) should load default
// configuration files
// load default config paths if none are found
if len(configFiles) == 0 {
configFiles = append(configFiles, "")
defaultFiles, err := config.GetDefaultConfigPath()
if err != nil {
return nil, fmt.Errorf("unable to load default config paths: %w", err)
}
configFiles = append(configFiles, defaultFiles...)
}
t.configFiles = configFiles

View File

@ -441,30 +441,17 @@ func isURL(str string) bool {
// LoadConfig loads the given config files and applies it to c
func (c *Config) LoadConfig(path string) error {
var err error
paths := []string{}
if path == "" {
if paths, err = GetDefaultConfigPath(); err != nil {
return err
}
} else {
paths = append(paths, path)
if !c.Agent.Quiet {
log.Printf("I! Loading config: %s", path)
}
for _, path := range paths {
if !c.Agent.Quiet {
log.Printf("I! Loading config: %s", path)
}
data, _, err := LoadConfigFile(path)
if err != nil {
return fmt.Errorf("error loading config file %s: %w", path, err)
}
data, _, err := LoadConfigFile(path)
if err != nil {
return fmt.Errorf("error loading config file %s: %w", path, err)
}
if err = c.LoadConfigData(data); err != nil {
return fmt.Errorf("error loading config file %s: %w", path, err)
}
if err = c.LoadConfigData(data); err != nil {
return fmt.Errorf("error loading config file %s: %w", path, err)
}
return nil

View File

@ -446,6 +446,7 @@ func TestConfig_AzureMonitorNamespacePrefix(t *testing.T) {
func TestGetDefaultConfigPathFromEnvURL(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("[agent]\ndebug = true"))
}))
defer ts.Close()
@ -454,8 +455,7 @@ func TestGetDefaultConfigPathFromEnvURL(t *testing.T) {
configPath, err := config.GetDefaultConfigPath()
require.NoError(t, err)
require.Equal(t, []string{ts.URL}, configPath)
err = c.LoadConfig("")
require.NoError(t, err)
require.NoError(t, c.LoadConfig(configPath[0]))
}
func TestConfig_URLLikeFileName(t *testing.T) {