Skip to content

Commit

Permalink
feat(agent): Enable watching for new files
Browse files Browse the repository at this point in the history
This allows for watching for new files in a configuraiton directory.
When a new file is found, Telegraf will reload with that file added to
the list of files to watch. Note this will reload when any file, a
telegraf config or not, is added to the directory.

TODO: notify type poll does not seem to work, only inotify
TODO: test on Windows

fixes: #12389
fixes: #12264
  • Loading branch information
powersj committed Jul 19, 2024
1 parent d58525b commit c6fbdb2
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -157,6 +158,18 @@ func (t *Telegraf) reloadLoop() error {
go t.watchLocalConfig(ctx, signals, fConfig)
}
}
for _, fConfigDirectory := range t.configDir {
absPath, err := filepath.Abs(fConfigDirectory)
if err != nil {
log.Printf("W! Cannot watch config directory %s: %s", fConfigDirectory, err)
continue
}
if _, err := os.Stat(absPath); err != nil {
log.Printf("W! Cannot watch config directory %s: %s", fConfigDirectory, err)
} else {
go t.watchLocalDirectory(ctx, signals, absPath)
}
}
}
if t.configURLWatchInterval > 0 {
remoteConfigs := make([]string, 0)
Expand Down Expand Up @@ -240,6 +253,32 @@ func (t *Telegraf) watchLocalConfig(ctx context.Context, signals chan os.Signal,
signals <- syscall.SIGHUP
}

func (t *Telegraf) watchLocalDirectory(ctx context.Context, signals chan os.Signal, directory string) {
var mytomb tomb.Tomb
var watcher watch.FileWatcher
if t.watchConfig == "poll" {
watcher = watch.NewPollingFileWatcher(directory)
} else {
watcher = watch.NewInotifyFileWatcher(directory)
}
changes, err := watcher.ChangeEvents(&mytomb, 0)
if err != nil {
log.Printf("E! Error watching config directory %q: %s\n", directory, err)
return
}
log.Printf("I! Config watcher started for directory %s\n", directory)
select {
case <-ctx.Done():
mytomb.Done()
case <-changes.Created:
log.Printf("I! Config directory %q has new file(s)\n", directory)
mytomb.Done()
signals <- syscall.SIGHUP
case <-mytomb.Dying():
log.Printf("I! Config directory watcher %q ended\n", directory)
}
}

func (t *Telegraf) watchRemoteConfigs(ctx context.Context, signals chan os.Signal, interval time.Duration, remoteConfigs []string) {
configs := strings.Join(remoteConfigs, ", ")
log.Printf("I! Remote config watcher started for: %s\n", configs)
Expand Down

0 comments on commit c6fbdb2

Please sign in to comment.