Fix aggregator window and shutdown of multiple aggregators (#5644)
This commit is contained in:
@@ -288,11 +288,13 @@ func SleepContext(ctx context.Context, duration time.Duration) error {
|
||||
}
|
||||
|
||||
// AlignDuration returns the duration until next aligned interval.
|
||||
// If the current time is aligned a 0 duration is returned.
|
||||
func AlignDuration(tm time.Time, interval time.Duration) time.Duration {
|
||||
return AlignTime(tm, interval).Sub(tm)
|
||||
}
|
||||
|
||||
// AlignTime returns the time of the next aligned interval.
|
||||
// If the current time is aligned the current time is returned.
|
||||
func AlignTime(tm time.Time, interval time.Duration) time.Time {
|
||||
truncated := tm.Truncate(interval)
|
||||
if truncated == tm {
|
||||
|
||||
@@ -271,6 +271,40 @@ func TestAlignDuration(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlignTime(t *testing.T) {
|
||||
rfc3339 := func(value string) time.Time {
|
||||
t, _ := time.Parse(time.RFC3339, value)
|
||||
return t
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
now time.Time
|
||||
interval time.Duration
|
||||
expected time.Time
|
||||
}{
|
||||
{
|
||||
name: "aligned",
|
||||
now: rfc3339("2018-01-01T01:01:00Z"),
|
||||
interval: 10 * time.Second,
|
||||
expected: rfc3339("2018-01-01T01:01:00Z"),
|
||||
},
|
||||
{
|
||||
name: "aligned",
|
||||
now: rfc3339("2018-01-01T01:01:01Z"),
|
||||
interval: 10 * time.Second,
|
||||
expected: rfc3339("2018-01-01T01:01:10Z"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
actual := AlignTime(tt.now, tt.interval)
|
||||
require.Equal(t, tt.expected, actual)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTimestamp(t *testing.T) {
|
||||
time, err := ParseTimestamp("2019-02-20 21:50:34.029665", "2006-01-02 15:04:05.000000")
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -74,9 +75,14 @@ func (r *RunningAggregator) Period() time.Duration {
|
||||
return r.Config.Period
|
||||
}
|
||||
|
||||
func (r *RunningAggregator) SetPeriodStart(start time.Time) {
|
||||
func (r *RunningAggregator) EndPeriod() time.Time {
|
||||
return r.periodEnd
|
||||
}
|
||||
|
||||
func (r *RunningAggregator) UpdateWindow(start, until time.Time) {
|
||||
r.periodStart = start
|
||||
r.periodEnd = r.periodStart.Add(r.Config.Period).Add(r.Config.Delay)
|
||||
r.periodEnd = until
|
||||
log.Printf("D! [%s] Updated aggregation range [%s, %s]", r.Name(), start, until)
|
||||
}
|
||||
|
||||
func (r *RunningAggregator) MakeMetric(metric telegraf.Metric) telegraf.Metric {
|
||||
@@ -97,10 +103,6 @@ func (r *RunningAggregator) MakeMetric(metric telegraf.Metric) telegraf.Metric {
|
||||
return m
|
||||
}
|
||||
|
||||
func (r *RunningAggregator) metricDropped(metric telegraf.Metric) {
|
||||
r.MetricsDropped.Incr(1)
|
||||
}
|
||||
|
||||
// Add a metric to the aggregator and return true if the original metric
|
||||
// should be dropped.
|
||||
func (r *RunningAggregator) Add(m telegraf.Metric) bool {
|
||||
@@ -108,22 +110,25 @@ func (r *RunningAggregator) Add(m telegraf.Metric) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Make a copy of the metric but don't retain tracking; it doesn't make
|
||||
// sense to fail a metric's delivery due to the aggregation not being
|
||||
// sent because we can't create aggregations of historical data.
|
||||
// Make a copy of the metric but don't retain tracking. We do not fail a
|
||||
// delivery due to the aggregation not being sent because we can't create
|
||||
// aggregations of historical data. Additionally, waiting for the
|
||||
// aggregation to be pushed would introduce a hefty latency to delivery.
|
||||
m = metric.FromMetric(m)
|
||||
|
||||
r.Config.Filter.Modify(m)
|
||||
if len(m.FieldList()) == 0 {
|
||||
r.metricDropped(m)
|
||||
r.MetricsFiltered.Incr(1)
|
||||
return r.Config.DropOriginal
|
||||
}
|
||||
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
if r.periodStart.IsZero() || m.Time().After(r.periodEnd) {
|
||||
r.metricDropped(m)
|
||||
if m.Time().Before(r.periodStart) || m.Time().After(r.periodEnd.Add(r.Config.Delay)) {
|
||||
log.Printf("D! [%s] metric is outside aggregation window; discarding. %s: m: %s e: %s",
|
||||
r.Name(), m.Time(), r.periodStart, r.periodEnd)
|
||||
r.MetricsDropped.Incr(1)
|
||||
return r.Config.DropOriginal
|
||||
}
|
||||
|
||||
@@ -135,8 +140,10 @@ func (r *RunningAggregator) Push(acc telegraf.Accumulator) {
|
||||
r.Lock()
|
||||
defer r.Unlock()
|
||||
|
||||
r.periodStart = r.periodEnd
|
||||
r.periodEnd = r.periodStart.Add(r.Config.Period).Add(r.Config.Delay)
|
||||
since := r.periodEnd
|
||||
until := r.periodEnd.Add(r.Config.Period)
|
||||
r.UpdateWindow(since, until)
|
||||
|
||||
r.push(acc)
|
||||
r.Aggregator.Reset()
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func TestAdd(t *testing.T) {
|
||||
acc := testutil.Accumulator{}
|
||||
|
||||
now := time.Now()
|
||||
ra.SetPeriodStart(now)
|
||||
ra.UpdateWindow(now, now.Add(ra.Config.Period))
|
||||
|
||||
m := testutil.MustMetric("RITest",
|
||||
map[string]string{},
|
||||
@@ -51,7 +51,7 @@ func TestAddMetricsOutsideCurrentPeriod(t *testing.T) {
|
||||
require.NoError(t, ra.Config.Filter.Compile())
|
||||
acc := testutil.Accumulator{}
|
||||
now := time.Now()
|
||||
ra.SetPeriodStart(now)
|
||||
ra.UpdateWindow(now, now.Add(ra.Config.Period))
|
||||
|
||||
m := testutil.MustMetric("RITest",
|
||||
map[string]string{},
|
||||
@@ -86,7 +86,7 @@ func TestAddMetricsOutsideCurrentPeriod(t *testing.T) {
|
||||
|
||||
ra.Push(&acc)
|
||||
require.Equal(t, 1, len(acc.Metrics))
|
||||
require.Equal(t, int64(202), acc.Metrics[0].Fields["sum"])
|
||||
require.Equal(t, int64(101), acc.Metrics[0].Fields["sum"])
|
||||
}
|
||||
|
||||
func TestAddAndPushOnePeriod(t *testing.T) {
|
||||
@@ -102,7 +102,7 @@ func TestAddAndPushOnePeriod(t *testing.T) {
|
||||
acc := testutil.Accumulator{}
|
||||
|
||||
now := time.Now()
|
||||
ra.SetPeriodStart(now)
|
||||
ra.UpdateWindow(now, now.Add(ra.Config.Period))
|
||||
|
||||
m := testutil.MustMetric("RITest",
|
||||
map[string]string{},
|
||||
@@ -129,7 +129,7 @@ func TestAddDropOriginal(t *testing.T) {
|
||||
require.NoError(t, ra.Config.Filter.Compile())
|
||||
|
||||
now := time.Now()
|
||||
ra.SetPeriodStart(now)
|
||||
ra.UpdateWindow(now, now.Add(ra.Config.Period))
|
||||
|
||||
m := testutil.MustMetric("RITest",
|
||||
map[string]string{},
|
||||
|
||||
Reference in New Issue
Block a user