forked from bosun-monitor/bosun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphite.go
225 lines (216 loc) · 5.97 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
package expr
import (
"encoding/json"
"fmt"
"strings"
"time"
"bosun.org/cmd/bosun/expr/parse"
"bosun.org/graphite"
"bosun.org/models"
"bosun.org/opentsdb"
"github.com/MiniProfiler/go/miniprofiler"
)
// Graphite defines functions for use with a Graphite backend.
var Graphite = map[string]parse.Func{
"graphiteBand": {
Args: []models.FuncType{models.TypeString, models.TypeString, models.TypeString, models.TypeString, models.TypeScalar},
Return: models.TypeSeriesSet,
Tags: graphiteTagQuery,
F: GraphiteBand,
},
"graphite": {
Args: []models.FuncType{models.TypeString, models.TypeString, models.TypeString, models.TypeString},
Return: models.TypeSeriesSet,
Tags: graphiteTagQuery,
F: GraphiteQuery,
},
}
func parseGraphiteResponse(req *graphite.Request, s *graphite.Response, formatTags []string) ([]*Result, error) {
const parseErrFmt = "graphite ParseError (%s): %s"
if len(*s) == 0 {
return nil, fmt.Errorf(parseErrFmt, req.URL, "empty response")
}
seen := make(map[string]bool)
results := make([]*Result, 0)
for _, res := range *s {
// build tag set
tags := make(opentsdb.TagSet)
if len(formatTags) == 1 && formatTags[0] == "" {
tags["key"] = res.Target
} else {
nodes := strings.Split(res.Target, ".")
if len(nodes) < len(formatTags) {
msg := fmt.Sprintf("returned target '%s' does not match format '%s'", res.Target, strings.Join(formatTags, ","))
return nil, fmt.Errorf(parseErrFmt, req.URL, msg)
}
for i, key := range formatTags {
if len(key) > 0 {
tags[key] = nodes[i]
}
}
}
if !tags.Valid() {
msg := fmt.Sprintf("returned target '%s' would make an invalid tag '%s'", res.Target, tags.String())
return nil, fmt.Errorf(parseErrFmt, req.URL, msg)
}
if ts := tags.String(); !seen[ts] {
seen[ts] = true
} else {
return nil, fmt.Errorf(parseErrFmt, req.URL, fmt.Sprintf("More than 1 series identified by tagset '%v'", ts))
}
// build data
dps := make(Series)
for _, dp := range res.Datapoints {
if len(dp) != 2 {
return nil, fmt.Errorf(parseErrFmt, req.URL, fmt.Sprintf("Datapoint has != 2 fields: %v", dp))
}
if len(dp[0].String()) == 0 {
// none value. skip this record
continue
}
val, err := dp[0].Float64()
if err != nil {
msg := fmt.Sprintf("value '%s' cannot be decoded to Float64: %s", dp[0], err.Error())
return nil, fmt.Errorf(parseErrFmt, req.URL, msg)
}
unixTS, err := dp[1].Int64()
if err != nil {
msg := fmt.Sprintf("timestamp '%s' cannot be decoded to Int64: %s", dp[1], err.Error())
return nil, fmt.Errorf(parseErrFmt, req.URL, msg)
}
t := time.Unix(unixTS, 0)
dps[t] = val
}
results = append(results, &Result{
Value: dps,
Group: tags,
})
}
return results, nil
}
func GraphiteBand(e *State, T miniprofiler.Timer, query, duration, period, format string, num float64) (r *Results, err error) {
r = new(Results)
r.IgnoreOtherUnjoined = true
r.IgnoreUnjoined = true
T.Step("graphiteBand", func(T miniprofiler.Timer) {
var d, p opentsdb.Duration
d, err = opentsdb.ParseDuration(duration)
if err != nil {
return
}
p, err = opentsdb.ParseDuration(period)
if err != nil {
return
}
if num < 1 || num > 100 {
err = fmt.Errorf("expr: Band: num out of bounds")
}
req := &graphite.Request{
Targets: []string{query},
}
now := e.now
req.End = &now
st := e.now.Add(-time.Duration(d))
req.Start = &st
for i := 0; i < int(num); i++ {
now = now.Add(time.Duration(-p))
req.End = &now
st := now.Add(time.Duration(-d))
req.Start = &st
var s graphite.Response
s, err = timeGraphiteRequest(e, T, req)
if err != nil {
return
}
formatTags := strings.Split(format, ".")
var results []*Result
results, err = parseGraphiteResponse(req, &s, formatTags)
if err != nil {
return
}
if i == 0 {
r.Results = results
} else {
// different graphite requests might return series with different id's.
// i.e. a different set of tagsets. merge the data of corresponding tagsets
for _, result := range results {
updateKey := -1
for j, existing := range r.Results {
if result.Group.Equal(existing.Group) {
updateKey = j
break
}
}
if updateKey == -1 {
// result tagset is new
r.Results = append(r.Results, result)
updateKey = len(r.Results) - 1
}
for k, v := range result.Value.(Series) {
r.Results[updateKey].Value.(Series)[k] = v
}
}
}
}
})
if err != nil {
return nil, fmt.Errorf("graphiteBand: %v", err)
}
return
}
func GraphiteQuery(e *State, T miniprofiler.Timer, query string, sduration, eduration, format string) (r *Results, err error) {
sd, err := opentsdb.ParseDuration(sduration)
if err != nil {
return
}
ed := opentsdb.Duration(0)
if eduration != "" {
ed, err = opentsdb.ParseDuration(eduration)
if err != nil {
return
}
}
st := e.now.Add(-time.Duration(sd))
et := e.now.Add(-time.Duration(ed))
req := &graphite.Request{
Targets: []string{query},
Start: &st,
End: &et,
}
s, err := timeGraphiteRequest(e, T, req)
if err != nil {
return nil, err
}
formatTags := strings.Split(format, ".")
r = new(Results)
results, err := parseGraphiteResponse(req, &s, formatTags)
if err != nil {
return nil, err
}
r.Results = results
return
}
func graphiteTagQuery(args []parse.Node) (parse.Tags, error) {
t := make(parse.Tags)
n := args[3].(*parse.StringNode)
for _, s := range strings.Split(n.Text, ".") {
if s != "" {
t[s] = struct{}{}
}
}
return t, nil
}
func timeGraphiteRequest(e *State, T miniprofiler.Timer, req *graphite.Request) (resp graphite.Response, err error) {
e.graphiteQueries = append(e.graphiteQueries, *req)
b, _ := json.MarshalIndent(req, "", " ")
T.StepCustomTiming("graphite", "query", string(b), func() {
key := req.CacheKey()
getFn := func() (interface{}, error) {
return e.GraphiteContext.Query(req)
}
var val interface{}
val, err = e.Cache.Get(key, getFn)
resp = val.(graphite.Response)
})
return
}