2016-11-07 16:34:46 +08:00
|
|
|
package internal
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-29 23:20:59 +08:00
|
|
|
"fmt"
|
2016-11-07 16:34:46 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/selfstat"
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
|
2021-04-23 05:08:03 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-11-07 16:34:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestSelfPlugin(t *testing.T) {
|
2023-08-29 23:20:59 +08:00
|
|
|
s := Self{
|
|
|
|
|
CollectMemstats: true,
|
|
|
|
|
}
|
2016-11-07 16:34:46 +08:00
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
|
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, s.Gather(acc))
|
|
|
|
|
require.True(t, acc.HasMeasurement("internal_memstats"))
|
2016-11-07 16:34:46 +08:00
|
|
|
|
|
|
|
|
// test that a registered stat is incremented
|
|
|
|
|
stat := selfstat.Register("mytest", "test", map[string]string{"test": "foo"})
|
|
|
|
|
stat.Incr(1)
|
|
|
|
|
stat.Incr(2)
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, s.Gather(acc))
|
2022-08-18 03:08:31 +08:00
|
|
|
|
2016-11-07 16:34:46 +08:00
|
|
|
acc.AssertContainsTaggedFields(t, "internal_mytest",
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"test": int64(3),
|
|
|
|
|
},
|
|
|
|
|
map[string]string{
|
2019-08-09 01:51:03 +08:00
|
|
|
"test": "foo",
|
2022-08-18 03:08:31 +08:00
|
|
|
"version": "unknown",
|
2016-11-07 16:34:46 +08:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
acc.ClearMetrics()
|
|
|
|
|
|
|
|
|
|
// test that a registered stat is set properly
|
|
|
|
|
stat.Set(101)
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, s.Gather(acc))
|
2016-11-07 16:34:46 +08:00
|
|
|
acc.AssertContainsTaggedFields(t, "internal_mytest",
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"test": int64(101),
|
|
|
|
|
},
|
|
|
|
|
map[string]string{
|
2019-08-09 01:51:03 +08:00
|
|
|
"test": "foo",
|
2022-08-18 03:08:31 +08:00
|
|
|
"version": "unknown",
|
2016-11-07 16:34:46 +08:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
acc.ClearMetrics()
|
|
|
|
|
|
|
|
|
|
// test that regular and timing stats can share the same measurement, and
|
|
|
|
|
// that timings are set properly.
|
|
|
|
|
timing := selfstat.RegisterTiming("mytest", "test_ns", map[string]string{"test": "foo"})
|
|
|
|
|
timing.Incr(100)
|
|
|
|
|
timing.Incr(200)
|
2021-04-23 05:08:03 +08:00
|
|
|
require.NoError(t, s.Gather(acc))
|
2016-11-07 16:34:46 +08:00
|
|
|
acc.AssertContainsTaggedFields(t, "internal_mytest",
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"test": int64(101),
|
|
|
|
|
"test_ns": int64(150),
|
|
|
|
|
},
|
|
|
|
|
map[string]string{
|
2019-08-09 01:51:03 +08:00
|
|
|
"test": "foo",
|
2022-08-18 03:08:31 +08:00
|
|
|
"version": "unknown",
|
2016-11-07 16:34:46 +08:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|
2023-08-29 23:20:59 +08:00
|
|
|
|
|
|
|
|
func TestNoMemStat(t *testing.T) {
|
|
|
|
|
s := Self{
|
|
|
|
|
CollectMemstats: false,
|
|
|
|
|
CollectGostats: false,
|
|
|
|
|
}
|
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
|
|
|
|
|
|
require.NoError(t, s.Gather(acc))
|
|
|
|
|
require.False(t, acc.HasMeasurement("internal_memstats"))
|
|
|
|
|
require.False(t, acc.HasMeasurement("internal_gostats"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGostats(t *testing.T) {
|
|
|
|
|
s := Self{
|
|
|
|
|
CollectMemstats: false,
|
|
|
|
|
CollectGostats: true,
|
|
|
|
|
}
|
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
|
|
|
|
|
|
require.NoError(t, s.Gather(acc))
|
|
|
|
|
require.False(t, acc.HasMeasurement("internal_memstats"))
|
|
|
|
|
require.True(t, acc.HasMeasurement("internal_gostats"))
|
|
|
|
|
|
|
|
|
|
var metric *testutil.Metric
|
|
|
|
|
for _, m := range acc.Metrics {
|
|
|
|
|
if m.Measurement == "internal_gostats" {
|
|
|
|
|
metric = m
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
require.NotNil(t, metric)
|
|
|
|
|
require.Equal(t, metric.Measurement, "internal_gostats")
|
|
|
|
|
require.Equal(t, len(metric.Tags), 1)
|
|
|
|
|
require.Contains(t, metric.Tags, "go_version")
|
|
|
|
|
|
|
|
|
|
for name, value := range metric.Fields {
|
|
|
|
|
switch value.(type) {
|
|
|
|
|
case int64, uint64, float64:
|
|
|
|
|
default:
|
|
|
|
|
require.True(t, false, fmt.Sprintf("field %s is of non-numeric type %T\n", name, value))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|