/
graphite.go
421 lines (347 loc) · 10 KB
/
graphite.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
package infrastructure
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
httpurl "net/url"
"path"
"time"
)
type Client struct {
URL httpurl.URL
Client *http.Client
}
// Create a new Client from a given URL. The URL is the base adress to
// Graphite, ie. without "/render" suffix etc.
func New(url string) (*Client, error) {
u, err := httpurl.Parse(url)
if err != nil {
return nil, err
}
return &Client{
URL: *u,
Client: &http.Client{},
}, nil
}
type MultiDatapoints []Datapoints
func (m MultiDatapoints) asMap() map[string]Datapoints {
res := make(map[string]Datapoints)
for _, point := range m {
res[point.Target] = point
}
return res
}
// Create a new Client from a given URL. The URL is the base adress to
// Graphite, ie. without "/render" suffix etc.
func NewFromURL(url httpurl.URL) *Client {
return &Client{url, &http.Client{}}
}
type TimeInterval struct {
From time.Time
To time.Time
}
func (t *TimeInterval) Check() error {
if t.From.After(t.To) {
return errors.New("From must be before To.")
}
return nil
}
func graphiteDateFormat(t time.Time) string {
return fmt.Sprintf("%02d:%02d_%d%02d%02d", t.Hour(), t.Minute(), t.Year(), t.Month(), t.Day())
}
type FloatDatapoint struct {
Time time.Time
Value *float64
}
type IntDatapoint struct {
Time time.Time
Value *int64
}
type Datapoints struct {
// Previous error to make single queries nicer to work with.
err error
Target string
points [][]interface{}
}
func (d Datapoints) AsInts() ([]IntDatapoint, error) {
if d.err != nil {
return nil, d.err
}
points := make([]IntDatapoint, 0, len(d.points))
for _, point := range d.points {
jsonUnixTime, ok := point[1].(json.Number)
if !ok {
return nil, errors.New("Unix timestamp not number.")
}
unixTime, err := jsonUnixTime.Int64()
if err != nil {
return nil, errors.New("Unix time not proper number.")
}
var value *int64
if point[0] != nil {
jsonValue, ok := point[0].(json.Number)
if !ok {
return nil, errors.New("Value not a number.")
}
value = new(int64)
*value, err = jsonValue.Int64()
if err != nil {
floatVal, err := jsonValue.Float64()
if err != nil {
return nil, errors.New("Value not proper number.")
}
*value = int64(floatVal)
}
}
points = append(points, IntDatapoint{time.Unix(unixTime, 0), value})
}
return points, nil
}
func (d Datapoints) AsFloats() ([]FloatDatapoint, error) {
if d.err != nil {
return nil, d.err
}
points := make([]FloatDatapoint, 0, len(d.points))
for _, point := range d.points {
jsonUnixTime, ok := point[1].(json.Number)
if !ok {
return nil, errors.New("Unix timestamp not number.")
}
unixTime, err := jsonUnixTime.Int64()
if err != nil {
return nil, errors.New("Unix time not proper number.")
}
var value *float64
if point[0] != nil {
jsonValue, ok := point[0].(json.Number)
if !ok {
return nil, errors.New("Value not a number.")
}
value = new(float64)
*value, err = jsonValue.Float64()
if err != nil {
return nil, errors.New("Value not proper number.")
}
}
points = append(points, FloatDatapoint{time.Unix(unixTime, 0), value})
}
return points, nil
}
func constructQueryPart(qs []string) httpurl.Values {
query := make(httpurl.Values)
for _, q := range qs {
query.Add("target", q)
}
query.Add("format", "json")
return query
}
type FindResultItem struct {
Leaf bool
Text string
Id string
Expandable bool
AllowChildren bool
}
// Used to map isLeaf from integer to
type rawFindResultItem struct {
Leaf int `json:"leaf"`
Text string `json:"text"`
Id string `json:"id"`
Expandable int `json:"expandable"`
AllowChildren int `json:"allowChildren"`
}
type FindOpts struct {
From *time.Time
Until *time.Time
}
func (g *Client) Find(query string, opts *FindOpts) ([]FindResultItem, error) {
// Cloning to be able to modify.
url := g.URL
url.Path = path.Join(url.Path, "/metrics/find")
queryvalues := make(httpurl.Values)
queryvalues.Add("query", query)
if opts != nil && opts.From != nil {
queryvalues.Add("from", graphiteDateFormat(*opts.From))
}
if opts != nil && opts.Until != nil {
queryvalues.Add("until", graphiteDateFormat(*opts.Until))
}
url.RawQuery = queryvalues.Encode()
resp, err := g.Client.Get(url.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
var res []rawFindResultItem
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&res)
if err != nil {
return nil, err
}
realResult := make([]FindResultItem, len(res))
for i, item := range res {
realResult[i].Id = item.Id
realResult[i].Leaf = item.Leaf > 0
realResult[i].Text = item.Text
realResult[i].Expandable = item.Expandable > 0
realResult[i].AllowChildren = item.AllowChildren > 0
}
return realResult, nil
}
// Helper method to make it easier to create an interface for Client.
func (g *Client) QueryInts(q string, interval TimeInterval) ([]IntDatapoint, error) {
return g.Query(q, interval).AsInts()
}
// Helper method to make it easier to create an interface for Client.
func (g *Client) QueryFloats(q string, interval TimeInterval) ([]FloatDatapoint, error) {
return g.Query(q, interval).AsFloats()
}
// Helper method to make it easier to create an interface for Client.
func (g *Client) QueryIntsSince(q string, ago time.Duration) ([]IntDatapoint, error) {
return g.QuerySince(q, ago).AsInts()
}
// Helper method to make it easier to create an interface for Client.
func (g *Client) QueryFloatsSince(q string, ago time.Duration) ([]FloatDatapoint, error) {
return g.QuerySince(q, ago).AsFloats()
}
// Fetches one or multiple Graphite series. Deferring identifying whether the
// result are ints of floats to later. Useful in clients that executes adhoc
// queries.
func (g *Client) QueryMulti(q []string, interval TimeInterval) (MultiDatapoints, error) {
if err := interval.Check(); err != nil {
return nil, err
}
// Cloning to be able to modify.
url := g.URL
url.Path = path.Join(url.Path, "/render")
queryPart := constructQueryPart(q)
queryPart.Add("from", graphiteDateFormat(interval.From))
queryPart.Add("until", graphiteDateFormat(interval.To))
url.RawQuery = queryPart.Encode()
resp, err := g.Client.Get(url.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return parseGraphiteResponse(body)
}
// Fetches one or multiple Graphite series. Deferring identifying whether the
// result are ints of floats to later. Useful in clients that executes adhoc
// queries.
func (g *Client) QueryMultiSince(q []string, ago time.Duration) (MultiDatapoints, error) {
if ago.Nanoseconds() <= 0 {
return nil, errors.New("Duration is expected to be positive.")
}
// Cloning to be able to modify.
url := g.URL
url.Path = path.Join(url.Path, "/render")
queryPart := constructQueryPart(q)
queryPart.Add("from", graphiteSinceString(ago))
url.RawQuery = queryPart.Encode()
resp, err := g.Client.Get(url.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return parseGraphiteResponse(body)
}
// Fetches a Graphite result only expecting one timeseries. Deferring
// identifying whether the result are ints of floats to later. Useful in
// clients that executes adhoc queries.
func (g *Client) Query(q string, interval TimeInterval) Datapoints {
if err := interval.Check(); err != nil {
return Datapoints{err, "", nil}
}
// Cloning to be able to modify.
url := g.URL
url.Path = path.Join(url.Path, "/render")
queryPart := constructQueryPart([]string{q})
queryPart.Add("from", graphiteDateFormat(interval.From))
queryPart.Add("until", graphiteDateFormat(interval.To))
url.RawQuery = queryPart.Encode()
resp, err := g.Client.Get(url.String())
if err != nil {
return Datapoints{err, "", nil}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Datapoints{err, "", nil}
}
points, err := parseGraphiteResponse(body)
return parseSingleGraphiteResponse(points, err)
}
func graphiteSinceString(duration time.Duration) string {
return fmt.Sprintf("%dminutes", -int(duration.Minutes()+0.5))
}
func (g *Client) QuerySince(q string, ago time.Duration) Datapoints {
if ago.Nanoseconds() <= 0 {
return Datapoints{errors.New("Duration is expected to be positive."), "", nil}
}
// Cloning to be able to modify.
url := g.URL
url.Path = path.Join(url.Path, "/render")
queryPart := constructQueryPart([]string{q})
queryPart.Add("from", graphiteSinceString(ago))
url.RawQuery = queryPart.Encode()
resp, err := http.Get(url.String())
if err != nil {
return Datapoints{err, "", nil}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Datapoints{err, "", nil}
}
points, err := parseGraphiteResponse(body)
return parseSingleGraphiteResponse(points, err)
}
func parseSingleGraphiteResponse(dpss []Datapoints, err error) (dps Datapoints) {
if len(dpss) == 0 {
dps.err = errors.New("Unexpected Graphite response. No targets were matched.")
}
if len(dpss) > 1 {
dps.err = errors.New("Unexpected Graphite response. More than one target were returned.")
}
if dps.err != nil {
return
}
dps.points = dpss[0].points
return
}
func parseGraphiteResponse(body []byte) (MultiDatapoints, error) {
var res []target
decoder := json.NewDecoder(bytes.NewBuffer(body))
// Important to distinguish between ints and floats.
decoder.UseNumber()
err := decoder.Decode(&res)
datapoints := make([]Datapoints, len(res))
for i, points := range res {
datapoints[i].Target = points.Target
datapoints[i].points = points.Datapoints
}
return MultiDatapoints(datapoints), err
}
type queryResult []target
type target struct {
Target string `json:"target"`
// Datapoints are either
//
// [[FLOAT, INT], ..., [FLOAT, INT]] (type []intDatapoint).
//
// or
//
// [[FLOAT, FLOAT], ..., [FLOAT, FLOAT]] (type []floatDatapoint).
Datapoints [][]interface{} `json:"datapoints"`
}