feat(agent): Watch default config files if none specified (#13774)
This commit is contained in:
parent
6f6d5b5be8
commit
0b7ec728b6
|
|
@ -232,10 +232,13 @@ func (t *Telegraf) loadConfiguration() (*config.Config, error) {
|
||||||
configFiles = append(configFiles, files...)
|
configFiles = append(configFiles, files...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// providing no "config" or "config-directory" flag(s) should load default
|
// load default config paths if none are found
|
||||||
// configuration files
|
|
||||||
if len(configFiles) == 0 {
|
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
|
t.configFiles = configFiles
|
||||||
|
|
|
||||||
|
|
@ -441,30 +441,17 @@ func isURL(str string) bool {
|
||||||
|
|
||||||
// LoadConfig loads the given config files and applies it to c
|
// LoadConfig loads the given config files and applies it to c
|
||||||
func (c *Config) LoadConfig(path string) error {
|
func (c *Config) LoadConfig(path string) error {
|
||||||
var err error
|
if !c.Agent.Quiet {
|
||||||
paths := []string{}
|
log.Printf("I! Loading config: %s", path)
|
||||||
|
|
||||||
if path == "" {
|
|
||||||
if paths, err = GetDefaultConfigPath(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
paths = append(paths, path)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, path := range paths {
|
data, _, err := LoadConfigFile(path)
|
||||||
if !c.Agent.Quiet {
|
if err != nil {
|
||||||
log.Printf("I! Loading config: %s", path)
|
return fmt.Errorf("error loading config file %s: %w", path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data, _, err := LoadConfigFile(path)
|
if err = c.LoadConfigData(data); err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("error loading config file %s: %w", path, err)
|
||||||
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
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -446,6 +446,7 @@ func TestConfig_AzureMonitorNamespacePrefix(t *testing.T) {
|
||||||
func TestGetDefaultConfigPathFromEnvURL(t *testing.T) {
|
func TestGetDefaultConfigPathFromEnvURL(t *testing.T) {
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write([]byte("[agent]\ndebug = true"))
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -454,8 +455,7 @@ func TestGetDefaultConfigPathFromEnvURL(t *testing.T) {
|
||||||
configPath, err := config.GetDefaultConfigPath()
|
configPath, err := config.GetDefaultConfigPath()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, []string{ts.URL}, configPath)
|
require.Equal(t, []string{ts.URL}, configPath)
|
||||||
err = c.LoadConfig("")
|
require.NoError(t, c.LoadConfig(configPath[0]))
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConfig_URLLikeFileName(t *testing.T) {
|
func TestConfig_URLLikeFileName(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue