Skip to content

Commit 38c01b4

Browse files
authored
Log after interval has elapsed; skip short intervals (influxdata#7854)
1 parent 0bcc515 commit 38c01b4

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

agent/agent.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (a *Agent) runInputs(
323323
wg.Add(1)
324324
go func(input *models.RunningInput) {
325325
defer wg.Done()
326-
a.gatherLoop(ctx, acc, input, ticker)
326+
a.gatherLoop(ctx, acc, input, ticker, interval)
327327
}(input)
328328
}
329329

@@ -454,13 +454,14 @@ func (a *Agent) gatherLoop(
454454
acc telegraf.Accumulator,
455455
input *models.RunningInput,
456456
ticker Ticker,
457+
interval time.Duration,
457458
) {
458459
defer panicRecover(input)
459460

460461
for {
461462
select {
462463
case <-ticker.Elapsed():
463-
err := a.gatherOnce(acc, input, ticker)
464+
err := a.gatherOnce(acc, input, ticker, interval)
464465
if err != nil {
465466
acc.AddError(err)
466467
}
@@ -476,18 +477,28 @@ func (a *Agent) gatherOnce(
476477
acc telegraf.Accumulator,
477478
input *models.RunningInput,
478479
ticker Ticker,
480+
interval time.Duration,
479481
) error {
480482
done := make(chan error)
481483
go func() {
482484
done <- input.Gather(acc)
483485
}()
484486

487+
// Only warn after interval seconds, even if the interval is started late.
488+
// Intervals can start late if the previous interval went over or due to
489+
// clock changes.
490+
slowWarning := time.NewTicker(interval)
491+
defer slowWarning.Stop()
492+
485493
for {
486494
select {
487495
case err := <-done:
488496
return err
497+
case <-slowWarning.C:
498+
log.Printf("W! [%s] Collection took longer than expected; not complete after interval of %s",
499+
input.LogName(), interval)
489500
case <-ticker.Elapsed():
490-
log.Printf("W! [agent] [%s] did not complete within its interval",
501+
log.Printf("D! [%s] Previous collection has not completed; scheduled collection skipped",
491502
input.LogName())
492503
}
493504
}

agent/tick.go

+17-10
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ type Ticker interface {
3131
// no maximum sleep, when using large intervals alignment is not corrected
3232
// until the next tick.
3333
type AlignedTicker struct {
34-
interval time.Duration
35-
jitter time.Duration
36-
ch chan time.Time
37-
cancel context.CancelFunc
38-
wg sync.WaitGroup
34+
interval time.Duration
35+
jitter time.Duration
36+
minInterval time.Duration
37+
ch chan time.Time
38+
cancel context.CancelFunc
39+
wg sync.WaitGroup
3940
}
4041

4142
func NewAlignedTicker(now time.Time, interval, jitter time.Duration) *AlignedTicker {
@@ -45,10 +46,11 @@ func NewAlignedTicker(now time.Time, interval, jitter time.Duration) *AlignedTic
4546
func newAlignedTicker(now time.Time, interval, jitter time.Duration, clock clock.Clock) *AlignedTicker {
4647
ctx, cancel := context.WithCancel(context.Background())
4748
t := &AlignedTicker{
48-
interval: interval,
49-
jitter: jitter,
50-
ch: make(chan time.Time, 1),
51-
cancel: cancel,
49+
interval: interval,
50+
jitter: jitter,
51+
minInterval: interval / 100,
52+
ch: make(chan time.Time, 1),
53+
cancel: cancel,
5254
}
5355

5456
d := t.next(now)
@@ -64,7 +66,12 @@ func newAlignedTicker(now time.Time, interval, jitter time.Duration, clock clock
6466
}
6567

6668
func (t *AlignedTicker) next(now time.Time) time.Duration {
67-
next := internal.AlignTime(now, t.interval)
69+
// Add minimum interval size to avoid scheduling an interval that is
70+
// exceptionally short. This avoids an issue that can occur where the
71+
// previous interval ends slightly early due to very minor clock changes.
72+
next := now.Add(t.minInterval)
73+
74+
next = internal.AlignTime(next, t.interval)
6875
d := next.Sub(now)
6976
if d == 0 {
7077
d = t.interval

0 commit comments

Comments
 (0)