/
snapshotClient.go
359 lines (320 loc) · 10.8 KB
/
snapshotClient.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
package snapshot
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"log"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
// SnapClient is for taking multiple snapshots of a Grafana instance and posting
// them to a snapshot host
type SnapClient struct {
config *Config
datasourceCache map[string]interface{}
}
// Snapshot is returned on a successful Take call
type Snapshot struct {
URL string `json:"url"`
Key string `json:"key"`
DeleteURL string `json:"deleteUrl"`
DeleteKey string `json:"deleteKey"`
}
type snapshotData struct {
Target string `json:"target"`
Datapoints [][]interface{} `json:"datapoints"`
// Metric is a set of labels (e.g. instance=alp) which is retained
// so that we can replace labels according to target.legendFormat.
Metric model.Metric `json:"-"`
}
func init() {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
// NewSnapClient takes a Config, validates it, and returns a SnapClient
func NewSnapClient(config *Config) (*SnapClient, error) {
c, err := processConfig(config)
if err != nil {
return nil, err
}
return &SnapClient{c, nil}, nil
}
// Take is for taking a snapshot
// TODO: Should take context
func (sc *SnapClient) Take(config *TakeConfig) (*Snapshot, error) {
// process and validate config
c, err := processTakeConfig(config)
if err != nil {
return nil, err
}
// get dashboard
rawDashString, err := sc.getDashboardDef(c)
if err != nil {
return nil, err
}
// Get available datasources and map them to their names
datasourceMap, err := sc.getDatasourceDefs()
if err != nil {
return nil, err
}
sc.datasourceCache = datasourceMap
// Replace all templated variables
subbedDashString, err := sc.substituteVars(c, rawDashString)
if err != nil {
err = fmt.Errorf("failed to substituteVars of rawDashString: %v", err)
return nil, err
}
// Unmarshal it
var dash map[string]interface{}
if err = json.Unmarshal([]byte(subbedDashString), &dash); err != nil {
return nil, fmt.Errorf("Could not decode dashboard json: %s", err.Error())
}
if dash["dashboard"] == nil {
return nil, fmt.Errorf(dash["message"].(string))
}
// For each panel in dashboard...
for _, p := range dash["dashboard"].(map[string]interface{})["panels"].([]interface{}) {
panel := p.(map[string]interface{})
// Get the datasource and targets
datasourceName := panel["datasource"].(string)
targets := panel["targets"].([]interface{})
// For each target in panel...
for _, t := range targets {
target := t.(map[string]interface{})
// Calculate “step” like Grafana. For the original code, see:
// https://github.com/grafana/grafana/blob/79138e211fac98bf1d12f1645ecd9fab5846f4fb/public/app/plugins/datasource/prometheus/datasource.ts#L83
intervalFactor := float64(1)
if target["intervalFactor"] != nil {
intervalFactor = target["intervalFactor"].(float64)
}
interval := time.Second * 30
if target["interval"] != nil && target["interval"].(string) != "" {
log.Printf(target["interval"].(string))
interval, err = time.ParseDuration(target["interval"].(string))
}
if err != nil {
return nil, err
}
step := interval.Seconds() * intervalFactor
// Lookup datasource
datasource := datasourceMap[datasourceName].(map[string]interface{})
// Fetch data points from datasource proxy
var dataPoints []snapshotData
switch datasource["type"].(string) {
case "prometheus":
dataPoints, err = sc.fetchDataPointsPrometheus(c, target, datasource, step)
if err != nil {
return nil, err
}
case "elasticsearch":
dataPoints, err = sc.fetchDataPointsElastic(c, target, datasource, step)
if err != nil {
return nil, err
}
default:
// unsupported
continue
}
var snapshotData []interface{}
// build snapshot data
for idx, dp := range dataPoints {
if target["legendFormat"] != nil && target["legendFormat"].(string) != "" {
dp.Target = sc.renderTemplate(target["legendFormat"].(string), dp.Metric)
} else {
dp.Target = dp.Metric.String()
}
dataPoints[idx] = dp
snapshotData = append(snapshotData, dp)
}
if snapshotData == nil {
snapshotData = []interface{}{}
}
// insert snapshot data into panels
panel["snapshotData"] = snapshotData
panel["targets"] = []interface{}{}
panel["links"] = []interface{}{}
panel["datasource"] = []interface{}{}
}
}
// Build Snapshot
snapshot := make(map[string]interface{})
// remove templating
dash["dashboard"].(map[string]interface{})["templating"].(map[string]interface{})["list"] = []interface{}{}
// update time range
dash["dashboard"].(map[string]interface{})["time"].(map[string]interface{})["from"] = c.From.Format(time.RFC3339Nano)
dash["dashboard"].(map[string]interface{})["time"].(map[string]interface{})["to"] = c.To.Format(time.RFC3339Nano)
snapshot["dashboard"] = dash["dashboard"]
snapshot["expires"] = (c.Expires / time.Second)
snapshot["name"] = c.SnapshotName
b, err := json.Marshal(snapshot)
// Post Snapshot
reqURL := *sc.config.SnapshotAddr
reqURL.Path = reqURL.Path + "api/snapshots"
log.Printf("Posting snapshot to: %s", reqURL.String())
req, err := http.NewRequest("POST", reqURL.String(), bytes.NewReader(b))
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+sc.config.SnapshotAPIKey)
req.Header.Add("Content-Type", "application/json")
resp, err := (&http.Client{}).Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Unexpected status code when posting snapshot: %s", resp.Status)
}
// read body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// parse body
var snapshotResponse Snapshot
if err = json.Unmarshal(body, &snapshotResponse); err != nil {
return nil, err
}
return &snapshotResponse, nil
}
func (sc *SnapClient) getDashboardDef(config *TakeConfig) (string, error) {
// Get dashboard def
reqURL := *sc.config.GrafanaAddr
reqURL.Path = reqURL.Path + "api/dashboards/uid/" + config.DashUID
fmt.Println(reqURL.String())
req, err := http.NewRequest("GET", reqURL.String(), nil)
if err != nil {
return "", err
}
req.Header.Add("Authorization", "Bearer "+sc.config.GrafanaAPIKey)
resp, err := (&http.Client{}).Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func (sc *SnapClient) getDatasourceDefs() (map[string]interface{}, error) {
// Get datasource defs
reqURL := *sc.config.GrafanaAddr
reqURL.Path = reqURL.Path + "api/datasources"
req, err := http.NewRequest("GET", reqURL.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", "Bearer "+sc.config.GrafanaAPIKey)
resp, err := (&http.Client{}).Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New("Unexpected status code: " + resp.Status)
}
// read body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// parse body
var datasources []interface{}
if err = json.Unmarshal(body, &datasources); err != nil {
return nil, err
}
// map datasources to their names
datasourceMap := make(map[string]interface{})
for _, ds := range datasources {
datasourceMap[ds.(map[string]interface{})["name"].(string)] = ds
}
return datasourceMap, nil
}
func (sc *SnapClient) substituteVars(config *TakeConfig, dashboardString string) (string, error) {
for k, v := range config.Vars {
vk := "$" + k
dashboardString = strings.Replace(dashboardString, vk, v, -1)
}
return dashboardString, nil
}
// Implementation of CancelableTransport (https://gowalker.org/github.com/prometheus/client_golang/api/prometheus#CancelableTransport)
// Required to intercept the api requests and add the auth header for going
// through the Grafana datasource proxy
type grafanaProxyTransport struct {
http.Transport
grafanaAPIKey string
}
// Adds the Grafana API key auth header to any request
func (gpt *grafanaProxyTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer "+gpt.grafanaAPIKey)
return (&http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}).RoundTrip(req)
}
func (sc *SnapClient) fetchDataPointsPrometheus(config *TakeConfig, target, datasource map[string]interface{}, step float64) ([]snapshotData, error) {
reqURL := *sc.config.GrafanaAddr
reqURL.Path = reqURL.Path + "api/datasources/proxy/" + strconv.Itoa(int(datasource["id"].(float64)))
log.Printf("Requesting data points from: %s\n\tusing: %s", reqURL.String(), target["expr"].(string))
// Use our Grafana proxy transport with configured API key
transport := grafanaProxyTransport{grafanaAPIKey: sc.config.GrafanaAPIKey}
client, err := api.NewClient(api.Config{Address: reqURL.String(), RoundTripper: &transport})
if err != nil {
return nil, err
}
api := v1.NewAPI(client)
// Query
val, err := api.QueryRange(context.Background(), target["expr"].(string), v1.Range{
Start: *config.From,
End: *config.To,
Step: time.Duration(step) * time.Second,
})
if err != nil {
return nil, err
}
if val.Type() != model.ValMatrix {
return nil, fmt.Errorf("Unexpected value type: got %q, want %q", val.Type(), model.ValMatrix)
}
matrix, ok := val.(model.Matrix)
if !ok {
return nil, fmt.Errorf("Bug: val.Type() == model.ValMatrix, but type assertion failed")
}
results := make([]snapshotData, matrix.Len())
for idx, stream := range matrix {
datapoints := make([][]interface{}, len(stream.Values))
for idx, samplepair := range stream.Values {
if math.IsNaN(float64(samplepair.Value)) {
datapoints[idx] = []interface{}{nil, float64(samplepair.Timestamp)}
} else {
datapoints[idx] = []interface{}{float64(samplepair.Value), float64(samplepair.Timestamp)}
}
}
results[idx] = snapshotData{
Metric: stream.Metric,
Datapoints: datapoints,
}
}
return results, nil
}
func (sc *SnapClient) fetchDataPointsElastic(config *TakeConfig, target, datasource map[string]interface{}, step float64) ([]snapshotData, error) {
return nil, nil
}
var aliasRe = regexp.MustCompile(`{{\s*(.+?)\s*}}`)
// renderTemplate is a re-implementation of renderTemplate in
// Grafana’s Prometheus datasource; for the original, see:
// https://github.com/grafana/grafana/blob/79138e211fac98bf1d12f1645ecd9fab5846f4fb/public/app/plugins/datasource/prometheus/datasource.ts#L263
func (sc *SnapClient) renderTemplate(format string, metric model.Metric) string {
return aliasRe.ReplaceAllStringFunc(format, func(match string) string {
matches := aliasRe.FindStringSubmatch(match)
return string(metric[model.LabelName(matches[1])])
})
}