Skip to content

feat(agent): Add debounce for watch events #17048

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions cmd/telegraf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
configURLWatchInterval: cCtx.Duration("config-url-watch-interval"),
watchConfig: cCtx.String("watch-config"),
watchInterval: cCtx.Duration("watch-interval"),
watchDebounceInterval: cCtx.Duration("watch-debounce-interval"),
pidFile: cCtx.String("pidfile"),
plugindDir: cCtx.String("plugin-directory"),
password: cCtx.String("password"),
Expand Down Expand Up @@ -295,6 +296,12 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
Usage: "monitoring config changes [notify, poll] of --config and --config-directory options. " +
"Notify supports linux, *bsd, and macOS. Poll is required for Windows and checks every 250ms.",
},
&cli.DurationFlag{
Name: "watch-debounce-interval",
Usage: "Time duration to wait after a config change before reloading",
DefaultText: "0s",
Value: 0,
},
&cli.StringFlag{
Name: "pidfile",
Usage: "file to write our pid to",
Expand Down
119 changes: 97 additions & 22 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type GlobalFlags struct {
configURLWatchInterval time.Duration
watchConfig string
watchInterval time.Duration
watchDebounceInterval time.Duration
pidFile string
plugindDir string
password string
Expand Down Expand Up @@ -231,30 +232,104 @@ func (t *Telegraf) watchLocalConfig(ctx context.Context, signals chan os.Signal,
return
}
log.Printf("I! Config watcher started for %s\n", fConfig)
select {
case <-ctx.Done():
mytomb.Done()
return
case <-changes.Modified:
log.Printf("I! Config file/directory %q modified\n", fConfig)
case <-changes.Deleted:
// deleted can mean moved. wait a bit a check existence
<-time.After(time.Second)
if _, err := os.Stat(fConfig); err == nil {
log.Printf("I! Config file/directory %q overwritten\n", fConfig)
} else {
log.Printf("W! Config file/directory %q deleted\n", fConfig)

// Setup debounce timer
var reloadTimer *time.Timer
var reloadPending bool

if t.watchDebounceInterval > 0 {
reloadTimer = time.NewTimer(t.watchDebounceInterval)
if !reloadTimer.Stop() {
<-reloadTimer.C // Drain if already fired
}
}

// Update resetTimer function:
resetTimer := func(reason string) {
log.Printf("%s", reason)

if t.watchDebounceInterval == 0 {
// No debouncing - trigger immediately
select {
case signals <- syscall.SIGHUP:
case <-ctx.Done():
return
}
return
}

if !reloadPending {
reloadPending = true
}

// Properly drain and reset timer
if !reloadTimer.Stop() {
select {
case <-reloadTimer.C:
default:
}
}
reloadTimer.Reset(t.watchDebounceInterval)
}

for {
select {
case <-ctx.Done():
reloadTimer.Stop()
mytomb.Done()
return

case <-changes.Modified:
resetTimer(fmt.Sprintf("I! Config file/directory %q modified\n", fConfig))

case <-changes.Deleted:
// Use select with timeout instead of blocking wait
timer := time.NewTimer(time.Second)
select {
case <-timer.C:
// Proceed with file existence check
case <-ctx.Done():
timer.Stop()
return
}

var reason string
if _, err := os.Stat(fConfig); err == nil {
reason = fmt.Sprintf("I! Config file/directory %q overwritten\n", fConfig)
} else {
reason = fmt.Sprintf("W! Config file/directory %q deleted\n", fConfig)
}
resetTimer(reason)

case <-changes.Truncated:
resetTimer(fmt.Sprintf("I! Config file/directory %q truncated\n", fConfig))

case <-changes.Created:
resetTimer(fmt.Sprintf("I! Config directory %q has new file(s)\n", fConfig))

case <-func() <-chan time.Time {
if reloadTimer != nil {
return reloadTimer.C
}
// Return a channel that never fires when debouncing is disabled
return make(<-chan time.Time)
}():
if reloadPending {
log.Printf("I! Debounce period elapsed, triggering config reload for %q\n", fConfig)
select {
case signals <- syscall.SIGHUP:
case <-ctx.Done():
return
}
reloadPending = false
}

case <-mytomb.Dying():
reloadTimer.Stop()
log.Printf("I! Config watcher %q ended\n", fConfig)
return
}
case <-changes.Truncated:
log.Printf("I! Config file/directory %q truncated\n", fConfig)
case <-changes.Created:
log.Printf("I! Config directory %q has new file(s)\n", fConfig)
case <-mytomb.Dying():
log.Printf("I! Config watcher %q ended\n", fConfig)
return
}
mytomb.Done()
signals <- syscall.SIGHUP
}

func (*Telegraf) watchRemoteConfigs(ctx context.Context, signals chan os.Signal, interval time.Duration, remoteConfigs []string) {
Expand Down
Loading