forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datadog_test.go
484 lines (456 loc) · 13 KB
/
datadog_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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package statsd
import (
"testing"
"time"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
func TestEventGather(t *testing.T) {
now := time.Now()
type expected struct {
title string
tags map[string]string
fields map[string]interface{}
}
tests := []struct {
name string
message string
hostname string
now time.Time
err bool
expected expected
}{{
name: "basic",
message: "_e{10,9}:test title|test text",
hostname: "default-hostname",
now: now,
err: false,
expected: expected{
title: "test title",
tags: map[string]string{"source": "default-hostname"},
fields: map[string]interface{}{
"priority": priorityNormal,
"alert_type": "info",
"text": "test text",
},
},
},
{
name: "escape some stuff",
message: "_e{10,24}:test title|test\\line1\\nline2\\nline3",
hostname: "default-hostname",
now: now.Add(1),
err: false,
expected: expected{
title: "test title",
tags: map[string]string{"source": "default-hostname"},
fields: map[string]interface{}{
"priority": priorityNormal,
"alert_type": "info",
"text": "test\\line1\nline2\nline3",
},
},
},
{
name: "custom time",
message: "_e{10,9}:test title|test text|d:21",
hostname: "default-hostname",
now: now.Add(2),
err: false,
expected: expected{
title: "test title",
tags: map[string]string{"source": "default-hostname"},
fields: map[string]interface{}{
"priority": priorityNormal,
"alert_type": "info",
"text": "test text",
"ts": int64(21),
},
},
},
}
acc := &testutil.Accumulator{}
s := NewTestStatsd()
require.NoError(t, s.Start(acc))
defer s.Stop()
for i := range tests {
t.Run(tests[i].name, func(t *testing.T) {
err := s.parseEventMessage(tests[i].now, tests[i].message, tests[i].hostname)
if tests[i].err {
require.NotNil(t, err)
} else {
require.Nil(t, err)
}
require.Equal(t, uint64(i+1), acc.NMetrics())
require.Nil(t, err)
require.Equal(t, tests[i].expected.title, acc.Metrics[i].Measurement)
require.Equal(t, tests[i].expected.tags, acc.Metrics[i].Tags)
require.Equal(t, tests[i].expected.fields, acc.Metrics[i].Fields)
})
}
}
// These tests adapted from tests in
// https://github.com/DataDog/datadog-agent/blob/master/pkg/dogstatsd/parser_test.go
// to ensure compatibility with the datadog-agent parser
func TestEvents(t *testing.T) {
now := time.Now()
type args struct {
now time.Time
message string
hostname string
}
type expected struct {
title string
text interface{}
now time.Time
ts interface{}
priority string
source string
alertType interface{}
aggregationKey string
sourceTypeName interface{}
checkTags map[string]string
}
tests := []struct {
name string
args args
expected expected
}{
{
name: "event minimal",
args: args{
now: now,
message: "_e{10,9}:test title|test text",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now,
priority: priorityNormal,
source: "default-hostname",
alertType: eventInfo,
aggregationKey: "",
},
},
{
name: "event multilines text",
args: args{
now: now.Add(1),
message: "_e{10,24}:test title|test\\line1\\nline2\\nline3",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test\\line1\nline2\nline3",
now: now.Add(1),
priority: priorityNormal,
source: "default-hostname",
alertType: eventInfo,
aggregationKey: "",
},
},
{
name: "event pipe in title",
args: args{
now: now.Add(2),
message: "_e{10,24}:test|title|test\\line1\\nline2\\nline3",
hostname: "default-hostname",
},
expected: expected{
title: "test|title",
text: "test\\line1\nline2\nline3",
now: now.Add(2),
priority: priorityNormal,
source: "default-hostname",
alertType: eventInfo,
aggregationKey: "",
},
},
{
name: "event metadata timestamp",
args: args{
now: now.Add(3),
message: "_e{10,9}:test title|test text|d:21",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(3),
priority: priorityNormal,
source: "default-hostname",
alertType: eventInfo,
aggregationKey: "",
ts: int64(21),
},
},
{
name: "event metadata priority",
args: args{
now: now.Add(4),
message: "_e{10,9}:test title|test text|p:low",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(4),
priority: priorityLow,
source: "default-hostname",
alertType: eventInfo,
},
},
{
name: "event metadata hostname",
args: args{
now: now.Add(5),
message: "_e{10,9}:test title|test text|h:localhost",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(5),
priority: priorityNormal,
source: "localhost",
alertType: eventInfo,
},
},
{
name: "event metadata hostname in tag",
args: args{
now: now.Add(6),
message: "_e{10,9}:test title|test text|#host:localhost",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(6),
priority: priorityNormal,
source: "localhost",
alertType: eventInfo,
},
},
{
name: "event metadata empty host tag",
args: args{
now: now.Add(7),
message: "_e{10,9}:test title|test text|#host:,other:tag",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(7),
priority: priorityNormal,
source: "true",
alertType: eventInfo,
checkTags: map[string]string{"other": "tag", "source": "true"},
},
},
{
name: "event metadata alert type",
args: args{
now: now.Add(8),
message: "_e{10,9}:test title|test text|t:warning",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(8),
priority: priorityNormal,
source: "default-hostname",
alertType: eventWarning,
},
},
{
name: "event metadata aggregation key",
args: args{
now: now.Add(9),
message: "_e{10,9}:test title|test text|k:some aggregation key",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(9),
priority: priorityNormal,
source: "default-hostname",
alertType: eventInfo,
aggregationKey: "some aggregation key",
},
},
{
name: "event metadata aggregation key",
args: args{
now: now.Add(10),
message: "_e{10,9}:test title|test text|k:some aggregation key",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(10),
priority: priorityNormal,
source: "default-hostname",
alertType: eventInfo,
aggregationKey: "some aggregation key",
},
},
{
name: "event metadata source type",
args: args{
now: now.Add(11),
message: "_e{10,9}:test title|test text|s:this is the source",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(11),
priority: priorityNormal,
source: "default-hostname",
sourceTypeName: "this is the source",
alertType: eventInfo,
},
},
{
name: "event metadata source type",
args: args{
now: now.Add(11),
message: "_e{10,9}:test title|test text|s:this is the source",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(11),
priority: priorityNormal,
source: "default-hostname",
sourceTypeName: "this is the source",
alertType: eventInfo,
},
},
{
name: "event metadata source tags",
args: args{
now: now.Add(11),
message: "_e{10,9}:test title|test text|#tag1,tag2:test",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(11),
priority: priorityNormal,
source: "default-hostname",
alertType: eventInfo,
checkTags: map[string]string{"tag1": "true", "tag2": "test", "source": "default-hostname"},
},
},
{
name: "event metadata multiple",
args: args{
now: now.Add(11),
message: "_e{10,9}:test title|test text|t:warning|d:12345|p:low|h:some.host|k:aggKey|s:source test|#tag1,tag2:test",
hostname: "default-hostname",
},
expected: expected{
title: "test title",
text: "test text",
now: now.Add(11),
priority: priorityLow,
source: "some.host",
ts: int64(12345),
alertType: eventWarning,
aggregationKey: "aggKey",
sourceTypeName: "source test",
checkTags: map[string]string{"aggregation_key": "aggKey", "tag1": "true", "tag2": "test", "source": "some.host"},
},
},
}
s := NewTestStatsd()
acc := &testutil.Accumulator{}
require.NoError(t, s.Start(acc))
defer s.Stop()
for i := range tests {
t.Run(tests[i].name, func(t *testing.T) {
acc.ClearMetrics()
err := s.parseEventMessage(tests[i].args.now, tests[i].args.message, tests[i].args.hostname)
require.Nil(t, err)
m := acc.Metrics[0]
require.Equal(t, tests[i].expected.title, m.Measurement)
require.Equal(t, tests[i].expected.text, m.Fields["text"])
require.Equal(t, tests[i].expected.now, m.Time)
require.Equal(t, tests[i].expected.ts, m.Fields["ts"])
require.Equal(t, tests[i].expected.priority, m.Fields["priority"])
require.Equal(t, tests[i].expected.source, m.Tags["source"])
require.Equal(t, tests[i].expected.alertType, m.Fields["alert_type"])
require.Equal(t, tests[i].expected.aggregationKey, m.Tags["aggregation_key"])
require.Equal(t, tests[i].expected.sourceTypeName, m.Fields["source_type_name"])
if tests[i].expected.checkTags != nil {
require.Equal(t, tests[i].expected.checkTags, m.Tags)
}
})
}
}
func TestEventError(t *testing.T) {
now := time.Now()
s := NewTestStatsd()
acc := &testutil.Accumulator{}
require.NoError(t, s.Start(acc))
defer s.Stop()
// missing length header
err := s.parseEventMessage(now, "_e:title|text", "default-hostname")
require.Error(t, err)
// greater length than packet
err = s.parseEventMessage(now, "_e{10,10}:title|text", "default-hostname")
require.Error(t, err)
// zero length
err = s.parseEventMessage(now, "_e{0,0}:a|a", "default-hostname")
require.Error(t, err)
// missing title or text length
err = s.parseEventMessage(now, "_e{5555:title|text", "default-hostname")
require.Error(t, err)
// missing wrong len format
err = s.parseEventMessage(now, "_e{a,1}:title|text", "default-hostname")
require.Error(t, err)
err = s.parseEventMessage(now, "_e{1,a}:title|text", "default-hostname")
require.Error(t, err)
// missing title or text length
err = s.parseEventMessage(now, "_e{5,}:title|text", "default-hostname")
require.Error(t, err)
err = s.parseEventMessage(now, "_e{100,:title|text", "default-hostname")
require.Error(t, err)
err = s.parseEventMessage(now, "_e,100:title|text", "default-hostname")
require.Error(t, err)
err = s.parseEventMessage(now, "_e{,4}:title|text", "default-hostname")
require.Error(t, err)
err = s.parseEventMessage(now, "_e{}:title|text", "default-hostname")
require.Error(t, err)
err = s.parseEventMessage(now, "_e{,}:title|text", "default-hostname")
require.Error(t, err)
// not enough information
err = s.parseEventMessage(now, "_e|text", "default-hostname")
require.Error(t, err)
err = s.parseEventMessage(now, "_e:|text", "default-hostname")
require.Error(t, err)
// invalid timestamp
err = s.parseEventMessage(now, "_e{5,4}:title|text|d:abc", "default-hostname")
require.NoError(t, err)
// invalid priority
err = s.parseEventMessage(now, "_e{5,4}:title|text|p:urgent", "default-hostname")
require.NoError(t, err)
// invalid priority
err = s.parseEventMessage(now, "_e{5,4}:title|text|p:urgent", "default-hostname")
require.NoError(t, err)
// invalid alert type
err = s.parseEventMessage(now, "_e{5,4}:title|text|t:test", "default-hostname")
require.NoError(t, err)
// unknown metadata
err = s.parseEventMessage(now, "_e{5,4}:title|text|x:1234", "default-hostname")
require.Error(t, err)
}