-
Notifications
You must be signed in to change notification settings - Fork 451
/
write.go
495 lines (437 loc) · 15.8 KB
/
write.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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package remote
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/m3db/m3/src/cmd/services/m3coordinator/ingest"
"github.com/m3db/m3/src/dbnode/client"
"github.com/m3db/m3/src/metrics/policy"
"github.com/m3db/m3/src/query/api/v1/handler"
"github.com/m3db/m3/src/query/api/v1/handler/prometheus"
"github.com/m3db/m3/src/query/generated/proto/prompb"
"github.com/m3db/m3/src/query/models"
"github.com/m3db/m3/src/query/storage"
"github.com/m3db/m3/src/query/ts"
"github.com/m3db/m3/src/query/util/logging"
"github.com/m3db/m3/src/x/clock"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/m3db/m3/src/x/instrument"
xhttp "github.com/m3db/m3/src/x/net/http"
xsync "github.com/m3db/m3/src/x/sync"
xtime "github.com/m3db/m3/src/x/time"
"github.com/golang/protobuf/proto"
"github.com/uber-go/tally"
"go.uber.org/zap"
)
const (
// PromWriteURL is the url for the prom write handler
PromWriteURL = handler.RoutePrefixV1 + "/prom/remote/write"
// PromWriteHTTPMethod is the HTTP method used with this resource.
PromWriteHTTPMethod = http.MethodPost
// emptyStoragePolicyVar for code readability.
emptyStoragePolicyVar = ""
// defaultForwardingTimeout is the default forwarding timeout.
defaultForwardingTimeout = 15 * time.Second
)
var (
errNoDownsamplerAndWriter = errors.New("no downsampler and writer set")
errNoTagOptions = errors.New("no tag options set")
errNoNowFn = errors.New("no now fn set")
errUnaggregatedStoragePolicySet = errors.New("storage policy should not be set for unaggregated metrics")
)
// PromWriteHandler represents a handler for prometheus write endpoint.
type PromWriteHandler struct {
downsamplerAndWriter ingest.DownsamplerAndWriter
tagOptions models.TagOptions
forwarding PromWriteHandlerForwardingOptions
forwardTimeout time.Duration
forwardHTTPClient *http.Client
forwardingBoundWorkers xsync.WorkerPool
forwardContext context.Context
nowFn clock.NowFn
instrumentOpts instrument.Options
metrics promWriteMetrics
}
// PromWriteHandlerForwardingOptions is the forwarding options for prometheus write handler.
type PromWriteHandlerForwardingOptions struct {
// MaxConcurrency is the max parallel forwarding and if zero will be unlimited.
MaxConcurrency int `yaml:"maxConcurrency"`
Timeout time.Duration `yaml:"timeout"`
Targets []PromWriteHandlerForwardTargetOptions `yaml:"targets"`
}
// PromWriteHandlerForwardTargetOptions is a prometheus write handler forwarder target.
type PromWriteHandlerForwardTargetOptions struct {
// URL of the target to send to.
URL string `yaml:"url"`
// Method defaults to POST if not set.
Method string `yaml:"method"`
}
// NewPromWriteHandler returns a new instance of handler.
func NewPromWriteHandler(
downsamplerAndWriter ingest.DownsamplerAndWriter,
tagOptions models.TagOptions,
forwarding PromWriteHandlerForwardingOptions,
nowFn clock.NowFn,
instrumentOpts instrument.Options,
) (http.Handler, error) {
if downsamplerAndWriter == nil {
return nil, errNoDownsamplerAndWriter
}
if tagOptions == nil {
return nil, errNoTagOptions
}
if nowFn == nil {
return nil, errNoNowFn
}
metrics, err := newPromWriteMetrics(instrumentOpts.MetricsScope())
if err != nil {
return nil, err
}
// Only use a forwarding worker pool if concurrency is bound, otherwise
// if unlimited we just spin up a goroutine for each incoming write.
var forwardingBoundWorkers xsync.WorkerPool
if v := forwarding.MaxConcurrency; v > 0 {
forwardingBoundWorkers = xsync.NewWorkerPool(v)
forwardingBoundWorkers.Init()
}
forwardTimeout := defaultForwardingTimeout
if v := forwarding.Timeout; v > 0 {
forwardTimeout = v
}
forwardHTTPOpts := xhttp.DefaultHTTPClientOptions()
forwardHTTPOpts.DisableCompression = true // Already snappy compressed.
forwardHTTPOpts.RequestTimeout = forwardTimeout
return &PromWriteHandler{
downsamplerAndWriter: downsamplerAndWriter,
tagOptions: tagOptions,
forwarding: forwarding,
forwardTimeout: forwardTimeout,
forwardHTTPClient: xhttp.NewHTTPClient(forwardHTTPOpts),
forwardingBoundWorkers: forwardingBoundWorkers,
forwardContext: context.Background(),
nowFn: nowFn,
metrics: metrics,
instrumentOpts: instrumentOpts,
}, nil
}
type promWriteMetrics struct {
writeSuccess tally.Counter
writeErrorsServer tally.Counter
writeErrorsClient tally.Counter
ingestLatency tally.Histogram
ingestLatencyBuckets tally.DurationBuckets
forwardSuccess tally.Counter
forwardErrors tally.Counter
forwardDropped tally.Counter
forwardLatency tally.Histogram
}
func newPromWriteMetrics(scope tally.Scope) (promWriteMetrics, error) {
upTo1sBuckets, err := tally.LinearDurationBuckets(0, 100*time.Millisecond, 10)
if err != nil {
return promWriteMetrics{}, err
}
upTo10sBuckets, err := tally.LinearDurationBuckets(time.Second, 500*time.Millisecond, 18)
if err != nil {
return promWriteMetrics{}, err
}
upTo60sBuckets, err := tally.LinearDurationBuckets(10*time.Second, 5*time.Second, 11)
if err != nil {
return promWriteMetrics{}, err
}
upTo60mBuckets, err := tally.LinearDurationBuckets(0, 5*time.Minute, 12)
if err != nil {
return promWriteMetrics{}, err
}
upTo60mBuckets = upTo60mBuckets[1:] // Remove the first 0s to get 5 min aligned buckets
upTo6hBuckets, err := tally.LinearDurationBuckets(time.Hour, 30*time.Minute, 12)
if err != nil {
return promWriteMetrics{}, err
}
upTo24hBuckets, err := tally.LinearDurationBuckets(6*time.Hour, time.Hour, 19)
if err != nil {
return promWriteMetrics{}, err
}
upTo24hBuckets = upTo24hBuckets[1:] // Remove the first 6h to get 1 hour aligned buckets
var ingestLatencyBuckets tally.DurationBuckets
ingestLatencyBuckets = append(ingestLatencyBuckets, upTo1sBuckets...)
ingestLatencyBuckets = append(ingestLatencyBuckets, upTo10sBuckets...)
ingestLatencyBuckets = append(ingestLatencyBuckets, upTo60sBuckets...)
ingestLatencyBuckets = append(ingestLatencyBuckets, upTo60mBuckets...)
ingestLatencyBuckets = append(ingestLatencyBuckets, upTo6hBuckets...)
ingestLatencyBuckets = append(ingestLatencyBuckets, upTo24hBuckets...)
var forwardLatencyBuckets tally.DurationBuckets
forwardLatencyBuckets = append(forwardLatencyBuckets, upTo1sBuckets...)
forwardLatencyBuckets = append(forwardLatencyBuckets, upTo10sBuckets...)
forwardLatencyBuckets = append(forwardLatencyBuckets, upTo60sBuckets...)
forwardLatencyBuckets = append(forwardLatencyBuckets, upTo60mBuckets...)
return promWriteMetrics{
writeSuccess: scope.SubScope("write").Counter("success"),
writeErrorsServer: scope.SubScope("write").Tagged(map[string]string{"code": "5XX"}).Counter("errors"),
writeErrorsClient: scope.SubScope("write").Tagged(map[string]string{"code": "4XX"}).Counter("errors"),
ingestLatency: scope.SubScope("ingest").Histogram("latency", ingestLatencyBuckets),
ingestLatencyBuckets: ingestLatencyBuckets,
forwardSuccess: scope.SubScope("forward").Counter("success"),
forwardErrors: scope.SubScope("forward").Counter("errors"),
forwardDropped: scope.SubScope("forward").Counter("dropped"),
forwardLatency: scope.SubScope("forward").Histogram("latency", forwardLatencyBuckets),
}, nil
}
func (h *PromWriteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
req, opts, result, rErr := h.parseRequest(r)
if rErr != nil {
h.metrics.writeErrorsClient.Inc(1)
xhttp.Error(w, rErr.Inner(), rErr.Code())
return
}
// Begin async forwarding.
// NB(r): Be careful about not returning buffers to pool
// if the request bodies ever get pooled until after
// forwarding completes.
if targets := h.forwarding.Targets; len(targets) > 0 {
for _, target := range targets {
target := target // Capture for lambda.
forward := func() {
// Consider propgating baggage without tying
// context to request context in future.
ctx, cancel := context.WithTimeout(h.forwardContext, h.forwardTimeout)
defer cancel()
if err := h.forward(ctx, result, target); err != nil {
h.metrics.forwardErrors.Inc(1)
logger := logging.WithContext(h.forwardContext, h.instrumentOpts)
logger.Error("forward error", zap.Error(err))
return
}
h.metrics.forwardSuccess.Inc(1)
}
spawned := false
if h.forwarding.MaxConcurrency > 0 {
spawned = h.forwardingBoundWorkers.GoIfAvailable(forward)
} else {
go forward()
spawned = true
}
if !spawned {
h.metrics.forwardDropped.Inc(1)
}
}
}
batchErr := h.write(r.Context(), req, opts)
// Record ingestion delay latency
now := h.nowFn()
for _, series := range req.Timeseries {
for _, sample := range series.Samples {
age := now.Sub(storage.PromTimestampToTime(sample.Timestamp))
h.metrics.ingestLatency.RecordDuration(age)
}
}
if batchErr != nil {
var (
errs = batchErr.Errors()
lastRegularErr string
lastBadRequestErr string
numRegular int
numBadRequest int
)
for _, err := range errs {
switch {
case client.IsBadRequestError(err):
numBadRequest++
lastBadRequestErr = err.Error()
case xerrors.IsInvalidParams(err):
numBadRequest++
lastBadRequestErr = err.Error()
default:
numRegular++
lastRegularErr = err.Error()
}
}
var status int
switch {
case numBadRequest == len(errs):
status = http.StatusBadRequest
h.metrics.writeErrorsClient.Inc(1)
default:
status = http.StatusInternalServerError
h.metrics.writeErrorsServer.Inc(1)
}
logger := logging.WithContext(r.Context(), h.instrumentOpts)
logger.Error("write error",
zap.String("remoteAddr", r.RemoteAddr),
zap.Int("httpResponseStatusCode", status),
zap.Int("numRegularErrors", numRegular),
zap.Int("numBadRequestErrors", numBadRequest),
zap.String("lastRegularError", lastRegularErr),
zap.String("lastBadRequestErr", lastBadRequestErr))
var resultErr string
if lastRegularErr != "" {
resultErr = fmt.Sprintf("retryable_errors: count=%d, last=%s",
numRegular, lastRegularErr)
}
if lastBadRequestErr != "" {
var sep string
if lastRegularErr != "" {
sep = ", "
}
resultErr = fmt.Sprintf("%s%sbad_request_errors: count=%d, last=%s",
resultErr, sep, numBadRequest, lastBadRequestErr)
}
xhttp.Error(w, errors.New(resultErr), status)
return
}
h.metrics.writeSuccess.Inc(1)
}
func (h *PromWriteHandler) parseRequest(
r *http.Request,
) (*prompb.WriteRequest, ingest.WriteOptions, prometheus.ParsePromCompressedRequestResult, *xhttp.ParseError) {
var opts ingest.WriteOptions
if v := strings.TrimSpace(r.Header.Get(handler.MetricsTypeHeader)); v != "" {
// Allow the metrics type and storage policies to override
// the default rules and policies if specified.
metricsType, err := storage.ParseMetricsType(v)
if err != nil {
return nil, ingest.WriteOptions{},
prometheus.ParsePromCompressedRequestResult{},
xhttp.NewParseError(err, http.StatusBadRequest)
}
// Ensure ingest options specify we are overriding the
// downsampling rules with zero rules to be applied (so
// only direct writes will be made).
opts.DownsampleOverride = true
opts.DownsampleMappingRules = nil
strPolicy := strings.TrimSpace(r.Header.Get(handler.MetricsStoragePolicyHeader))
switch metricsType {
case storage.UnaggregatedMetricsType:
if strPolicy != emptyStoragePolicyVar {
err := errUnaggregatedStoragePolicySet
return nil, ingest.WriteOptions{},
prometheus.ParsePromCompressedRequestResult{},
xhttp.NewParseError(err, http.StatusBadRequest)
}
default:
parsed, err := policy.ParseStoragePolicy(strPolicy)
if err != nil {
err = fmt.Errorf("could not parse storage policy: %v", err)
return nil, ingest.WriteOptions{},
prometheus.ParsePromCompressedRequestResult{},
xhttp.NewParseError(err, http.StatusBadRequest)
}
// Make sure this specific storage policy is used for the writes.
opts.WriteOverride = true
opts.WriteStoragePolicies = policy.StoragePolicies{
parsed,
}
}
}
result, err := prometheus.ParsePromCompressedRequest(r)
if err != nil {
return nil, ingest.WriteOptions{},
prometheus.ParsePromCompressedRequestResult{}, err
}
var req prompb.WriteRequest
if err := proto.Unmarshal(result.UncompressedBody, &req); err != nil {
return nil, ingest.WriteOptions{},
prometheus.ParsePromCompressedRequestResult{},
xhttp.NewParseError(err, http.StatusBadRequest)
}
return &req, opts, result, nil
}
func (h *PromWriteHandler) write(
ctx context.Context,
r *prompb.WriteRequest,
opts ingest.WriteOptions,
) ingest.BatchError {
iter := newPromTSIter(r.Timeseries, h.tagOptions)
return h.downsamplerAndWriter.WriteBatch(ctx, iter, opts)
}
func (h *PromWriteHandler) forward(
ctx context.Context,
request prometheus.ParsePromCompressedRequestResult,
target PromWriteHandlerForwardTargetOptions,
) error {
method := target.Method
if method == "" {
method = http.MethodPost
}
url := target.URL
req, err := http.NewRequest(method, url, bytes.NewReader(request.CompressedBody))
if err != nil {
return err
}
resp, err := h.forwardHTTPClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
response = []byte(fmt.Sprintf("error reading body: %v", err))
}
return fmt.Errorf("expected status code 2XX: actual=%v, method=%v, url=%v, resp=%s",
resp.StatusCode, method, url, response)
}
return nil
}
func newPromTSIter(timeseries []*prompb.TimeSeries, tagOpts models.TagOptions) *promTSIter {
// Construct the tags and datapoints upfront so that if the iterator
// is reset, we don't have to generate them twice.
var (
tags = make([]models.Tags, 0, len(timeseries))
datapoints = make([]ts.Datapoints, 0, len(timeseries))
)
for _, promTS := range timeseries {
tags = append(tags, storage.PromLabelsToM3Tags(promTS.Labels, tagOpts))
datapoints = append(datapoints, storage.PromSamplesToM3Datapoints(promTS.Samples))
}
return &promTSIter{
idx: -1,
tags: tags,
datapoints: datapoints,
}
}
type promTSIter struct {
idx int
tags []models.Tags
datapoints []ts.Datapoints
}
func (i *promTSIter) Next() bool {
i.idx++
return i.idx < len(i.tags)
}
func (i *promTSIter) Current() (models.Tags, ts.Datapoints, xtime.Unit) {
if len(i.tags) == 0 || i.idx < 0 || i.idx >= len(i.tags) {
return models.EmptyTags(), nil, 0
}
return i.tags[i.idx], i.datapoints[i.idx], xtime.Millisecond
}
func (i *promTSIter) Reset() error {
i.idx = -1
return nil
}
func (i *promTSIter) Error() error {
return nil
}