2016-09-08 14:22:10 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-02-25 18:40:29 +00:00
|
|
|
"github.com/influxdata/telegraf/selfstat"
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2018-03-28 00:30:51 +00:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-09-08 14:22:10 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-07-11 22:54:38 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
|
|
|
|
2018-12-18 22:20:43 +00:00
|
|
|
func TestMakeMetricFilterAfterApplyingGlobalTags(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Filter: Filter{
|
|
|
|
TagInclude: []string{"b"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, ri.Config.Filter.Compile())
|
|
|
|
ri.SetDefaultTags(map[string]string{"a": "x", "b": "y"})
|
|
|
|
|
|
|
|
m, err := metric.New("cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42,
|
|
|
|
},
|
|
|
|
now)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
actual := ri.MakeMetric(m)
|
|
|
|
|
|
|
|
expected, err := metric.New("cpu",
|
|
|
|
map[string]string{
|
|
|
|
"b": "y",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42,
|
|
|
|
},
|
|
|
|
now)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testutil.RequireMetricEqual(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
func TestMakeMetricNoFields(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("RITest",
|
2016-09-08 14:22:10 +00:00
|
|
|
map[string]string{},
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]interface{}{},
|
2016-09-08 14:22:10 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
|
|
|
m = ri.MakeMetric(m)
|
|
|
|
require.NoError(t, err)
|
2016-09-08 14:22:10 +00:00
|
|
|
assert.Nil(t, m)
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:12:36 +00:00
|
|
|
// nil fields should get dropped
|
|
|
|
func TestMakeMetricNilFields(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
2016-10-07 17:12:36 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("RITest",
|
|
|
|
map[string]string{},
|
2016-10-07 17:12:36 +00:00
|
|
|
map[string]interface{}{
|
2018-09-28 21:48:20 +00:00
|
|
|
"value": int64(101),
|
2016-10-07 17:12:36 +00:00
|
|
|
"nil": nil,
|
|
|
|
},
|
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
|
|
|
require.NoError(t, err)
|
|
|
|
m = ri.MakeMetric(m)
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("RITest",
|
2016-09-08 14:22:10 +00:00
|
|
|
map[string]string{},
|
2018-03-28 00:30:51 +00:00
|
|
|
map[string]interface{}{
|
|
|
|
"value": int(101),
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
now,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-03-28 00:30:51 +00:00
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricWithPluginTags(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
Tags: map[string]string{
|
|
|
|
"foo": "bar",
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
2016-11-07 08:34:46 +00:00
|
|
|
})
|
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
m := testutil.MustMetric("RITest",
|
2018-09-28 21:48:20 +00:00
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
|
|
|
m = ri.MakeMetric(m)
|
2018-03-28 00:30:51 +00:00
|
|
|
|
|
|
|
expected, err := metric.New("RITest",
|
|
|
|
map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 101,
|
|
|
|
},
|
|
|
|
now,
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricFilteredOut(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
Tags: map[string]string{
|
|
|
|
"foo": "bar",
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
2016-11-07 08:34:46 +00:00
|
|
|
Filter: Filter{NamePass: []string{"foobar"}},
|
|
|
|
})
|
|
|
|
|
2016-09-08 14:22:10 +00:00
|
|
|
assert.NoError(t, ri.Config.Filter.Compile())
|
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("RITest",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": int64(101),
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
|
|
|
m = ri.MakeMetric(m)
|
|
|
|
require.NoError(t, err)
|
2016-09-08 14:22:10 +00:00
|
|
|
assert.Nil(t, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricWithDaemonTags(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
ri.SetDefaultTags(map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
})
|
2016-11-07 08:34:46 +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),
|
|
|
|
},
|
2016-09-08 14:22:10 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
|
|
|
m = ri.MakeMetric(m)
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("RITest",
|
|
|
|
map[string]string{
|
|
|
|
"foo": "bar",
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
|
|
|
map[string]interface{}{
|
2018-03-28 00:30:51 +00:00
|
|
|
"value": 101,
|
2016-09-08 14:22:10 +00:00
|
|
|
},
|
|
|
|
now,
|
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricNameOverride(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
NameOverride: "foobar",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("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-09-08 14:22:10 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
|
|
|
require.NoError(t, err)
|
|
|
|
m = ri.MakeMetric(m)
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("foobar",
|
|
|
|
nil,
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 101,
|
|
|
|
},
|
|
|
|
now,
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricNamePrefix(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
MeasurementPrefix: "foobar_",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("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-09-08 14:22:10 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
|
|
|
require.NoError(t, err)
|
|
|
|
m = ri.MakeMetric(m)
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("foobar_RITest",
|
|
|
|
nil,
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 101,
|
|
|
|
},
|
|
|
|
now,
|
2016-09-08 14:22:10 +00:00
|
|
|
)
|
2018-03-28 00:30:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2016-09-08 14:22:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeMetricNameSuffix(t *testing.T) {
|
|
|
|
now := time.Now()
|
2016-11-07 08:34:46 +00:00
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestRunningInput",
|
|
|
|
MeasurementSuffix: "_foobar",
|
|
|
|
})
|
2016-09-08 14:22:10 +00:00
|
|
|
|
2018-09-28 21:48:20 +00:00
|
|
|
m, err := metric.New("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-09-08 14:22:10 +00:00
|
|
|
now,
|
2018-09-28 21:48:20 +00:00
|
|
|
telegraf.Untyped)
|
|
|
|
require.NoError(t, err)
|
|
|
|
m = ri.MakeMetric(m)
|
2018-03-28 00:30:51 +00:00
|
|
|
expected, err := metric.New("RITest_foobar",
|
|
|
|
nil,
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 101,
|
2017-07-11 22:54:38 +00:00
|
|
|
},
|
2018-03-28 00:30:51 +00:00
|
|
|
now,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, m)
|
2017-07-11 22:54:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 18:40:29 +00:00
|
|
|
func TestMetricErrorCounters(t *testing.T) {
|
|
|
|
ri := NewRunningInput(&testInput{}, &InputConfig{
|
|
|
|
Name: "TestMetricErrorCounters",
|
|
|
|
})
|
|
|
|
|
|
|
|
getGatherErrors := func() int64 {
|
|
|
|
for _, r := range selfstat.Metrics() {
|
|
|
|
tag, hasTag := r.GetTag("input")
|
|
|
|
if r.Name() == "internal_gather" && hasTag && tag == "TestMetricErrorCounters" {
|
|
|
|
errCount, ok := r.GetField("errors")
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("Expected error field")
|
|
|
|
}
|
|
|
|
return errCount.(int64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
before := getGatherErrors()
|
|
|
|
|
|
|
|
ri.Log().Error("Oh no")
|
|
|
|
|
|
|
|
after := getGatherErrors()
|
|
|
|
|
|
|
|
require.Greater(t, after, before)
|
|
|
|
require.GreaterOrEqual(t, int64(1), GlobalGatherErrors.Get())
|
|
|
|
}
|
|
|
|
|
2016-11-07 08:34:46 +00:00
|
|
|
type testInput struct{}
|
|
|
|
|
|
|
|
func (t *testInput) Description() string { return "" }
|
|
|
|
func (t *testInput) SampleConfig() string { return "" }
|
|
|
|
func (t *testInput) Gather(acc telegraf.Accumulator) error { return nil }
|