Skip to content

Commit

Permalink
feat(agent): Watch for deleted files (#15644)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored Jul 24, 2024
1 parent 99a2df2 commit 26df1e7
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ func (t *Telegraf) reloadLoop() error {
case sig := <-signals:
if sig == syscall.SIGHUP {
log.Println("I! Reloading Telegraf config")
// May need to update the list of known config files
// if a delete or create occured. That way on the reload
// we ensure we watch the correct files.
if err := t.getConfigFiles(); err != nil {
log.Println("E! Error loading config files: ", err)
}
<-reload
reload <- true
}
Expand Down Expand Up @@ -223,11 +229,6 @@ func (t *Telegraf) watchLocalConfig(ctx context.Context, signals chan os.Signal,
log.Printf("I! Config file %q overwritten\n", fConfig)
} else {
log.Printf("W! Config file %q deleted\n", fConfig)
if err := watcher.BlockUntilExists(&mytomb); err != nil {
log.Printf("E! Cannot watch for config %q: %s\n", fConfig, err.Error())
return
}
log.Printf("I! Config file %q appeared\n", fConfig)
}
case <-changes.Truncated:
log.Printf("I! Config file %q truncated\n", fConfig)
Expand Down Expand Up @@ -284,17 +285,28 @@ func (t *Telegraf) loadConfiguration() (*config.Config, error) {
// If no other options are specified, load the config file and run.
c := config.NewConfig()
c.Agent.Quiet = t.quiet
c.Agent.ConfigURLRetryAttempts = t.configURLRetryAttempts
c.OutputFilters = t.outputFilters
c.InputFilters = t.inputFilters
c.SecretStoreFilters = t.secretstoreFilters

if err := t.getConfigFiles(); err != nil {
return c, err
}
if err := c.LoadAll(t.configFiles...); err != nil {
return c, err
}
return c, nil
}

func (t *Telegraf) getConfigFiles() error {
var configFiles []string

configFiles = append(configFiles, t.config...)
for _, fConfigDirectory := range t.configDir {
files, err := config.WalkDirectory(fConfigDirectory)
if err != nil {
return c, err
return err
}
configFiles = append(configFiles, files...)
}
Expand All @@ -303,17 +315,13 @@ func (t *Telegraf) loadConfiguration() (*config.Config, error) {
if len(configFiles) == 0 {
defaultFiles, err := config.GetDefaultConfigPath()
if err != nil {
return nil, fmt.Errorf("unable to load default config paths: %w", err)
return fmt.Errorf("unable to load default config paths: %w", err)
}
configFiles = append(configFiles, defaultFiles...)
}

c.Agent.ConfigURLRetryAttempts = t.configURLRetryAttempts
t.configFiles = configFiles
if err := c.LoadAll(configFiles...); err != nil {
return c, err
}
return c, nil
return nil
}

func (t *Telegraf) runAgent(ctx context.Context, reloadConfig bool) error {
Expand Down

0 comments on commit 26df1e7

Please sign in to comment.