-
Notifications
You must be signed in to change notification settings - Fork 139
/
render_handler.go
419 lines (354 loc) · 11.9 KB
/
render_handler.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
package http
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/ansel1/merry"
"github.com/go-graphite/carbonapi/carbonapipb"
"github.com/go-graphite/carbonapi/cmd/carbonapi/config"
"github.com/go-graphite/carbonapi/date"
"github.com/go-graphite/carbonapi/expr"
"github.com/go-graphite/carbonapi/expr/functions/cairo/png"
"github.com/go-graphite/carbonapi/expr/types"
"github.com/go-graphite/carbonapi/pkg/parser"
utilctx "github.com/go-graphite/carbonapi/util/ctx"
pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
"github.com/lomik/zapwriter"
uuid "github.com/satori/go.uuid"
"go.uber.org/zap"
)
func cleanupParams(r *http.Request) {
// make sure the cache key doesn't say noCache, because it will never hit
r.Form.Del("noCache")
// jsonp callback names are frequently autogenerated and hurt our cache
r.Form.Del("jsonp")
// Strip some cache-busters. If you don't want to cache, use noCache=1
r.Form.Del("_salt")
r.Form.Del("_ts")
r.Form.Del("_t") // Used by jquery.graphite.js
}
func setError(w http.ResponseWriter, accessLogDetails *carbonapipb.AccessLogDetails, msg string, status int) {
http.Error(w, http.StatusText(status)+": "+msg, status)
accessLogDetails.Reason = msg
accessLogDetails.HTTPCode = int32(status)
}
func getCacheTimeout(logger *zap.Logger, r *http.Request, defaultTimeout int32) int32 {
if tstr := r.FormValue("cacheTimeout"); tstr != "" {
t, err := strconv.Atoi(tstr)
if err != nil {
logger.Error("failed to parse cacheTimeout",
zap.String("cache_string", tstr),
zap.Error(err),
)
} else {
return int32(t)
}
}
return defaultTimeout
}
func renderHandler(w http.ResponseWriter, r *http.Request) {
t0 := time.Now()
uid := uuid.NewV4()
// TODO: Migrate to context.WithTimeout
// ctx, _ := context.WithTimeout(context.TODO(), config.Config.ZipperTimeout)
ctx := utilctx.SetUUID(r.Context(), uid.String())
username, _, _ := r.BasicAuth()
requestHeaders := utilctx.GetLogHeaders(ctx)
logger := zapwriter.Logger("render").With(
zap.String("carbonapi_uuid", uid.String()),
zap.String("username", username),
zap.Any("request_headers", requestHeaders),
)
srcIP, srcPort := splitRemoteAddr(r.RemoteAddr)
accessLogger := zapwriter.Logger("access")
var accessLogDetails = &carbonapipb.AccessLogDetails{
Handler: "render",
Username: username,
CarbonapiUUID: uid.String(),
URL: r.URL.RequestURI(),
PeerIP: srcIP,
PeerPort: srcPort,
Host: r.Host,
Referer: r.Referer(),
URI: r.RequestURI,
RequestHeaders: requestHeaders,
}
logAsError := false
defer func() {
deferredAccessLogging(accessLogger, accessLogDetails, t0, logAsError)
}()
ApiMetrics.Requests.Add(1)
err := r.ParseForm()
if err != nil {
setError(w, accessLogDetails, err.Error(), http.StatusBadRequest)
logAsError = true
return
}
targets := r.Form["target"]
from := r.FormValue("from")
until := r.FormValue("until")
template := r.FormValue("template")
maxDataPoints, _ := strconv.ParseInt(r.FormValue("maxDataPoints"), 10, 64)
ctx = utilctx.SetMaxDatapoints(ctx, maxDataPoints)
useCache := !parser.TruthyBool(r.FormValue("noCache"))
noNullPoints := parser.TruthyBool(r.FormValue("noNullPoints"))
// status will be checked later after we'll setup everything else
format, ok, formatRaw := getFormat(r, pngFormat)
var jsonp string
if format == jsonFormat {
// TODO(dgryski): check jsonp only has valid characters
jsonp = r.FormValue("jsonp")
}
timestampFormat := strings.ToLower(r.FormValue("timestampFormat"))
if timestampFormat == "" {
timestampFormat = "s"
}
timestampMultiplier := int64(1)
switch timestampFormat {
case "s":
timestampMultiplier = 1
case "ms", "millisecond", "milliseconds":
timestampMultiplier = 1000
case "us", "microsecond", "microseconds":
timestampMultiplier = 1000000
case "ns", "nanosecond", "nanoseconds":
timestampMultiplier = 1000000000
default:
setError(w, accessLogDetails, "unsupported timestamp format, supported: 's', 'ms', 'us', 'ns'", http.StatusBadRequest)
logAsError = true
return
}
responseCacheTimeout := getCacheTimeout(logger, r, config.Config.ResponseCacheConfig.DefaultTimeoutSec)
backendCacheTimeout := getCacheTimeout(logger, r, config.Config.BackendCacheConfig.DefaultTimeoutSec)
cleanupParams(r)
responseCacheKey := r.Form.Encode()
// normalize from and until values
qtz := r.FormValue("tz")
from32 := date.DateParamToEpoch(from, qtz, timeNow().Add(-24*time.Hour).Unix(), config.Config.DefaultTimeZone)
until32 := date.DateParamToEpoch(until, qtz, timeNow().Unix(), config.Config.DefaultTimeZone)
accessLogDetails.UseCache = useCache
accessLogDetails.FromRaw = from
accessLogDetails.From = from32
accessLogDetails.UntilRaw = until
accessLogDetails.Until = until32
accessLogDetails.Tz = qtz
accessLogDetails.CacheTimeout = responseCacheTimeout
accessLogDetails.Format = formatRaw
accessLogDetails.Targets = targets
if !ok || !format.ValidRenderFormat() {
setError(w, accessLogDetails, "unsupported format specified: "+formatRaw, http.StatusBadRequest)
logAsError = true
return
}
if format == protoV3Format {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
accessLogDetails.HTTPCode = http.StatusBadRequest
accessLogDetails.Reason = "failed to parse message body: " + err.Error()
http.Error(w, "bad request (failed to parse format): "+err.Error(), http.StatusBadRequest)
return
}
var pv3Request pb.MultiFetchRequest
err = pv3Request.Unmarshal(body)
if err != nil {
accessLogDetails.HTTPCode = http.StatusBadRequest
accessLogDetails.Reason = "failed to parse message body: " + err.Error()
http.Error(w, "bad request (failed to parse format): "+err.Error(), http.StatusBadRequest)
return
}
from32 = pv3Request.Metrics[0].StartTime
until32 = pv3Request.Metrics[0].StopTime
targets = make([]string, len(pv3Request.Metrics))
for i, r := range pv3Request.Metrics {
targets[i] = r.PathExpression
}
}
if useCache {
tc := time.Now()
response, err := config.Config.ResponseCache.Get(responseCacheKey)
td := time.Since(tc).Nanoseconds()
ApiMetrics.RenderCacheOverheadNS.Add(td)
accessLogDetails.CarbonzipperResponseSizeBytes = 0
accessLogDetails.CarbonapiResponseSizeBytes = int64(len(response))
if err == nil {
ApiMetrics.RequestCacheHits.Add(1)
writeResponse(w, http.StatusOK, response, format, jsonp)
accessLogDetails.FromCache = true
return
}
ApiMetrics.RequestCacheMisses.Add(1)
}
if from32 == until32 {
setError(w, accessLogDetails, "Invalid or empty time range", http.StatusBadRequest)
logAsError = true
return
}
defer func() {
if r := recover(); r != nil {
logger.Error("panic during eval:",
zap.String("cache_key", responseCacheKey),
zap.Any("reason", r),
zap.Stack("stack"),
)
logAsError = true
var answer string
if config.Config.HTTPResponseStackTrace {
answer = fmt.Sprintf("%v\nStack trace: %v", r, zap.Stack("").String)
} else {
answer = fmt.Sprint(r)
}
setError(w, accessLogDetails, answer, http.StatusInternalServerError)
}
}()
errors := make(map[string]merry.Error)
backendCacheKey := backendCacheComputeKey(from, until, targets)
results, err := backendCacheFetchResults(logger, useCache, backendCacheKey, accessLogDetails)
if err != nil {
ApiMetrics.BackendCacheMisses.Add(1)
results = make([]*types.MetricData, 0)
values := make(map[parser.MetricRequest][]*types.MetricData)
for _, target := range targets {
exp, e, err := parser.ParseExpr(target)
if err != nil || e != "" {
msg := buildParseErrorString(target, e, err)
setError(w, accessLogDetails, msg, http.StatusBadRequest)
logAsError = true
return
}
ApiMetrics.RenderRequests.Add(1)
result, err := expr.FetchAndEvalExp(ctx, exp, from32, until32, values)
if err != nil {
errors[target] = merry.Wrap(err)
}
results = append(results, result...)
}
for mFetch := range values {
expr.SortMetrics(values[mFetch], mFetch)
}
if len(errors) == 0 {
backendCacheStoreResults(logger, backendCacheKey, results, backendCacheTimeout)
}
}
size := 0
for _, result := range results {
size += result.Size()
}
var body []byte
returnCode := http.StatusOK
if len(results) == 0 {
// Obtain error code from the errors
// In case we have only "Not Found" errors, result should be 404
// Otherwise it should be 500
returnCode = http.StatusNotFound
errMsgs := make([]string, 0)
for _, err := range errors {
if merry.HTTPCode(err) == 404 || merry.Is(err, parser.ErrSeriesDoesNotExist) {
continue
}
errMsgs = append(errMsgs, err.Error())
if returnCode < 500 {
returnCode = merry.HTTPCode(err)
}
}
logger.Debug("error response or no response", zap.Strings("error", errMsgs))
// Allow override status code for 404-not-found replies.
if returnCode == 404 {
returnCode = config.Config.NotFoundStatusCode
}
if returnCode >= 500 {
setError(w, accessLogDetails, "error or no response: "+strings.Join(errMsgs, ","), returnCode)
logAsError = true
return
}
}
switch format {
case jsonFormat:
if maxDataPoints != 0 {
types.ConsolidateJSON(maxDataPoints, results)
accessLogDetails.MaxDataPoints = maxDataPoints
}
body = types.MarshalJSON(results, timestampMultiplier, noNullPoints)
case protoV2Format:
body, err = types.MarshalProtobufV2(results)
if err != nil {
setError(w, accessLogDetails, err.Error(), http.StatusInternalServerError)
logAsError = true
return
}
case protoV3Format:
body, err = types.MarshalProtobufV3(results)
if err != nil {
setError(w, accessLogDetails, err.Error(), http.StatusInternalServerError)
logAsError = true
return
}
case rawFormat:
body = types.MarshalRaw(results)
case csvFormat:
body = types.MarshalCSV(results)
case pickleFormat:
body = types.MarshalPickle(results)
case pngFormat:
body = png.MarshalPNGRequest(r, results, template)
case svgFormat:
body = png.MarshalSVGRequest(r, results, template)
}
accessLogDetails.Metrics = targets
accessLogDetails.CarbonzipperResponseSizeBytes = int64(size)
accessLogDetails.CarbonapiResponseSizeBytes = int64(len(body))
writeResponse(w, returnCode, body, format, jsonp)
if len(results) != 0 {
tc := time.Now()
config.Config.ResponseCache.Set(responseCacheKey, body, responseCacheTimeout)
td := time.Since(tc).Nanoseconds()
ApiMetrics.RenderCacheOverheadNS.Add(td)
}
gotErrors := len(errors) > 0
accessLogDetails.HaveNonFatalErrors = gotErrors
}
func backendCacheComputeKey(from, until string, targets []string) string {
var backendCacheKey bytes.Buffer
backendCacheKey.WriteString("from:")
backendCacheKey.WriteString(from)
backendCacheKey.WriteString(" until:")
backendCacheKey.WriteString(until)
backendCacheKey.WriteString(" targets:")
backendCacheKey.WriteString(strings.Join(targets, ","))
return backendCacheKey.String()
}
func backendCacheFetchResults(logger *zap.Logger, useCache bool, backendCacheKey string, accessLogDetails *carbonapipb.AccessLogDetails) ([]*types.MetricData, error) {
if !useCache {
return nil, errors.New("useCache is false")
}
backendCacheResults, err := config.Config.BackendCache.Get(backendCacheKey)
if err != nil {
return nil, err
}
var results []*types.MetricData
cacheDecodingBuf := bytes.NewBuffer(backendCacheResults)
dec := gob.NewDecoder(cacheDecodingBuf)
err = dec.Decode(&results)
if err != nil {
logger.Error("Error decoding cached backend results")
return nil, err
}
accessLogDetails.UsedBackendCache = true
ApiMetrics.BackendCacheHits.Add(1)
return results, nil
}
func backendCacheStoreResults(logger *zap.Logger, backendCacheKey string, results []*types.MetricData, backendCacheTimeout int32) {
var serializedResults bytes.Buffer
enc := gob.NewEncoder(&serializedResults)
err := enc.Encode(results)
if err != nil {
logger.Error("Error encoding backend results for caching")
return
}
config.Config.BackendCache.Set(backendCacheKey, serializedResults.Bytes(), backendCacheTimeout)
}