2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-09-22 17:10:51 +00:00
|
|
|
"sync"
|
|
|
|
"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"
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
assert.NoError(t, ra.Config.Filter.Compile())
|
2016-09-22 17:10:51 +00:00
|
|
|
acc := testutil.Accumulator{}
|
2018-01-19 01:37:53 +00:00
|
|
|
go ra.Run(&acc, make(chan struct{}))
|
2016-09-08 14:22:10 +00:00
|
|
|
|
|
|
|
m := ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
2016-10-10 12:43:47 +00:00
|
|
|
time.Now().Add(time.Millisecond*150),
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
2016-09-22 17:10:51 +00:00
|
|
|
assert.False(t, ra.Add(m))
|
|
|
|
|
|
|
|
for {
|
2016-10-10 12:43:47 +00:00
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
if atomic.LoadInt64(&a.sum) > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, int64(101), atomic.LoadInt64(&a.sum))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddMetricsOutsideCurrentPeriod(t *testing.T) {
|
|
|
|
a := &TestAggregator{}
|
|
|
|
ra := NewRunningAggregator(a, &AggregatorConfig{
|
|
|
|
Name: "TestRunningAggregator",
|
|
|
|
Filter: Filter{
|
|
|
|
NamePass: []string{"*"},
|
|
|
|
},
|
|
|
|
Period: time.Millisecond * 500,
|
|
|
|
})
|
|
|
|
assert.NoError(t, ra.Config.Filter.Compile())
|
|
|
|
acc := testutil.Accumulator{}
|
2018-01-19 01:37:53 +00:00
|
|
|
go ra.Run(&acc, make(chan struct{}))
|
2016-10-10 12:43:47 +00:00
|
|
|
|
|
|
|
// metric before current period
|
|
|
|
m := ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
time.Now().Add(-time.Hour),
|
|
|
|
)
|
|
|
|
assert.False(t, ra.Add(m))
|
|
|
|
|
|
|
|
// metric after current period
|
|
|
|
m = ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
time.Now().Add(time.Hour),
|
|
|
|
)
|
|
|
|
assert.False(t, ra.Add(m))
|
|
|
|
|
|
|
|
// "now" metric
|
|
|
|
m = ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
time.Now().Add(time.Millisecond*50),
|
|
|
|
)
|
|
|
|
assert.False(t, ra.Add(m))
|
|
|
|
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Millisecond)
|
2016-09-22 17:10:51 +00:00
|
|
|
if atomic.LoadInt64(&a.sum) > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, int64(101), atomic.LoadInt64(&a.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,
|
|
|
|
})
|
|
|
|
assert.NoError(t, ra.Config.Filter.Compile())
|
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
shutdown := make(chan struct{})
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-01-19 01:37:53 +00:00
|
|
|
ra.Run(&acc, shutdown)
|
2016-09-22 17:10:51 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
m := ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
2016-10-10 12:43:47 +00:00
|
|
|
time.Now().Add(time.Millisecond*100),
|
2016-09-22 17:10:51 +00:00
|
|
|
)
|
|
|
|
assert.False(t, ra.Add(m))
|
|
|
|
|
|
|
|
for {
|
2016-10-10 12:43:47 +00:00
|
|
|
time.Sleep(time.Millisecond)
|
2016-09-22 17:10:51 +00:00
|
|
|
if acc.NMetrics() > 0 {
|
|
|
|
break
|
|
|
|
}
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
2016-09-22 17:10:51 +00:00
|
|
|
acc.AssertContainsFields(t, "TestMetric", map[string]interface{}{"sum": int64(101)})
|
|
|
|
|
|
|
|
close(shutdown)
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDropOriginal(t *testing.T) {
|
|
|
|
ra := NewRunningAggregator(&TestAggregator{}, &AggregatorConfig{
|
|
|
|
Name: "TestRunningAggregator",
|
|
|
|
Filter: Filter{
|
|
|
|
NamePass: []string{"RI*"},
|
|
|
|
},
|
|
|
|
DropOriginal: true,
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
assert.NoError(t, ra.Config.Filter.Compile())
|
|
|
|
|
|
|
|
m := ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
time.Now(),
|
|
|
|
)
|
2016-09-22 17:10:51 +00:00
|
|
|
assert.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
|
2016-09-08 14:22:10 +00:00
|
|
|
m2 := ra.MakeMetric(
|
|
|
|
"foobar",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
time.Now(),
|
|
|
|
)
|
2016-09-22 17:10:51 +00:00
|
|
|
assert.False(t, ra.Add(m2))
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make an untyped, counter, & gauge metric
|
|
|
|
func TestMakeMetricA(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-09-22 17:10:51 +00:00
|
|
|
ra := NewRunningAggregator(&TestAggregator{}, &AggregatorConfig{
|
|
|
|
Name: "TestRunningAggregator",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
assert.Equal(t, "aggregators.TestRunningAggregator", ra.Name())
|
|
|
|
|
|
|
|
m := ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Untyped,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
m.Type(),
|
|
|
|
telegraf.Untyped,
|
|
|
|
)
|
|
|
|
|
|
|
|
m = ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Counter,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
m.Type(),
|
|
|
|
telegraf.Counter,
|
|
|
|
)
|
|
|
|
|
|
|
|
m = ra.MakeMetric(
|
|
|
|
"RITest",
|
|
|
|
map[string]interface{}{"value": int(101)},
|
|
|
|
map[string]string{},
|
|
|
|
telegraf.Gauge,
|
|
|
|
now,
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
2016-11-28 18:19:35 +00:00
|
|
|
fmt.Sprintf("RITest value=101i %d\n", now.UnixNano()),
|
2016-09-08 14:22:10 +00:00
|
|
|
m.String(),
|
|
|
|
)
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
m.Type(),
|
|
|
|
telegraf.Gauge,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|