2016-03-07 14:46:23 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2016-07-25 12:09:49 +00:00
|
|
|
"bytes"
|
2016-03-07 14:46:23 +00:00
|
|
|
"fmt"
|
2016-07-25 12:09:49 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2016-03-07 14:46:23 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2020-05-04 18:09:10 +00:00
|
|
|
"github.com/influxdata/telegraf/models"
|
2016-03-07 14:46:23 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-07-25 12:09:49 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-03-07 14:46:23 +00:00
|
|
|
)
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
func TestAddFields(t *testing.T) {
|
|
|
|
metrics := make(chan telegraf.Metric, 10)
|
|
|
|
defer close(metrics)
|
|
|
|
a := NewAccumulator(&TestMetricMaker{}, metrics)
|
2016-08-31 16:27:37 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
tags := map[string]string{"foo": "bar"}
|
2016-09-08 14:22:10 +00:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
"usage": float64(99),
|
|
|
|
}
|
2018-03-28 00:30:51 +00:00
|
|
|
now := time.Now()
|
|
|
|
a.AddCounter("acctest", fields, tags, now)
|
2016-08-31 16:27:37 +00:00
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
testm := <-metrics
|
2016-08-31 16:27:37 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
require.Equal(t, "acctest", testm.Name())
|
|
|
|
actual, ok := testm.GetField("usage")
|
|
|
|
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, float64(99), actual)
|
|
|
|
|
|
|
|
actual, ok = testm.GetTag("foo")
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, "bar", actual)
|
2016-08-31 16:27:37 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
tm := testm.Time()
|
|
|
|
// okay if monotonic clock differs
|
|
|
|
require.True(t, now.Equal(tm))
|
|
|
|
|
|
|
|
tp := testm.Type()
|
|
|
|
require.Equal(t, telegraf.Counter, tp)
|
2016-08-31 16:27:37 +00:00
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
func TestAccAddError(t *testing.T) {
|
|
|
|
errBuf := bytes.NewBuffer(nil)
|
|
|
|
log.SetOutput(errBuf)
|
|
|
|
defer log.SetOutput(os.Stderr)
|
2016-08-31 16:27:37 +00:00
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
metrics := make(chan telegraf.Metric, 10)
|
|
|
|
defer close(metrics)
|
|
|
|
a := NewAccumulator(&TestMetricMaker{}, metrics)
|
2016-08-31 16:27:37 +00:00
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
a.AddError(fmt.Errorf("foo"))
|
|
|
|
a.AddError(fmt.Errorf("bar"))
|
|
|
|
a.AddError(fmt.Errorf("baz"))
|
2016-08-31 16:27:37 +00:00
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
errs := bytes.Split(errBuf.Bytes(), []byte{'\n'})
|
|
|
|
require.Len(t, errs, 4) // 4 because of trailing newline
|
|
|
|
assert.Contains(t, string(errs[0]), "TestPlugin")
|
|
|
|
assert.Contains(t, string(errs[0]), "foo")
|
|
|
|
assert.Contains(t, string(errs[1]), "TestPlugin")
|
|
|
|
assert.Contains(t, string(errs[1]), "bar")
|
|
|
|
assert.Contains(t, string(errs[2]), "TestPlugin")
|
|
|
|
assert.Contains(t, string(errs[2]), "baz")
|
2016-08-31 16:27:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
func TestSetPrecision(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
unset bool
|
|
|
|
precision time.Duration
|
|
|
|
timestamp time.Time
|
|
|
|
expected time.Time
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default precision is nanosecond",
|
|
|
|
unset: true,
|
|
|
|
timestamp: time.Date(2006, time.February, 10, 12, 0, 0, 82912748, time.UTC),
|
|
|
|
expected: time.Date(2006, time.February, 10, 12, 0, 0, 82912748, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "second interval",
|
2019-03-29 22:40:33 +00:00
|
|
|
precision: time.Second,
|
2018-03-28 00:30:51 +00:00
|
|
|
timestamp: time.Date(2006, time.February, 10, 12, 0, 0, 82912748, time.UTC),
|
|
|
|
expected: time.Date(2006, time.February, 10, 12, 0, 0, 0, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "microsecond interval",
|
2019-03-29 22:40:33 +00:00
|
|
|
precision: time.Microsecond,
|
2018-03-28 00:30:51 +00:00
|
|
|
timestamp: time.Date(2006, time.February, 10, 12, 0, 0, 82912748, time.UTC),
|
|
|
|
expected: time.Date(2006, time.February, 10, 12, 0, 0, 82913000, time.UTC),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2 second precision",
|
|
|
|
precision: 2 * time.Second,
|
|
|
|
timestamp: time.Date(2006, time.February, 10, 12, 0, 2, 4, time.UTC),
|
|
|
|
expected: time.Date(2006, time.February, 10, 12, 0, 2, 0, time.UTC),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
metrics := make(chan telegraf.Metric, 10)
|
|
|
|
|
|
|
|
a := NewAccumulator(&TestMetricMaker{}, metrics)
|
|
|
|
if !tt.unset {
|
2019-03-29 22:40:33 +00:00
|
|
|
a.SetPrecision(tt.precision)
|
2018-03-28 00:30:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a.AddFields("acctest",
|
|
|
|
map[string]interface{}{"value": float64(101)},
|
|
|
|
map[string]string{},
|
|
|
|
tt.timestamp,
|
|
|
|
)
|
|
|
|
|
|
|
|
testm := <-metrics
|
|
|
|
require.Equal(t, tt.expected, testm.Time())
|
|
|
|
|
|
|
|
close(metrics)
|
|
|
|
})
|
|
|
|
}
|
2016-04-12 23:06:27 +00:00
|
|
|
}
|
2016-07-25 12:09:49 +00:00
|
|
|
|
2018-12-27 03:36:10 +00:00
|
|
|
func TestAddTrackingMetricGroupEmpty(t *testing.T) {
|
|
|
|
ch := make(chan telegraf.Metric, 10)
|
|
|
|
metrics := []telegraf.Metric{}
|
|
|
|
acc := NewAccumulator(&TestMetricMaker{}, ch).WithTracking(1)
|
|
|
|
|
|
|
|
id := acc.AddTrackingMetricGroup(metrics)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case tracking := <-acc.Delivered():
|
|
|
|
require.Equal(t, tracking.ID(), id)
|
|
|
|
default:
|
|
|
|
t.Fatal("empty group should be delivered immediately")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
type TestMetricMaker struct {
|
|
|
|
}
|
2016-07-25 12:09:49 +00:00
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
func (tm *TestMetricMaker) Name() string {
|
|
|
|
return "TestPlugin"
|
|
|
|
}
|
2018-09-28 21:48:20 +00:00
|
|
|
|
2019-08-21 23:49:07 +00:00
|
|
|
func (tm *TestMetricMaker) LogName() string {
|
|
|
|
return tm.Name()
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
func (tm *TestMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric {
|
|
|
|
return metric
|
2016-07-25 12:09:49 +00:00
|
|
|
}
|
2020-02-25 18:40:29 +00:00
|
|
|
|
|
|
|
func (tm *TestMetricMaker) Log() telegraf.Logger {
|
|
|
|
return models.NewLogger("TestPlugin", "test", "")
|
|
|
|
}
|