Skip to content

Commit

Permalink
fixed merge window time drift issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Joachim DB Systel authored Nov 4, 2024
1 parent 5793ff6 commit 5bdf91e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion models/running_aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,34 @@ func (r *RunningAggregator) Push(acc telegraf.Accumulator) {
r.Lock()
defer r.Unlock()

// In case of time drift forward (e.g. after sleep)
// we will have intentionally a long period, so
// metrics got stuck in meantime will not lost.
since := r.periodEnd
until := r.periodEnd.Add(r.Config.Period)
until := time.Now().Add(r.Config.Period)

// Truncate() eliminates the monotonic clock from the
// time which otherwise may lead to miscalculation of
// the duration. We want to calculate the duration
// based on the wall clock time. The check, if a metric
// is discarded or not, ist based on the wall clock
// time.
duration := until.Truncate(-1).Sub(since.Truncate(-1))

if duration < r.Config.Period {
// In case of time drift backwards, a new
// period based on now is constructed.
since = time.Now()
until = since.Add(r.Config.Period)
}

// Note:
// If the time drifted out of the merge window
// and a metric with that new time is pushed and
// the merge window was not yet adjusted, this
// metric will discarded anyway. There is no way
// to prevent this :-(

r.UpdateWindow(since, until)

start := time.Now()
Expand Down

0 comments on commit 5bdf91e

Please sign in to comment.