2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2016-09-22 17:10:51 +00:00
|
|
|
"sync/atomic"
|
2016-09-08 14:22:10 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2016-09-22 17:10:51 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2018-09-28 21:48:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
func TestAdd(t *testing.T) {
|
2016-09-08 14:22:10 +00:00
|
|
|
a := &TestAggregator{}
|
2016-09-22 17:10:51 +00:00
|
|
|
ra := NewRunningAggregator(a, &AggregatorConfig{
|
|
|
|
Name: "TestRunningAggregator",
|
|
|
|
Filter: Filter{
|
|
|
|
NamePass: []string{"*"},
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
2016-10-07 15:43:44 +00:00
|
|
|
Period: time.Millisecond * 500,
|
2016-09-22 17:10:51 +00:00
|
|
|
})
|
2018-11-05 21:34:28 +00:00
|
|
|
require.NoError(t, ra.Config.Filter.Compile())
|
2016-09-22 17:10:51 +00:00
|
|
|
acc := testutil.Accumulator{}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
now := time.Now()
|
2019-03-29 22:40:33 +00:00
|
|
|
ra.UpdateWindow(now, now.Add(ra.Config.Period))
|
2018-11-05 21:34:28 +00:00
|
|
|
|
|
|
|
m := testutil.MustMetric("RITest",
|
2016-09-08 14:22:10 +00:00
|
|
|
map[string]string{},
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2016-10-10 12:43:47 +00:00
|
|
|
time.Now().Add(time.Millisecond*150),
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
2018-11-05 21:34:28 +00:00
|
|
|
require.False(t, ra.Add(m))
|
|
|
|
ra.Push(&acc)
|
2016-09-22 17:10:51 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
require.Equal(t, 1, len(acc.Metrics))
|
|
|
|
require.Equal(t, int64(101), acc.Metrics[0].Fields["sum"])
|
2016-10-10 12:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddMetricsOutsideCurrentPeriod(t *testing.T) {
|
|
|
|
a := &TestAggregator{}
|
|
|
|
ra := NewRunningAggregator(a, &AggregatorConfig{
|
|
|
|
Name: "TestRunningAggregator",
|
|
|
|
Filter: Filter{
|
|
|
|
NamePass: []string{"*"},
|
|
|
|
},
|
|
|
|
Period: time.Millisecond * 500,
|
|
|
|
})
|
2018-11-05 21:34:28 +00:00
|
|
|
require.NoError(t, ra.Config.Filter.Compile())
|
2016-10-10 12:43:47 +00:00
|
|
|
acc := testutil.Accumulator{}
|
2018-11-05 21:34:28 +00:00
|
|
|
now := time.Now()
|
2019-03-29 22:40:33 +00:00
|
|
|
ra.UpdateWindow(now, now.Add(ra.Config.Period))
|
2016-10-10 12:43:47 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
m := testutil.MustMetric("RITest",
|
2016-10-10 12:43:47 +00:00
|
|
|
map[string]string{},
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2018-11-05 21:34:28 +00:00
|
|
|
now.Add(-time.Hour),
|
|
|
|
telegraf.Untyped,
|
|
|
|
)
|
|
|
|
require.False(t, ra.Add(m))
|
2016-10-10 12:43:47 +00:00
|
|
|
|
|
|
|
// metric after current period
|
2018-11-05 21:34:28 +00:00
|
|
|
m = testutil.MustMetric("RITest",
|
2016-10-10 12:43:47 +00:00
|
|
|
map[string]string{},
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2018-11-05 21:34:28 +00:00
|
|
|
now.Add(time.Hour),
|
|
|
|
telegraf.Untyped,
|
|
|
|
)
|
|
|
|
require.False(t, ra.Add(m))
|
2016-10-10 12:43:47 +00:00
|
|
|
|
|
|
|
// "now" metric
|
2018-11-05 21:34:28 +00:00
|
|
|
m = testutil.MustMetric("RITest",
|
2016-10-10 12:43:47 +00:00
|
|
|
map[string]string{},
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2016-10-10 12:43:47 +00:00
|
|
|
time.Now().Add(time.Millisecond*50),
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
2018-11-05 21:34:28 +00:00
|
|
|
require.False(t, ra.Add(m))
|
2016-10-10 12:43:47 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
ra.Push(&acc)
|
|
|
|
require.Equal(t, 1, len(acc.Metrics))
|
2019-03-29 22:40:33 +00:00
|
|
|
require.Equal(t, int64(101), acc.Metrics[0].Fields["sum"])
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
func TestAddAndPushOnePeriod(t *testing.T) {
|
|
|
|
a := &TestAggregator{}
|
|
|
|
ra := NewRunningAggregator(a, &AggregatorConfig{
|
|
|
|
Name: "TestRunningAggregator",
|
|
|
|
Filter: Filter{
|
|
|
|
NamePass: []string{"*"},
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
2016-09-22 17:10:51 +00:00
|
|
|
Period: time.Millisecond * 500,
|
|
|
|
})
|
2018-11-05 21:34:28 +00:00
|
|
|
require.NoError(t, ra.Config.Filter.Compile())
|
2016-09-22 17:10:51 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
now := time.Now()
|
2019-03-29 22:40:33 +00:00
|
|
|
ra.UpdateWindow(now, now.Add(ra.Config.Period))
|
2016-09-22 17:10:51 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
m := testutil.MustMetric("RITest",
|
2016-09-22 17:10:51 +00:00
|
|
|
map[string]string{},
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2016-10-10 12:43:47 +00:00
|
|
|
time.Now().Add(time.Millisecond*100),
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
2018-11-05 21:34:28 +00:00
|
|
|
require.False(t, ra.Add(m))
|
2016-09-22 17:10:51 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
ra.Push(&acc)
|
2016-09-22 17:10:51 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
acc.AssertContainsFields(t, "TestMetric", map[string]interface{}{"sum": int64(101)})
|
2016-09-22 17:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDropOriginal(t *testing.T) {
|
|
|
|
ra := NewRunningAggregator(&TestAggregator{}, &AggregatorConfig{
|
|
|
|
Name: "TestRunningAggregator",
|
|
|
|
Filter: Filter{
|
|
|
|
NamePass: []string{"RI*"},
|
|
|
|
},
|
|
|
|
DropOriginal: true,
|
|
|
|
})
|
2018-11-05 21:34:28 +00:00
|
|
|
require.NoError(t, ra.Config.Filter.Compile())
|
|
|
|
|
|
|
|
now := time.Now()
|
2019-03-29 22:40:33 +00:00
|
|
|
ra.UpdateWindow(now, now.Add(ra.Config.Period))
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
m := testutil.MustMetric("RITest",
|
2016-09-08 14:22:10 +00:00
|
|
|
map[string]string{},
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2018-11-05 21:34:28 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
2018-11-05 21:34:28 +00:00
|
|
|
require.True(t, ra.Add(m))
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
// this metric name doesn't match the filter, so Add will return false
|
2018-11-05 21:34:28 +00:00
|
|
|
m2 := testutil.MustMetric("foobar",
|
2016-09-08 14:22:10 +00:00
|
|
|
map[string]string{},
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2018-11-05 21:34:28 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
2018-11-05 21:34:28 +00:00
|
|
|
require.False(t, ra.Add(m2))
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:56:44 +00:00
|
|
|
func TestAddDoesNotModifyMetric(t *testing.T) {
|
|
|
|
ra := NewRunningAggregator(&TestAggregator{}, &AggregatorConfig{
|
|
|
|
Name: "TestRunningAggregator",
|
|
|
|
Filter: Filter{
|
|
|
|
FieldPass: []string{"a"},
|
|
|
|
},
|
|
|
|
DropOriginal: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, ra.Config.Filter.Compile())
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
m := testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"a": int64(42),
|
|
|
|
"b": int64(42),
|
|
|
|
},
|
|
|
|
now)
|
|
|
|
expected := m.Copy()
|
|
|
|
ra.Add(m)
|
|
|
|
|
|
|
|
testutil.RequireMetricEqual(t, expected, m)
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
type TestAggregator struct {
|
|
|
|
sum int64
|
|
|
|
}
|
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
func (t *TestAggregator) Description() string { return "" }
|
|
|
|
func (t *TestAggregator) SampleConfig() string { return "" }
|
2016-10-10 12:43:47 +00:00
|
|
|
func (t *TestAggregator) Reset() {
|
|
|
|
atomic.StoreInt64(&t.sum, 0)
|
|
|
|
}
|
2016-09-22 17:10:51 +00:00
|
|
|
|
|
|
|
func (t *TestAggregator) Push(acc telegraf.Accumulator) {
|
|
|
|
acc.AddFields("TestMetric",
|
|
|
|
map[string]interface{}{"sum": t.sum},
|
|
|
|
map[string]string{},
|
|
|
|
)
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2016-09-22 17:10:51 +00:00
|
|
|
func (t *TestAggregator) Add(in telegraf.Metric) {
|
2016-09-08 14:22:10 +00:00
|
|
|
for _, v := range in.Fields() {
|
|
|
|
if vi, ok := v.(int64); ok {
|
2016-09-22 17:10:51 +00:00
|
|
|
atomic.AddInt64(&t.sum, vi)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|