2016-03-07 22:46:23 +08:00
|
|
|
package agent
|
|
|
|
|
|
|
|
|
|
import (
|
2016-07-25 20:09:49 +08:00
|
|
|
"bytes"
|
2024-02-09 01:32:30 +08:00
|
|
|
"errors"
|
2016-07-25 20:09:49 +08:00
|
|
|
"os"
|
2016-03-07 22:46:23 +08:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2023-10-26 05:04:00 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2016-03-07 22:46:23 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2024-05-11 05:43:43 +08:00
|
|
|
"github.com/influxdata/telegraf/logger"
|
2024-07-01 21:00:13 +08:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-03-07 22:46:23 +08:00
|
|
|
)
|
|
|
|
|
|
2016-09-08 22:22:10 +08:00
|
|
|
func TestAddFields(t *testing.T) {
|
|
|
|
|
metrics := make(chan telegraf.Metric, 10)
|
|
|
|
|
defer close(metrics)
|
|
|
|
|
a := NewAccumulator(&TestMetricMaker{}, metrics)
|
2016-09-01 00:27:37 +08:00
|
|
|
|
2018-03-28 08:30:51 +08:00
|
|
|
tags := map[string]string{"foo": "bar"}
|
2016-09-08 22:22:10 +08:00
|
|
|
fields := map[string]interface{}{
|
|
|
|
|
"usage": float64(99),
|
|
|
|
|
}
|
2018-03-28 08:30:51 +08:00
|
|
|
now := time.Now()
|
|
|
|
|
a.AddCounter("acctest", fields, tags, now)
|
2016-09-01 00:27:37 +08:00
|
|
|
|
2016-09-08 22:22:10 +08:00
|
|
|
testm := <-metrics
|
2016-09-01 00:27:37 +08:00
|
|
|
|
2018-03-28 08:30:51 +08:00
|
|
|
require.Equal(t, "acctest", testm.Name())
|
|
|
|
|
actual, ok := testm.GetField("usage")
|
|
|
|
|
|
|
|
|
|
require.True(t, ok)
|
2024-07-01 21:00:13 +08:00
|
|
|
require.InDelta(t, float64(99), actual, testutil.DefaultDelta)
|
2018-03-28 08:30:51 +08:00
|
|
|
|
|
|
|
|
actual, ok = testm.GetTag("foo")
|
|
|
|
|
require.True(t, ok)
|
|
|
|
|
require.Equal(t, "bar", actual)
|
2016-09-01 00:27:37 +08:00
|
|
|
|
2018-03-28 08:30:51 +08: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-09-01 00:27:37 +08:00
|
|
|
}
|
|
|
|
|
|
2016-09-08 22:22:10 +08:00
|
|
|
func TestAccAddError(t *testing.T) {
|
|
|
|
|
errBuf := bytes.NewBuffer(nil)
|
2024-07-26 23:09:21 +08:00
|
|
|
logger.RedirectLogging(errBuf)
|
|
|
|
|
defer logger.RedirectLogging(os.Stderr)
|
2016-09-01 00:27:37 +08:00
|
|
|
|
2016-09-08 22:22:10 +08:00
|
|
|
metrics := make(chan telegraf.Metric, 10)
|
|
|
|
|
defer close(metrics)
|
|
|
|
|
a := NewAccumulator(&TestMetricMaker{}, metrics)
|
2016-09-01 00:27:37 +08:00
|
|
|
|
2024-02-09 01:32:30 +08:00
|
|
|
a.AddError(errors.New("foo"))
|
|
|
|
|
a.AddError(errors.New("bar"))
|
|
|
|
|
a.AddError(errors.New("baz"))
|
2016-09-01 00:27:37 +08:00
|
|
|
|
2016-09-08 22:22:10 +08:00
|
|
|
errs := bytes.Split(errBuf.Bytes(), []byte{'\n'})
|
|
|
|
|
require.Len(t, errs, 4) // 4 because of trailing newline
|
2023-10-26 05:04:00 +08:00
|
|
|
require.Contains(t, string(errs[0]), "TestPlugin")
|
|
|
|
|
require.Contains(t, string(errs[0]), "foo")
|
|
|
|
|
require.Contains(t, string(errs[1]), "TestPlugin")
|
|
|
|
|
require.Contains(t, string(errs[1]), "bar")
|
|
|
|
|
require.Contains(t, string(errs[2]), "TestPlugin")
|
|
|
|
|
require.Contains(t, string(errs[2]), "baz")
|
2016-09-01 00:27:37 +08:00
|
|
|
}
|
|
|
|
|
|
2018-03-28 08:30:51 +08: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-30 06:40:33 +08:00
|
|
|
precision: time.Second,
|
2018-03-28 08:30:51 +08: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-30 06:40:33 +08:00
|
|
|
precision: time.Microsecond,
|
2018-03-28 08:30:51 +08: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-30 06:40:33 +08:00
|
|
|
a.SetPrecision(tt.precision)
|
2018-03-28 08:30:51 +08: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-13 07:06:27 +08:00
|
|
|
}
|
2016-07-25 20:09:49 +08:00
|
|
|
|
2018-12-27 11:36:10 +08:00
|
|
|
func TestAddTrackingMetricGroupEmpty(t *testing.T) {
|
|
|
|
|
ch := make(chan telegraf.Metric, 10)
|
2024-11-13 15:24:35 +08:00
|
|
|
metrics := make([]telegraf.Metric, 0)
|
2018-12-27 11:36:10 +08:00
|
|
|
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 22:22:10 +08:00
|
|
|
type TestMetricMaker struct {
|
|
|
|
|
}
|
2016-07-25 20:09:49 +08:00
|
|
|
|
2025-01-16 03:58:58 +08:00
|
|
|
func (*TestMetricMaker) Name() string {
|
2016-09-08 22:22:10 +08:00
|
|
|
return "TestPlugin"
|
|
|
|
|
}
|
2018-09-29 05:48:20 +08:00
|
|
|
|
2019-08-22 07:49:07 +08:00
|
|
|
func (tm *TestMetricMaker) LogName() string {
|
|
|
|
|
return tm.Name()
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-16 03:58:58 +08:00
|
|
|
func (*TestMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric {
|
2018-09-29 05:48:20 +08:00
|
|
|
return metric
|
2016-07-25 20:09:49 +08:00
|
|
|
}
|
2020-02-26 02:40:29 +08:00
|
|
|
|
2025-01-16 03:58:58 +08:00
|
|
|
func (*TestMetricMaker) Log() telegraf.Logger {
|
2024-07-26 23:09:21 +08:00
|
|
|
return logger.New("TestPlugin", "test", "")
|
2020-02-26 02:40:29 +08:00
|
|
|
}
|