-
Notifications
You must be signed in to change notification settings - Fork 123
/
render.go
514 lines (447 loc) · 14.3 KB
/
render.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package carbonserver
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"net/http"
_ "net/http/pprof"
"strconv"
"strings"
"sync/atomic"
"time"
"go.uber.org/zap"
"github.com/go-graphite/carbonzipper/zipper/httpHeaders"
protov2 "github.com/go-graphite/protocol/carbonapi_v2_pb"
protov3 "github.com/go-graphite/protocol/carbonapi_v3_pb"
pickle "github.com/lomik/og-rek"
)
type fetchResponse struct {
data []byte
contentType string
metricsFetched int
valuesFetched int
memoryUsed int
metrics []string
}
type target struct {
Name string
PathExpression string
}
type timeRange struct {
from int32
until int32
}
func getTargetNames(targets map[timeRange][]target) []string {
c := 0
for _, v := range targets {
c += len(v)
}
names := make([]string, 0, c)
for _, v := range targets {
for _, t := range v {
names = append(names, t.Name)
}
}
return names
}
func stringToInt32(t string) (int32, error) {
i, err := strconv.Atoi(t)
if err != nil {
return int32(i), err
}
return int32(i), nil
}
func getFormat(req *http.Request) (responseFormat, error) {
format := req.FormValue("format")
if format == "" {
format = "json"
}
for _, accept := range req.Header["Accept"] {
if accept == httpHeaders.ContentTypeCarbonAPIv3PB {
format = "carbonapi_v3_pb"
break
}
}
formatCode, ok := knownFormats[format]
if !ok {
return formatCode, fmt.Errorf("Unknown format")
}
return formatCode, nil
}
func getTargets(req *http.Request, format responseFormat) (map[timeRange][]target, error) {
targets := make(map[timeRange][]target)
switch format {
case protoV3Format:
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return targets, fmt.Errorf("error reading body: %s", err.Error())
}
var pv3Request protov3.MultiFetchRequest
if err := pv3Request.Unmarshal(body); err != nil {
return targets, fmt.Errorf("invalid payload: %s", err.Error())
}
for _, t := range pv3Request.Metrics {
tr := timeRange{
from: int32(t.StartTime),
until: int32(t.StopTime),
}
targets[tr] = append(targets[tr], target{Name: t.Name, PathExpression: t.PathExpression})
}
default:
from, err := stringToInt32(req.FormValue("from"))
if err != nil {
return targets, fmt.Errorf("invalid 'from' time")
}
until, err := stringToInt32(req.FormValue("until"))
if err != nil {
return targets, fmt.Errorf("invalid 'until' time")
}
tr := timeRange{from: from, until: until}
for _, t := range req.Form["target"] {
targets[tr] = append(targets[tr], target{Name: t, PathExpression: t})
}
}
return targets, nil
}
func (listener *CarbonserverListener) renderHandler(wr http.ResponseWriter, req *http.Request) {
// URL: /render/?target=the.metric.Name&format=pickle&from=1396008021&until=1396022421
t0 := time.Now()
ctx := req.Context()
atomic.AddUint64(&listener.metrics.RenderRequests, 1)
accessLogger := TraceContextToZap(ctx, listener.accessLogger.With(
zap.String("handler", "render"),
zap.String("url", req.URL.RequestURI()),
zap.String("peer", req.RemoteAddr),
))
format, err := getFormat(req)
if err != nil {
atomic.AddUint64(&listener.metrics.RenderErrors, 1)
accessLogger.Error("fetch failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", err.Error()),
zap.Int("http_code", http.StatusBadRequest),
)
http.Error(wr, fmt.Sprintf("Bad request: %s", err), http.StatusBadRequest)
return
}
accessLogger = accessLogger.With(
zap.String("format", format.String()),
)
targets, err := getTargets(req, format)
if err != nil {
atomic.AddUint64(&listener.metrics.RenderErrors, 1)
accessLogger.Error("fetch failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", err.Error()),
zap.Int("http_code", http.StatusBadRequest),
)
http.Error(wr, fmt.Sprintf("Bad request: %s", err), http.StatusBadRequest)
return
}
tgs := getTargetNames(targets)
accessLogger = accessLogger.With(
zap.Strings("targets", tgs),
)
logger := TraceContextToZap(ctx, listener.accessLogger.With(
zap.String("handler", "render"),
zap.String("url", req.URL.RequestURI()),
zap.String("peer", req.RemoteAddr),
zap.Strings("targets", tgs),
zap.String("format", format.String()),
))
// Make sure we log which metric caused a panic()
defer func() {
if r := recover(); r != nil {
logger.Error("panic recovered",
zap.Stack("stack"),
zap.Any("error", r),
)
accessLogger.Error("fetch failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", "panic during serving the request"),
zap.Stack("stack"),
zap.Any("error", r),
zap.Int("http_code", http.StatusInternalServerError),
)
http.Error(wr, fmt.Sprintf("Panic occured, see logs for more information"), http.StatusInternalServerError)
}
}()
response, fromCache, err := listener.fetchWithCache(logger, format, targets)
wr.Header().Set("Content-Type", response.contentType)
if err != nil {
atomic.AddUint64(&listener.metrics.RenderErrors, 1)
accessLogger.Error("fetch failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", "failed to read data"),
zap.Int("http_code", http.StatusBadRequest),
zap.Error(err),
)
http.Error(wr, fmt.Sprintf("Bad request (%s)", err), http.StatusBadRequest)
return
}
if response.metricsFetched == 0 {
atomic.AddUint64(&listener.metrics.RenderErrors, 1)
accessLogger.Error("fetch failed",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.String("reason", "no metrics found"),
zap.Int("http_code", http.StatusNotFound),
zap.Error(err),
)
http.Error(wr, "Bad request (Not Found)", http.StatusNotFound)
return
}
listener.UpdateMetricsAccessTimesByRequest(response.metrics)
wr.Write(response.data)
atomic.AddUint64(&listener.metrics.FetchSize, uint64(response.memoryUsed))
logger.Info("fetch served",
zap.Duration("runtime_seconds", time.Since(t0)),
zap.Bool("query_cache_enabled", listener.queryCacheEnabled),
zap.Bool("from_cache", fromCache),
zap.Int("metrics_fetched", response.metricsFetched),
zap.Int("values_fetched", response.valuesFetched),
zap.Int("memory_used_bytes", response.memoryUsed),
zap.Int("http_code", http.StatusOK),
)
}
func (listener *CarbonserverListener) fetchWithCache(logger *zap.Logger, format responseFormat, targets map[timeRange][]target) (fetchResponse, bool, error) {
logger = logger.With(
zap.String("function", "fetchWithCache"),
)
fromCache := false
var err error
var response fetchResponse
if listener.queryCacheEnabled {
targetKeys := make([]string, 0, len(targets))
for tr, ts := range targets {
names := make([]string, 0, len(ts))
for _, t := range ts {
names = append(names, t.Name)
}
targetKeys = append(targetKeys, fmt.Sprintf("%s&%d&%d", strings.Join(names, "&"), tr.from, tr.until))
}
key := fmt.Sprintf("%s&%s", strings.Join(targetKeys, "&"), format)
size := uint64(100 * 1024 * 1024)
renderRequests := atomic.LoadUint64(&listener.metrics.RenderRequests)
fetchSize := atomic.LoadUint64(&listener.metrics.FetchSize)
if renderRequests > 0 {
size = fetchSize / renderRequests
}
// TODO(gmagnusson): Our cache key changes if we permute any of the
// metric names while keeping the from/until pairs fixed, or permute
// any combination of metric names and from/until pairs as a unit.
// These permutations to not change the response, so we should be more
// clever about how we construct the cache key.
//
// An option is to sort the metric names within each from/until pair,
// and to order (names,from,until) triples by the from timestamp.
//
// This is probably worth doing, as the only source of differences in
// from/until pairs I know of comes from using timeShift or timeStack,
// which again probably comes from trying to compare trends from last
// week to what's going on now, which is something people are generally
// interested in. It wouldn't be good to start adding false negative
// cache misses to those kinds of queries.
item := listener.queryCache.getQueryItem(key, size, 60)
res, ok := item.FetchOrLock()
listener.prometheus.cacheRequest("query", ok)
if !ok {
logger.Debug("query cache miss")
atomic.AddUint64(&listener.metrics.QueryCacheMiss, 1)
response, err = listener.prepareDataProto(format, targets)
if err != nil {
item.StoreAbort()
} else {
item.StoreAndUnlock(response)
}
} else {
logger.Debug("query cache hit")
atomic.AddUint64(&listener.metrics.QueryCacheHit, 1)
response = res.(fetchResponse)
fromCache = true
}
} else {
response, err = listener.prepareDataProto(format, targets)
}
return response, fromCache, err
}
func (listener *CarbonserverListener) prepareDataProto(format responseFormat, targets map[timeRange][]target) (fetchResponse, error) {
contentType := "application/text"
var b []byte
var err error
var metricsFetched int
var memoryUsed int
var valuesFetched int
var multiv3 protov3.MultiFetchResponse
var multiv2 protov2.MultiFetchResponse
var metrics []string
for tr, ts := range targets {
for _, metric := range ts {
fromTime := tr.from
untilTime := tr.until
listener.logger.Debug("fetching data...")
files, leafs, err := listener.expandGlobs(metric.Name)
if err != nil {
listener.logger.Debug("expand globs returned an error",
zap.Error(err),
)
continue
}
metricsCount := 0
for i := range files {
if leafs[i] {
metricsCount++
}
}
listener.logger.Debug("expandGlobs result",
zap.String("handler", "render"),
zap.String("action", "expandGlobs"),
zap.String("metric", metric.Name),
zap.Int("metrics_count", metricsCount),
zap.Int32("from", fromTime),
zap.Int32("until", untilTime),
)
if format == protoV2Format || format == jsonFormat {
res, err := listener.fetchDataPB(metric.Name, files, leafs, fromTime, untilTime)
if err != nil {
atomic.AddUint64(&listener.metrics.RenderErrors, 1)
listener.logger.Error("error while fetching the data",
zap.Error(err),
)
continue
}
multiv2.Metrics = append(multiv2.Metrics, res.Metrics...)
} else {
res, err := listener.fetchDataPB3(metric.Name, files, leafs, fromTime, untilTime)
if err != nil {
atomic.AddUint64(&listener.metrics.RenderErrors, 1)
listener.logger.Error("error while fetching the data",
zap.Error(err),
)
continue
}
for i := range res.Metrics {
res.Metrics[i].PathExpression = metric.PathExpression
}
multiv3.Metrics = append(multiv3.Metrics, res.Metrics...)
}
}
}
if format == protoV2Format || format == jsonFormat {
if len(multiv2.Metrics) == 0 && format == protoV2Format {
return fetchResponse{nil, contentType, 0, 0, 0, nil}, err
}
metricsFetched = len(multiv2.Metrics)
for i := range multiv2.Metrics {
metrics = append(metrics, multiv2.Metrics[i].Name)
memoryUsed += multiv2.Metrics[i].Size()
valuesFetched += len(multiv2.Metrics[i].Values)
}
} else {
if len(multiv3.Metrics) == 0 && format == protoV3Format {
return fetchResponse{nil, contentType, 0, 0, 0, nil}, err
}
metricsFetched = len(multiv3.Metrics)
for i := range multiv3.Metrics {
metrics = append(metrics, multiv3.Metrics[i].Name)
memoryUsed += multiv3.Metrics[i].Size()
valuesFetched += len(multiv3.Metrics[i].Values)
}
}
switch format {
// We still keep old json format, because it's painful to deal with math.NaN that can occur in new format.
case jsonFormat:
contentType = "application/json"
b, err = json.Marshal(multiv2)
case protoV2Format:
contentType = httpHeaders.ContentTypeCarbonAPIv2PB
b, err = multiv2.Marshal()
case protoV3Format:
contentType = httpHeaders.ContentTypeCarbonAPIv3PB
b, err = multiv3.Marshal()
case pickleFormat:
// transform protobuf data into what pickle expects
//[{'start': 1396271100, 'step': 60, 'Name': 'metric',
//'values': [9.0, 19.0, None], 'end': 1396273140}
var response []map[string]interface{}
for _, metric := range multiv3.GetMetrics() {
var m map[string]interface{}
m = make(map[string]interface{})
m["start"] = metric.StartTime
m["step"] = metric.StepTime
m["end"] = metric.StopTime
m["name"] = metric.Name
m["pathExpression"] = metric.PathExpression
m["xFilesFactor"] = metric.XFilesFactor
m["consolidationFunc"] = metric.ConsolidationFunc
mv := make([]interface{}, len(metric.Values))
for i, p := range metric.Values {
if math.IsNaN(metric.Values[i]) {
mv[i] = nil
} else {
mv[i] = p
}
}
m["values"] = mv
response = append(response, m)
}
contentType = httpHeaders.ContentTypePickle
var buf bytes.Buffer
pEnc := pickle.NewEncoder(&buf)
err = pEnc.Encode(response)
b = buf.Bytes()
default:
err = fmt.Errorf("unknown format: %v", format)
}
if err != nil {
return fetchResponse{nil, contentType, 0, 0, 0, nil}, err
}
return fetchResponse{b, contentType, metricsFetched, valuesFetched, memoryUsed, metrics}, nil
}
func (listener *CarbonserverListener) fetchDataPB3(pathExpression string, files []string, leafs []bool, fromTime, untilTime int32) (*protov3.MultiFetchResponse, error) {
var multi protov3.MultiFetchResponse
var errs []error
for i, fileName := range files {
if !leafs[i] {
listener.logger.Debug("skipping directory", zap.String("PathExpression", pathExpression))
// can't fetch a directory
continue
}
response, err := listener.fetchSingleMetricV3(fileName, pathExpression, fromTime, untilTime)
if err == nil {
multi.Metrics = append(multi.Metrics, *response)
} else {
errs = append(errs, err)
}
}
if len(errs) != 0 {
listener.logger.Warn("errors occur while fetching data",
zap.Any("errors", errs),
)
}
return &multi, nil
}
func (listener *CarbonserverListener) fetchDataPB(metric string, files []string, leafs []bool, fromTime, untilTime int32) (*protov2.MultiFetchResponse, error) {
var multi protov2.MultiFetchResponse
var errs []error
for i, metric := range files {
if !leafs[i] {
listener.logger.Debug("skipping directory", zap.String("metric", metric))
// can't fetch a directory
continue
}
response, err := listener.fetchSingleMetricV2(metric, fromTime, untilTime)
if err == nil {
multi.Metrics = append(multi.Metrics, *response)
} else {
errs = append(errs, err)
}
}
if len(errs) != 0 {
listener.logger.Warn("errors occur while fetching data",
zap.Any("errors", errs),
)
}
return &multi, nil
}