-
Notifications
You must be signed in to change notification settings - Fork 3
/
graphite.go
196 lines (166 loc) · 5.3 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
package gosnowth
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
)
// GraphiteOptions values contain optional parameters to be passed to the
// IRONdb graphite API call operations.
type GraphiteOptions struct {
Limit int64 `json:"limit"`
}
// GraphiteMetric values are used to describe metrics in graphite format.
type GraphiteMetric struct {
Leaf bool `json:"leaf"`
Name string `json:"name"`
Data map[string]string `json:"leaf_data,omitempty"`
}
// GraphiteLookup values are used to provide information needed to retrieve
// graphite datapoints.
type GraphiteLookup struct {
Start int64 `json:"start"`
End int64 `json:"end"`
Names []string `json:"names"`
}
// GraphiteDatapoints values are used to represent series of graphite
// datapoints.
type GraphiteDatapoints struct {
From int64 `json:"from"`
To int64 `json:"to"`
Step int64 `json:"step"`
Series map[string][]*float64 `json:"series"`
}
// GraphiteFindMetrics retrieves metrics that are associated with the provided
// graphite metric search query.
func (sc *SnowthClient) GraphiteFindMetrics(accountID int64,
prefix, query string, options *GraphiteOptions,
nodes ...*SnowthNode,
) ([]GraphiteMetric, error) {
return sc.GraphiteFindMetricsContext(context.Background(), accountID,
prefix, query, options, nodes...)
}
// GraphiteFindMetricsContext is the context aware version of
// GraphiteFindMetrics.
func (sc *SnowthClient) GraphiteFindMetricsContext(ctx context.Context,
accountID int64, prefix, query string, options *GraphiteOptions,
nodes ...*SnowthNode,
) ([]GraphiteMetric, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode()
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
u := fmt.Sprintf("%s?query=%s",
fmt.Sprintf("/graphite/%d/%s/metrics/find",
accountID, url.QueryEscape(prefix)), url.QueryEscape(query))
hdrs := http.Header{}
if options != nil && options.Limit != 0 {
hdrs.Set("X-Snowth-Advisory-Limit",
strconv.FormatInt(options.Limit, 10))
}
body, _, err := sc.DoRequestContext(ctx, node, "GET", u, nil, hdrs)
if err != nil {
return nil, err
}
r := []GraphiteMetric{}
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r, nil
}
// GraphiteFindTags retrieves metrics that are associated with the provided
// graphite tag search query.
func (sc *SnowthClient) GraphiteFindTags(accountID int64,
prefix, query string, options *GraphiteOptions,
nodes ...*SnowthNode,
) ([]GraphiteMetric, error) {
return sc.GraphiteFindTagsContext(context.Background(), accountID,
prefix, query, options, nodes...)
}
// GraphiteFindTagsContext is the context aware version of
// GraphiteFindTags.
func (sc *SnowthClient) GraphiteFindTagsContext(ctx context.Context,
accountID int64, prefix, query string, options *GraphiteOptions,
nodes ...*SnowthNode,
) ([]GraphiteMetric, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode()
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
u := fmt.Sprintf("%s?query=%s",
fmt.Sprintf("/graphite/%d/%s/tags/find",
accountID, url.QueryEscape(prefix)), url.QueryEscape(query))
hdrs := http.Header{}
if options != nil && options.Limit != 0 {
hdrs.Set("X-Snowth-Advisory-Limit",
strconv.FormatInt(options.Limit, 10))
}
body, _, err := sc.DoRequestContext(ctx, node, "GET", u, nil, hdrs)
if err != nil {
return nil, err
}
r := []GraphiteMetric{}
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r, nil
}
// GraphiteGetDatapoints retrieves graphite datapoint series for specified
// metrics for a specified time range.
func (sc *SnowthClient) GraphiteGetDatapoints(accountID int64,
prefix string, lookup *GraphiteLookup, options *GraphiteOptions,
nodes ...*SnowthNode,
) (*GraphiteDatapoints, error) {
return sc.GraphiteGetDatapointsContext(context.Background(), accountID,
prefix, lookup, options, nodes...)
}
// GraphiteGetDatapointsContext is the context aware version of
// GraphiteGetDatapoints.
func (sc *SnowthClient) GraphiteGetDatapointsContext(ctx context.Context,
accountID int64, prefix string, lookup *GraphiteLookup,
options *GraphiteOptions,
nodes ...*SnowthNode,
) (*GraphiteDatapoints, error) {
var node *SnowthNode
if len(nodes) > 0 && nodes[0] != nil {
node = nodes[0]
} else {
node = sc.GetActiveNode()
}
if node == nil {
return nil, fmt.Errorf("unable to get active node")
}
u := fmt.Sprintf("/graphite/%d/%s/series_multi",
accountID, url.QueryEscape(prefix))
buf := &bytes.Buffer{}
if err := json.NewEncoder(buf).Encode(&lookup); err != nil {
return nil, err
}
hdrs := http.Header{"Content-Type": {"application/json"}}
if options != nil && options.Limit != 0 {
hdrs.Set("X-Snowth-Advisory-Limit",
strconv.FormatInt(options.Limit, 10))
}
body, _, err := sc.DoRequestContext(ctx, node, "POST", u, buf, hdrs)
if err != nil {
return nil, err
}
r := &GraphiteDatapoints{}
if err := decodeJSON(body, &r); err != nil {
return nil, fmt.Errorf("unable to decode IRONdb response: %w", err)
}
return r, nil
}