-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmetrics_test.go
318 lines (268 loc) · 9.12 KB
/
metrics_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
package mackerel
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestPostHostMetricValues(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/tsdb" {
t.Error("request URL should be /api/v0/tsdb but: ", req.URL.Path)
}
if req.Method != "POST" {
t.Error("request method should be POST but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
var values []struct {
HostID string `json:"hostId"`
Name string `json:"name"`
Time float64 `json:"time"`
Value interface{} `json:"value"`
}
err := json.Unmarshal(body, &values)
if err != nil {
t.Fatal("request body should be decoded as json", string(body))
}
if values[0].HostID != "9rxGOHfVF8F" {
t.Error("request sends json including hostId but: ", values[0].HostID)
}
if values[0].Name != "custom.metric.mysql.connections" {
t.Error("request sends json including name but: ", values[0].Name)
}
if values[0].Time != 123456789 {
t.Error("request sends json including time but: ", values[0].Time)
}
if values[0].Value.(float64) != 100 {
t.Error("request sends json including value but: ", values[0].Value)
}
respJSON, _ := json.Marshal(map[string]bool{
"success": true,
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
err := client.PostHostMetricValues([]*HostMetricValue{
{
HostID: "9rxGOHfVF8F",
MetricValue: &MetricValue{
Name: "custom.metric.mysql.connections",
Time: 123456789,
Value: 100,
},
},
})
if err != nil {
t.Error("err should be nil but: ", err)
}
}
func TestPostServiceMetricValues(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/services/My-Service/tsdb" {
t.Error("request URL should be /api/v0/services/My-Service/tsdb but: ", req.URL.Path)
}
if req.Method != "POST" {
t.Error("request method should be POST but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
var values []struct {
Name string `json:"name"`
Time float64 `json:"time"`
Value interface{} `json:"value"`
}
err := json.Unmarshal(body, &values)
if err != nil {
t.Fatal("request body should be decoded as json", string(body))
}
if values[0].Name != "proxy.access_log.latency" {
t.Error("request sends json including name but: ", values[0].Name)
}
if values[0].Time != 123456789 {
t.Error("request sends json including time but: ", values[0].Time)
}
if values[0].Value.(float64) != 500 {
t.Error("request sends json including value but: ", values[0].Value)
}
respJSON, _ := json.Marshal(map[string]bool{
"success": true,
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
err := client.PostServiceMetricValues("My-Service", []*MetricValue{
{
Name: "proxy.access_log.latency",
Time: 123456789,
Value: 500,
},
})
if err != nil {
t.Error("err should be nil but: ", err)
}
}
func TestFetchLatestMetricValues(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/tsdb/latest" {
t.Error("request URL should be /api/v0/tsdb/latest but: ", req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
query := req.URL.Query()
if !reflect.DeepEqual(query["hostId"], []string{"123456ABCD", "654321ABCD"}) {
t.Error("request query 'hostId' param should be ['123456ABCD', '654321ABCD'] but: ", query["hostId"])
}
if !reflect.DeepEqual(query["name"], []string{"mysql.connections.Connections", "mysql.connections.Thread_created"}) {
t.Error("request query 'name' param should be ['mysql.connections.Connections', 'mysql.connections.Thread_created'] but: ", query["name"])
}
respJSON, _ := json.Marshal(map[string]map[string]map[string]*MetricValue{
"tsdbLatest": {
"123456ABCD": {
"mysql.connections.Connections": {
Name: "mysql.connections.Connections",
Time: 123456789,
Value: 200,
},
"mysql.connections.Thread_created": {
Name: "mysql.connections.Thread_created",
Time: 123456789,
Value: 220,
},
},
"654321ABCD": {
"mysql.connections.Connections": {
Name: "mysql.connections.Connections",
Time: 123456789,
Value: 300,
},
"mysql.connections.Thread_created": {
Name: "mysql.connections.Thread_created",
Time: 123456789,
Value: 310,
},
},
},
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
hostIDs := []string{"123456ABCD", "654321ABCD"}
metricNames := []string{"mysql.connections.Connections", "mysql.connections.Thread_created"}
latestMetricValues, err := client.FetchLatestMetricValues(hostIDs, metricNames)
if err != nil {
t.Error("err should be nil but: ", err)
}
if latestMetricValues["123456ABCD"]["mysql.connections.Connections"].Value.(float64) != 200 {
t.Error("123456ABCD host mysql.connections.Connections should be 200 but: ", latestMetricValues["123456ABCD"]["mysql.connections.Connections"].Value)
}
if latestMetricValues["654321ABCD"]["mysql.connections.Connections"].Value.(float64) != 300 {
t.Error("654321ABCD host mysql.connections.Connections should be 300 but: ", latestMetricValues["654321ABCD"]["mysql.connections.Connections"].Value)
}
}
func TestFetchHostMetricValues(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/hosts/123456ABCD/metrics" {
t.Error("request URL should be /api/v0/hosts/123456ABCD/metrics but: ", req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
query := req.URL.Query()
if !reflect.DeepEqual(query["name"], []string{"mysql.connections.Connections"}) {
t.Error("request query 'name' param should be ['mysql.connections.Connections'] but: ", query["name"])
}
respJSON, _ := json.Marshal(map[string][]MetricValue{
"metrics": {
{
Time: 1450000800,
Value: 200,
},
{
Time: 1450000860,
Value: 220,
},
{
Time: 1450000920,
Value: 240,
},
},
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
hostID := "123456ABCD"
metricName := "mysql.connections.Connections"
metricValues, err := client.FetchHostMetricValues(hostID, metricName, 1450000700, 1450001000)
if err != nil {
t.Error("err should be nil but: ", err)
}
if metricValues[0].Value.(float64) != 200 {
t.Error("123456ABCD host mysql.connections.Connections should be 200 but: ", metricValues[0].Value)
}
if metricValues[1].Value.(float64) != 220 {
t.Error("123456ABCD host mysql.connections.Connections should be 220 but: ", metricValues[1].Value)
}
if metricValues[2].Value.(float64) != 240 {
t.Error("123456ABCD host mysql.connections.Connections should be 240 but: ", metricValues[2].Value)
}
}
func TestFetchServiceMetricValues(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/services/123456ABCD/metrics" {
t.Error("request URL should be /api/v0/services/123456ABCD/metrics but: ", req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
query := req.URL.Query()
if !reflect.DeepEqual(query["name"], []string{"custom.access_latency.avg"}) {
t.Error("request query 'name' param should be ['custom.access_latency.avg'] but: ", query["name"])
}
respJSON, _ := json.Marshal(map[string][]MetricValue{
"metrics": {
{
Time: 1450000800,
Value: 0.12,
},
{
Time: 1450000860,
Value: 0.14,
},
{
Time: 1450000920,
Value: 0.16,
},
},
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
serviceName := "123456ABCD"
metricName := "custom.access_latency.avg"
metricValues, err := client.FetchServiceMetricValues(serviceName, metricName, 1450000700, 1450001000)
if err != nil {
t.Error("err should be nil but: ", err)
}
if metricValues[0].Value.(float64) != 0.12 {
t.Error("123456ABCD host custom.access_latency.avg should be 0.12 but: ", metricValues[0].Value)
}
if metricValues[1].Value.(float64) != 0.14 {
t.Error("123456ABCD host custom.access_latency.avg should be 0.14 but: ", metricValues[1].Value)
}
if metricValues[2].Value.(float64) != 0.16 {
t.Error("123456ABCD host custom.access_latency.avg should be 0.16 but: ", metricValues[2].Value)
}
}