forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_test.go
114 lines (98 loc) · 2.58 KB
/
internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package internal
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/selfstat"
"github.com/influxdata/telegraf/testutil"
)
func TestSelfPlugin(t *testing.T) {
s := Self{
CollectMemstats: true,
}
acc := &testutil.Accumulator{}
require.NoError(t, s.Gather(acc))
require.True(t, acc.HasMeasurement("internal_memstats"))
// test that a registered stat is incremented
stat := selfstat.Register("mytest", "test", map[string]string{"test": "foo"})
stat.Incr(1)
stat.Incr(2)
require.NoError(t, s.Gather(acc))
acc.AssertContainsTaggedFields(t, "internal_mytest",
map[string]interface{}{
"test": int64(3),
},
map[string]string{
"test": "foo",
"version": "unknown",
},
)
acc.ClearMetrics()
// test that a registered stat is set properly
stat.Set(101)
require.NoError(t, s.Gather(acc))
acc.AssertContainsTaggedFields(t, "internal_mytest",
map[string]interface{}{
"test": int64(101),
},
map[string]string{
"test": "foo",
"version": "unknown",
},
)
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)
require.NoError(t, s.Gather(acc))
acc.AssertContainsTaggedFields(t, "internal_mytest",
map[string]interface{}{
"test": int64(101),
"test_ns": int64(150),
},
map[string]string{
"test": "foo",
"version": "unknown",
},
)
}
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, "internal_gostats", metric.Measurement)
require.Len(t, 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))
}
}
}