forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavefront_test.go
291 lines (257 loc) · 7.41 KB
/
wavefront_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package wavefront
import (
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"reflect"
"strings"
"testing"
"time"
)
// default config used by Tests
func defaultWavefront() *Wavefront {
return &Wavefront{
Host: "localhost",
Port: 2878,
Prefix: "testWF.",
SimpleFields: false,
MetricSeparator: ".",
ConvertPaths: true,
ConvertBool: true,
UseRegex: false,
}
}
func TestBuildMetrics(t *testing.T) {
w := defaultWavefront()
w.Prefix = "testthis."
pathReplacer = strings.NewReplacer("_", w.MetricSeparator)
testMetric1, _ := metric.New(
"test.simple.metric",
map[string]string{"tag1": "value1", "host": "testHost"},
map[string]interface{}{"value": 123},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
)
var timestamp int64 = 1257894000
var metricTests = []struct {
metric telegraf.Metric
metricPoints []MetricPoint
}{
{
testutil.TestMetric(float64(1), "testing_just*a%metric:float", "metric2"),
[]MetricPoint{
{Metric: w.Prefix + "testing.just-a-metric-float", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
{Metric: w.Prefix + "testing.metric2", Value: 1, Timestamp: timestamp, Tags: map[string]string{"tag1": "value1"}},
},
},
{
testMetric1,
[]MetricPoint{{Metric: w.Prefix + "test.simple.metric", Value: 123, Timestamp: timestamp, Source: "testHost", Tags: map[string]string{"tag1": "value1"}}},
},
}
for _, mt := range metricTests {
ml := buildMetrics(mt.metric, w)
for i, line := range ml {
if mt.metricPoints[i].Metric != line.Metric || mt.metricPoints[i].Value != line.Value {
t.Errorf("\nexpected\t%+v %+v\nreceived\t%+v %+v\n", mt.metricPoints[i].Metric, mt.metricPoints[i].Value, line.Metric, line.Value)
}
}
}
}
func TestBuildMetricsWithSimpleFields(t *testing.T) {
w := defaultWavefront()
w.Prefix = "testthis."
w.SimpleFields = true
pathReplacer = strings.NewReplacer("_", w.MetricSeparator)
testMetric1, _ := metric.New(
"test.simple.metric",
map[string]string{"tag1": "value1"},
map[string]interface{}{"value": 123},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
)
var metricTests = []struct {
metric telegraf.Metric
metricLines []MetricPoint
}{
{
testutil.TestMetric(float64(1), "testing_just*a%metric:float"),
[]MetricPoint{{Metric: w.Prefix + "testing.just-a-metric-float.value", Value: 1}},
},
{
testMetric1,
[]MetricPoint{{Metric: w.Prefix + "test.simple.metric.value", Value: 123}},
},
}
for _, mt := range metricTests {
ml := buildMetrics(mt.metric, w)
for i, line := range ml {
if mt.metricLines[i].Metric != line.Metric || mt.metricLines[i].Value != line.Value {
t.Errorf("\nexpected\t%+v %+v\nreceived\t%+v %+v\n", mt.metricLines[i].Metric, mt.metricLines[i].Value, line.Metric, line.Value)
}
}
}
}
func TestBuildTags(t *testing.T) {
w := defaultWavefront()
var tagtests = []struct {
ptIn map[string]string
outSource string
outTags map[string]string
}{
{
map[string]string{},
"",
map[string]string{},
},
{
map[string]string{"one": "two", "three": "four", "host": "testHost"},
"testHost",
map[string]string{"one": "two", "three": "four"},
},
{
map[string]string{"aaa": "bbb", "host": "testHost"},
"testHost",
map[string]string{"aaa": "bbb"},
},
{
map[string]string{"bbb": "789", "aaa": "123", "host": "testHost"},
"testHost",
map[string]string{"aaa": "123", "bbb": "789"},
},
{
map[string]string{"host": "aaa", "dc": "bbb"},
"aaa",
map[string]string{"dc": "bbb"},
},
{
map[string]string{"host": "aaa", "dc": "a*$a\\abbb\"som/et|hing else", "bad#k%e/y that*sho\\uld work": "value1"},
"aaa",
map[string]string{"dc": "a-$a\\abbb\"som/et|hing else", "bad-k-e-y-that-sho-uld-work": "value1"},
},
}
for _, tt := range tagtests {
source, tags := buildTags(tt.ptIn, w)
if source != tt.outSource {
t.Errorf("\nexpected\t%+v\nreceived\t%+v\n", tt.outSource, source)
}
if !reflect.DeepEqual(tags, tt.outTags) {
t.Errorf("\nexpected\t%+v\nreceived\t%+v\n", tt.outTags, tags)
}
}
}
func TestBuildTagsWithSource(t *testing.T) {
w := defaultWavefront()
w.SourceOverride = []string{"snmp_host", "hostagent"}
var tagtests = []struct {
ptIn map[string]string
outSource string
outTags map[string]string
}{
{
map[string]string{"host": "realHost"},
"realHost",
map[string]string{},
},
{
map[string]string{"tag1": "value1", "host": "realHost"},
"realHost",
map[string]string{"tag1": "value1"},
},
{
map[string]string{"snmp_host": "realHost", "host": "origHost"},
"realHost",
map[string]string{"telegraf_host": "origHost"},
},
{
map[string]string{"hostagent": "realHost", "host": "origHost"},
"realHost",
map[string]string{"telegraf_host": "origHost"},
},
{
map[string]string{"hostagent": "abc", "snmp_host": "realHost", "host": "origHost"},
"realHost",
map[string]string{"hostagent": "abc", "telegraf_host": "origHost"},
},
{
map[string]string{"something": "abc", "host": "r*@l\"Ho/st"},
"r-@l\"Ho/st",
map[string]string{"something": "abc"},
},
}
for _, tt := range tagtests {
source, tags := buildTags(tt.ptIn, w)
if source != tt.outSource {
t.Errorf("\nexpected\t%+v\nreceived\t%+v\n", tt.outSource, source)
}
if !reflect.DeepEqual(tags, tt.outTags) {
t.Errorf("\nexpected\t%+v\nreceived\t%+v\n", tt.outTags, tags)
}
}
}
func TestBuildValue(t *testing.T) {
w := defaultWavefront()
var valuetests = []struct {
value interface{}
name string
out float64
isErr bool
}{
{value: int64(123), out: 123},
{value: uint64(456), out: 456},
{value: float64(789), out: 789},
{value: true, out: 1},
{value: false, out: 0},
{value: "bad", out: 0, isErr: true},
}
for _, vt := range valuetests {
value, err := buildValue(vt.value, vt.name, w)
if vt.isErr && err == nil {
t.Errorf("\nexpected error with\t%+v\nreceived\t%+v\n", vt.out, value)
} else if value != vt.out {
t.Errorf("\nexpected\t%+v\nreceived\t%+v\n", vt.out, value)
}
}
}
func TestBuildValueString(t *testing.T) {
w := defaultWavefront()
w.StringToNumber = map[string][]map[string]float64{
"test1": {{"green": 1, "red": 10}},
"test2": {{"active": 1, "hidden": 2}},
}
var valuetests = []struct {
value interface{}
name string
out float64
isErr bool
}{
{value: int64(123), name: "", out: 123},
{value: "green", name: "test1", out: 1},
{value: "red", name: "test1", out: 10},
{value: "hidden", name: "test2", out: 2},
{value: "bad", name: "test1", out: 0, isErr: true},
}
for _, vt := range valuetests {
value, err := buildValue(vt.value, vt.name, w)
if vt.isErr && err == nil {
t.Errorf("\nexpected error with\t%+v\nreceived\t%+v\n", vt.out, value)
} else if value != vt.out {
t.Errorf("\nexpected\t%+v\nreceived\t%+v\n", vt.out, value)
}
}
}
// Benchmarks to test performance of string replacement via Regex and Replacer
var testString = "this_is*my!test/string\\for=replacement"
func BenchmarkReplaceAllString(b *testing.B) {
for n := 0; n < b.N; n++ {
sanitizedRegex.ReplaceAllString(testString, "-")
}
}
func BenchmarkReplaceAllLiteralString(b *testing.B) {
for n := 0; n < b.N; n++ {
sanitizedRegex.ReplaceAllLiteralString(testString, "-")
}
}
func BenchmarkReplacer(b *testing.B) {
for n := 0; n < b.N; n++ {
sanitizedChars.Replace(testString)
}
}