Skip to content

Commit 7107521

Browse files
committed
fix(distributor): relax content-type check
We used to allow content type == text/plain for Prometheus remote write v1 push. Let's continue to not impact users. Signed-off-by: György Krajcsovits <[email protected]>
1 parent dc13127 commit 7107521

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
* [BUGFIX] MQE: Fix deriv with histograms #10383
8484
* [BUGFIX] PromQL: Fix <aggr_over_time> functions with histograms https://github.com/prometheus/prometheus/pull/15711 #10400
8585
* [BUGFIX] MQE: Fix <aggr_over_time> functions with histograms #10400
86-
* [BUGFIX] Distributor: return HTTP status 415 Unsupported Media Type instead of 200 Success for Remote Write 2.0 until we support it. #10423
86+
* [BUGFIX] Distributor: return HTTP status 415 Unsupported Media Type instead of 200 Success for Remote Write 2.0 until we support it. #10423 #10916
8787
* [BUGFIX] Query-frontend: Add flag `-query-frontend.prom2-range-compat` and corresponding YAML to rewrite queries with ranges that worked in Prometheus 2 but are invalid in Prometheus 3. #10445 #10461 #10502
8888
* [BUGFIX] Distributor: Fix edge case at the HA-tracker with memberlist as KVStore, where when a replica in the KVStore is marked as deleted but not yet removed, it fails to update the KVStore. #10443
8989
* [BUGFIX] Distributor: Fix panics in `DurationWithJitter` util functions when computed variance is zero. #10507

pkg/distributor/push.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ func isRemoteWrite2(r *http.Request) (bool, error) {
251251
}
252252
parts := strings.Split(contentType, ";")
253253
if parts[0] != appProtoContentType {
254-
return false, fmt.Errorf("expected %v as the first (media) part, got %v content-type", appProtoContentType, contentType)
254+
// We didn't use to check the content type so if someone wrote for
255+
// example text/plain here, we'll accept and assume it is v1.
256+
return false, nil
255257
}
256258

257259
// Parse potential https://www.rfc-editor.org/rfc/rfc9110#parameter

pkg/distributor/push_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ func TestHandler_remoteWrite(t *testing.T) {
5757
assert.Equal(t, 200, resp.Code)
5858
}
5959

60+
func TestHandler_remoteWriteWithMalformedRequest(t *testing.T) {
61+
req := createMalformedRW1Request(t, createPrometheusRemoteWriteProtobuf(t))
62+
resp := httptest.NewRecorder()
63+
handler := Handler(100000, nil, nil, false, false, validation.MockDefaultOverrides(), RetryConfig{}, verifyWritePushFunc(t, mimirpb.API), nil, log.NewNopLogger())
64+
handler.ServeHTTP(resp, req)
65+
assert.Equal(t, 200, resp.Code)
66+
}
67+
6068
func TestOTelMetricsToMetadata(t *testing.T) {
6169
otelMetrics := pmetric.NewMetrics()
6270
rs := otelMetrics.ResourceMetrics().AppendEmpty()
@@ -525,6 +533,22 @@ func createRequest(t testing.TB, protobuf []byte) *http.Request {
525533
return req
526534
}
527535

536+
func createMalformedRW1Request(t testing.TB, protobuf []byte) *http.Request {
537+
t.Helper()
538+
inoutBytes := snappy.Encode(nil, protobuf)
539+
req, err := http.NewRequest("POST", "http://localhost/", bytes.NewReader(inoutBytes))
540+
require.NoError(t, err)
541+
req.Header.Add("Content-Encoding", "snappy")
542+
req.Header.Set("Content-Type", "text/plain")
543+
544+
const tenantID = "test"
545+
req.Header.Set("X-Scope-OrgID", tenantID)
546+
ctx := user.InjectOrgID(context.Background(), tenantID)
547+
req = req.WithContext(ctx)
548+
549+
return req
550+
}
551+
528552
func createPrometheusRemoteWriteProtobuf(t testing.TB) []byte {
529553
t.Helper()
530554
input := prompb.WriteRequest{

0 commit comments

Comments
 (0)