-
Notifications
You must be signed in to change notification settings - Fork 569
/
Copy pathpush.go
339 lines (300 loc) · 12.7 KB
/
push.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
// SPDX-License-Identifier: AGPL-3.0-only
// Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/util/push/push.go
// Provenance-includes-license: Apache-2.0
// Provenance-includes-copyright: The Cortex Authors.
package distributor
import (
"context"
"flag"
"fmt"
"math"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/httpgrpc"
"github.com/grafana/dskit/httpgrpc/server"
"github.com/grafana/dskit/middleware"
"github.com/grafana/dskit/tenant"
"github.com/grafana/dskit/user"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/grafana/mimir/pkg/mimirpb"
"github.com/grafana/mimir/pkg/util"
"github.com/grafana/mimir/pkg/util/globalerror"
utillog "github.com/grafana/mimir/pkg/util/log"
"github.com/grafana/mimir/pkg/util/validation"
)
// PushFunc defines the type of the push. It is similar to http.HandlerFunc.
type PushFunc func(ctx context.Context, req *Request) error
// parserFunc defines how to read the body the request from an HTTP request. It takes an optional RequestBuffers.
type parserFunc func(ctx context.Context, r *http.Request, maxSize int, buffers *util.RequestBuffers, req *mimirpb.PreallocWriteRequest, logger log.Logger) error
var (
errNonPositiveMinBackoffDuration = errors.New("min-backoff should be greater than or equal to 1s")
errNonPositiveMaxBackoffDuration = errors.New("max-backoff should be greater than or equal to 1s")
)
const (
SkipLabelNameValidationHeader = "X-Mimir-SkipLabelNameValidation"
SkipLabelCountValidationHeader = "X-Mimir-SkipLabelCountValidation"
statusClientClosedRequest = 499
)
type RetryConfig struct {
Enabled bool `yaml:"enabled" category:"advanced"`
MinBackoff time.Duration `yaml:"min_backoff" category:"advanced"`
MaxBackoff time.Duration `yaml:"max_backoff" category:"advanced"`
}
// RegisterFlags adds the flags required to config this to the given FlagSet.
func (cfg *RetryConfig) RegisterFlags(f *flag.FlagSet) {
f.BoolVar(&cfg.Enabled, "distributor.retry-after-header.enabled", true, "Enables inclusion of the Retry-After header in the response: true includes it for client retry guidance, false omits it.")
f.DurationVar(&cfg.MinBackoff, "distributor.retry-after-header.min-backoff", 6*time.Second, "Minimum duration of the Retry-After HTTP header in responses to 429/5xx errors. Must be greater than or equal to 1s. Backoff is calculated as MinBackoff*2^(RetryAttempt-1) seconds with random jitter of 50% in either direction. RetryAttempt is the value of the Retry-Attempt HTTP header.")
f.DurationVar(&cfg.MaxBackoff, "distributor.retry-after-header.max-backoff", 96*time.Second, "Minimum duration of the Retry-After HTTP header in responses to 429/5xx errors. Must be greater than or equal to 1s. Backoff is calculated as MinBackoff*2^(RetryAttempt-1) seconds with random jitter of 50% in either direction. RetryAttempt is the value of the Retry-Attempt HTTP header.")
}
func (cfg *RetryConfig) Validate() error {
if cfg.MinBackoff < time.Second {
return errNonPositiveMinBackoffDuration
}
if cfg.MaxBackoff < time.Second {
return errNonPositiveMaxBackoffDuration
}
return nil
}
// Handler is a http.Handler which accepts WriteRequests.
func Handler(
maxRecvMsgSize int,
requestBufferPool util.Pool,
sourceIPs *middleware.SourceIPExtractor,
allowSkipLabelNameValidation bool,
allowSkipLabelCountValidation bool,
limits *validation.Overrides,
retryCfg RetryConfig,
push PushFunc,
pushMetrics *PushMetrics,
logger log.Logger,
) http.Handler {
return handler(maxRecvMsgSize, requestBufferPool, sourceIPs, allowSkipLabelNameValidation, allowSkipLabelCountValidation, limits, retryCfg, push, logger, func(ctx context.Context, r *http.Request, maxRecvMsgSize int, buffers *util.RequestBuffers, req *mimirpb.PreallocWriteRequest, _ log.Logger) error {
protoBodySize, err := util.ParseProtoReader(ctx, r.Body, int(r.ContentLength), maxRecvMsgSize, buffers, req, util.RawSnappy)
if errors.Is(err, util.MsgSizeTooLargeErr{}) {
err = distributorMaxWriteMessageSizeErr{actual: int(r.ContentLength), limit: maxRecvMsgSize}
}
if err != nil {
return err
}
tenantID, err := tenant.TenantID(ctx)
if err != nil {
return err
}
pushMetrics.ObserveUncompressedBodySize(tenantID, float64(protoBodySize))
return nil
})
}
type distributorMaxWriteMessageSizeErr struct {
actual, limit int
}
func (e distributorMaxWriteMessageSizeErr) Error() string {
msgSizeDesc := fmt.Sprintf(" of %d bytes", e.actual)
if e.actual < 0 {
msgSizeDesc = ""
}
return globalerror.DistributorMaxWriteMessageSize.MessageWithPerInstanceLimitConfig(fmt.Sprintf("the incoming push request has been rejected because its message size%s is larger than the allowed limit of %d bytes", msgSizeDesc, e.limit), "distributor.max-recv-msg-size")
}
type distributorMaxOTLPRequestSizeErr struct {
actual, limit int
}
func (e distributorMaxOTLPRequestSizeErr) Error() string {
msgSizeDesc := fmt.Sprintf(" of %d bytes", e.actual)
if e.actual < 0 {
msgSizeDesc = ""
}
return globalerror.DistributorMaxOTLPRequestSize.MessageWithPerInstanceLimitConfig(fmt.Sprintf("the incoming OTLP request has been rejected because its message size%s is larger than the allowed limit of %d bytes", msgSizeDesc, e.limit), maxOTLPRequestSizeFlag)
}
func handler(
maxRecvMsgSize int,
requestBufferPool util.Pool,
sourceIPs *middleware.SourceIPExtractor,
allowSkipLabelNameValidation bool,
allowSkipLabelCountValidation bool,
limits *validation.Overrides,
retryCfg RetryConfig,
push PushFunc,
logger log.Logger,
parser parserFunc,
) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
logger := utillog.WithContext(ctx, logger)
if sourceIPs != nil {
source := sourceIPs.Get(r)
if source != "" {
logger = utillog.WithSourceIPs(source, logger)
}
}
var supplier supplierFunc
isRW2, err := isRemoteWrite2(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
if isRW2 {
supplier = func() (*mimirpb.WriteRequest, func(), error) {
// Return 415 Unsupported Media Type for remote-write v2 requests for now. This is not retryable
// unless the client switches to remote-write v1.
return nil, nil, httpgrpc.Error(http.StatusUnsupportedMediaType, "remote-write v2 is not supported")
}
} else {
supplier = func() (*mimirpb.WriteRequest, func(), error) {
rb := util.NewRequestBuffers(requestBufferPool)
var req mimirpb.PreallocWriteRequest
userID, err := tenant.TenantID(ctx)
if err != nil && !errors.Is(err, user.ErrNoOrgID) { // ignore user.ErrNoOrgID
return nil, nil, errors.Wrap(err, "failed to get tenant ID")
}
// userID might be empty if none was in the ctx, in this case just use the default setting.
if limits.MaxGlobalExemplarsPerUser(userID) == 0 {
// The user is not allowed to send exemplars, so there is no need to unmarshal them.
// Optimization to avoid the allocations required for unmarshaling exemplars.
req.SkipUnmarshalingExemplars = true
}
if err := parser(ctx, r, maxRecvMsgSize, rb, &req, logger); err != nil {
// Check for httpgrpc error, default to client error if parsing failed
if _, ok := httpgrpc.HTTPResponseFromError(err); !ok {
err = httpgrpc.Error(http.StatusBadRequest, err.Error())
}
rb.CleanUp()
return nil, nil, err
}
if allowSkipLabelNameValidation {
req.SkipLabelValidation = req.SkipLabelValidation && r.Header.Get(SkipLabelNameValidationHeader) == "true"
} else {
req.SkipLabelValidation = false
}
if allowSkipLabelCountValidation {
req.SkipLabelCountValidation = req.SkipLabelCountValidation && r.Header.Get(SkipLabelCountValidationHeader) == "true"
} else {
req.SkipLabelCountValidation = false
}
cleanup := func() {
mimirpb.ReuseSlice(req.Timeseries)
rb.CleanUp()
}
return &req.WriteRequest, cleanup, nil
}
}
req := newRequest(supplier)
if err := push(ctx, req); err != nil {
if errors.Is(err, context.Canceled) {
http.Error(w, err.Error(), statusClientClosedRequest)
level.Warn(logger).Log("msg", "push request canceled", "err", err)
return
}
var (
code int
msg string
)
if resp, ok := httpgrpc.HTTPResponseFromError(err); ok {
// This code is needed for a correct handling of errors returned by the supplier function.
// These errors are created by using the httpgrpc package.
code, msg = int(resp.Code), string(resp.Body)
} else {
code = toHTTPStatus(ctx, err, limits)
msg = err.Error()
}
if code != 202 {
// This error message is consistent with error message in OTLP handler, and ingester's ingest-storage pushToStorage method.
msgs := []interface{}{"msg", "detected an error while ingesting Prometheus remote-write request (the request may have been partially ingested)", "httpCode", code, "err", err}
if code/100 == 4 {
msgs = append(msgs, "insight", true)
}
level.Error(logger).Log(msgs...)
}
addHeaders(w, err, r, code, retryCfg)
http.Error(w, validUTF8Message(msg), code)
}
})
}
func isRemoteWrite2(r *http.Request) (bool, error) {
const appProtoContentType = "application/x-protobuf"
contentType := r.Header.Get("Content-Type")
if contentType == "" {
// If the content type is not set, we assume it is remote write v1.
return false, nil
}
parts := strings.Split(contentType, ";")
if parts[0] != appProtoContentType {
return false, fmt.Errorf("expected %v as the first (media) part, got %v content-type", appProtoContentType, contentType)
}
// Parse potential https://www.rfc-editor.org/rfc/rfc9110#parameter
for _, p := range parts[1:] {
pair := strings.Split(p, "=")
if len(pair) != 2 {
return false, fmt.Errorf("as per https://www.rfc-editor.org/rfc/rfc9110#parameter expected parameters to be key-values, got %v in %v content-type", p, contentType)
}
if pair[0] == "proto" {
switch pair[1] {
case "prometheus.WriteRequest":
return false, nil
case "io.prometheus.write.v2.Request":
return true, nil
default:
return false, fmt.Errorf("got %v content type; expected prometheus.WriteRequest or io.prometheus.write.v2.Request", contentType)
}
}
}
// No "proto=" parameter, assuming v1.
return false, nil
}
func calculateRetryAfter(retryAttemptHeader string, minBackoff, maxBackoff time.Duration) string {
const jitterFactor = 0.5
retryAttempt, err := strconv.Atoi(retryAttemptHeader)
// If retry-attempt is not valid, set it to default 1
if err != nil || retryAttempt < 1 {
retryAttempt = 1
}
delaySeconds := minBackoff.Seconds() * math.Pow(2.0, float64(retryAttempt-1))
delaySeconds = min(maxBackoff.Seconds(), delaySeconds)
if jitterAmount := int64(delaySeconds * jitterFactor); jitterAmount > 0 {
// The random jitter can be negative too, so we generate a 2x greater the random number and subtract the jitter.
randomJitter := float64(rand.Int63n(jitterAmount*2+1) - jitterAmount)
delaySeconds += randomJitter
}
// Jitter might have pushed the delaySeconds over maxBackoff or minBackoff, so we need to clamp it again.
delaySeconds = min(maxBackoff.Seconds(), delaySeconds)
delaySeconds = max(minBackoff.Seconds(), delaySeconds)
return strconv.FormatInt(int64(delaySeconds), 10)
}
// toHTTPStatus converts the given error into an appropriate HTTP status corresponding
// to that error, if the error is one of the errors from this package. Otherwise, an
// http.StatusInternalServerError is returned.
func toHTTPStatus(ctx context.Context, pushErr error, limits *validation.Overrides) int {
if errors.Is(pushErr, context.DeadlineExceeded) {
return http.StatusInternalServerError
}
var distributorErr Error
if errors.As(pushErr, &distributorErr) {
serviceOverloadErrorEnabled := false
userID, err := tenant.TenantID(ctx)
if err == nil {
serviceOverloadErrorEnabled = limits.ServiceOverloadStatusCodeOnRateLimitEnabled(userID)
}
return errorCauseToHTTPStatusCode(distributorErr.Cause(), serviceOverloadErrorEnabled)
}
return http.StatusInternalServerError
}
func addHeaders(w http.ResponseWriter, err error, r *http.Request, responseCode int, retryCfg RetryConfig) {
var doNotLogError middleware.DoNotLogError
if errors.As(err, &doNotLogError) {
w.Header().Set(server.DoNotLogErrorHeaderKey, "true")
}
if responseCode == http.StatusTooManyRequests || responseCode/100 == 5 {
if retryCfg.Enabled {
retryAttemptHeader := r.Header.Get("Retry-Attempt")
retrySeconds := calculateRetryAfter(retryAttemptHeader, retryCfg.MinBackoff, retryCfg.MaxBackoff)
w.Header().Set("Retry-After", retrySeconds)
if sp := opentracing.SpanFromContext(r.Context()); sp != nil {
sp.SetTag("retry-after", retrySeconds)
sp.SetTag("retry-attempt", retryAttemptHeader)
}
}
}
}