forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riemann_test.go
212 lines (180 loc) · 5.54 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
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
package riemann
import (
"fmt"
"testing"
"time"
"github.com/amir/raidman"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"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])
}
func TestConnectAndWrite(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
r := &Riemann{
URL: fmt.Sprintf("tcp://%s:5555", testutil.GetLocalHost()),
TTL: 15.0,
Separator: "/",
MeasurementAsAttribute: false,
StringAsState: true,
DescriptionText: "metrics from telegraf",
Tags: []string{"docker"},
}
err := r.Connect()
require.NoError(t, err)
err = r.Write(testutil.MockMetrics())
require.NoError(t, err)
metrics := make([]telegraf.Metric, 0)
metrics = append(metrics, testutil.TestMetric(2))
metrics = append(metrics, testutil.TestMetric(3.456789))
metrics = append(metrics, testutil.TestMetric(uint(0)))
metrics = append(metrics, testutil.TestMetric("ok"))
metrics = append(metrics, testutil.TestMetric("running"))
err = r.Write(metrics)
require.NoError(t, err)
// are there any "docker" tagged events in Riemann?
events, err := r.client.Query(`tagged "docker"`)
require.NoError(t, err)
require.NotZero(t, len(events))
// get Riemann events with state = "running", should be 1 event
events, err = r.client.Query(`state = "running"`)
require.NoError(t, err)
require.Len(t, events, 1)
// is event as expected?
require.Equal(t, []string{"docker", "value1"}, events[0].Tags)
require.Equal(t, "running", events[0].State)
require.Equal(t, "test1/value", events[0].Service)
require.Equal(t, "metrics from telegraf", events[0].Description)
require.Equal(t, map[string]string{"tag1": "value1"}, events[0].Attributes)
}