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

feat(agent): Watch for deleted files #15644

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
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)
srebhan marked this conversation as resolved.
Show resolved Hide resolved
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
Loading