forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riemann_test.go
161 lines (137 loc) · 3.91 KB
/
riemann_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
package riemann
import (
"testing"
"time"
"github.com/amir/raidman"
"github.com/influxdata/telegraf/metric"
"github.com/stretchr/testify/require"
)
func TestAttributes(t *testing.T) {
tags := map[string]string{"tag1": "value1", "tag2": "value2"}
r := &Riemann{}
require.Equal(t,
map[string]string{"tag1": "value1", "tag2": "value2"},
r.attributes("test", tags))
// enable measurement as attribute, should now be included
r.MeasurementAsAttribute = true
require.Equal(t,
map[string]string{"tag1": "value1", "tag2": "value2", "measurement": "test"},
r.attributes("test", tags))
}
func TestService(t *testing.T) {
r := &Riemann{
Separator: "/",
}
require.Equal(t, "test/value", r.service("test", "value"))
// enable measurement as attribute, should not be part of service name anymore
r.MeasurementAsAttribute = true
require.Equal(t, "value", r.service("test", "value"))
}
func TestTags(t *testing.T) {
tags := map[string]string{"tag1": "value1", "tag2": "value2"}
// all tag values plus additional tag should be present
r := &Riemann{
Tags: []string{"test"},
}
require.Equal(t,
[]string{"test", "value1", "value2"},
r.tags(tags))
// only tag2 value plus additional tag should be present
r.TagKeys = []string{"tag2"}
require.Equal(t,
[]string{"test", "value2"},
r.tags(tags))
// only tag1 value should be present
r.Tags = nil
r.TagKeys = []string{"tag1"}
require.Equal(t,
[]string{"value1"},
r.tags(tags))
}
func TestMetricEvents(t *testing.T) {
r := &Riemann{
TTL: 20.0,
Separator: "/",
MeasurementAsAttribute: false,
DescriptionText: "metrics from telegraf",
Tags: []string{"telegraf"},
}
// build a single event
m, _ := metric.New(
"test1",
map[string]string{"tag1": "value1", "host": "abc123"},
map[string]interface{}{"value": 5.6},
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
)
events := r.buildRiemannEvents(m)
require.Len(t, events, 1)
// is event as expected?
expectedEvent := &raidman.Event{
Ttl: 20.0,
Time: 1257894000,
Tags: []string{"telegraf", "value1"},
Host: "abc123",
State: "",
Service: "test1/value",
Metric: 5.6,
Description: "metrics from telegraf",
Attributes: map[string]string{"tag1": "value1"},
}
require.Equal(t, expectedEvent, events[0])
// build 2 events
m, _ = metric.New(
"test2",
map[string]string{"host": "xyz987"},
map[string]interface{}{"point": 1},
time.Date(2012, time.November, 2, 3, 0, 0, 0, time.UTC),
)
events = append(events, r.buildRiemannEvents(m)...)
require.Len(t, events, 2)
// first event should still be the same
require.Equal(t, expectedEvent, events[0])
// second event
expectedEvent = &raidman.Event{
Ttl: 20.0,
Time: 1351825200,
Tags: []string{"telegraf"},
Host: "xyz987",
State: "",
Service: "test2/point",
Metric: int64(1),
Description: "metrics from telegraf",
Attributes: map[string]string{},
}
require.Equal(t, expectedEvent, events[1])
}
func TestStateEvents(t *testing.T) {
r := &Riemann{
MeasurementAsAttribute: true,
}
// string metrics will be skipped unless explicitly enabled
m, _ := metric.New(
"test",
map[string]string{"host": "host"},
map[string]interface{}{"value": "running"},
time.Date(2015, time.November, 9, 22, 0, 0, 0, time.UTC),
)
events := r.buildRiemannEvents(m)
// no event should be present
require.Len(t, events, 0)
// enable string metrics as event states
r.StringAsState = true
events = r.buildRiemannEvents(m)
require.Len(t, events, 1)
// is event as expected?
expectedEvent := &raidman.Event{
Ttl: 0,
Time: 1447106400,
Tags: nil,
Host: "host",
State: "running",
Service: "value",
Metric: nil,
Description: "",
Attributes: map[string]string{"measurement": "test"},
}
require.Equal(t, expectedEvent, events[0])
}