From 28be6b63251551bd5ce63357e21eee5ecdf3b23a Mon Sep 17 00:00:00 2001 From: Marko Bakovic Date: Thu, 17 Jul 2025 15:31:53 +0100 Subject: [PATCH 1/5] Fallback to content type in error handler (#1) --- receiver/otlpreceiver/otlphttp.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/receiver/otlpreceiver/otlphttp.go b/receiver/otlpreceiver/otlphttp.go index e719cd58eaf..2b0f7b106c2 100644 --- a/receiver/otlpreceiver/otlphttp.go +++ b/receiver/otlpreceiver/otlphttp.go @@ -195,7 +195,11 @@ func writeError(w http.ResponseWriter, encoder encoder, err error, statusCode in // by the OTLP protocol. func errorHandler(w http.ResponseWriter, r *http.Request, errMsg string, statusCode int) { s := statusutil.NewStatusFromMsgAndHTTPCode(errMsg, statusCode) - switch getMimeTypeFromContentType(r.Header.Get("Content-Type")) { + contentType := r.Header.Get("Content-Type") + if contentType == "" { + contentType = fallbackContentType + } + switch getMimeTypeFromContentType(contentType) { case pbContentType: writeStatusResponse(w, pbEncoder, statusCode, s) return From 90d0367cc99839d5de38b7fbc9526e7814c34989 Mon Sep 17 00:00:00 2001 From: Marko Bakovic Date: Thu, 17 Jul 2025 16:47:00 +0100 Subject: [PATCH 2/5] add changelog --- .chloggen/otlp-receiver-error-handler.yml | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .chloggen/otlp-receiver-error-handler.yml diff --git a/.chloggen/otlp-receiver-error-handler.yml b/.chloggen/otlp-receiver-error-handler.yml new file mode 100644 index 00000000000..1ae89d47218 --- /dev/null +++ b/.chloggen/otlp-receiver-error-handler.yml @@ -0,0 +1,25 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: otlpreceiver + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Error handler correctly fallbacks to content type + +# One or more tracking issues or pull requests related to the change +issues: [] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [user] From 186c46d72332b52cff7c0a0adc938498cdad361c Mon Sep 17 00:00:00 2001 From: Marko Bakovic Date: Thu, 17 Jul 2025 16:52:15 +0100 Subject: [PATCH 3/5] Update .chloggen/otlp-receiver-error-handler.yml Co-authored-by: Yang Song --- .chloggen/otlp-receiver-error-handler.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/otlp-receiver-error-handler.yml b/.chloggen/otlp-receiver-error-handler.yml index 1ae89d47218..701894c6932 100644 --- a/.chloggen/otlp-receiver-error-handler.yml +++ b/.chloggen/otlp-receiver-error-handler.yml @@ -10,7 +10,7 @@ component: otlpreceiver note: Error handler correctly fallbacks to content type # One or more tracking issues or pull requests related to the change -issues: [] +issues: [13414] # (Optional) One or more lines of additional information to render under the primary note. # These lines will be padded with 2 spaces and then inserted directly into the document. From 0ac5b18c00a8955e5a6b0489becbd3c9e36951e4 Mon Sep 17 00:00:00 2001 From: Marko Bakovic Date: Thu, 17 Jul 2025 16:58:50 +0100 Subject: [PATCH 4/5] This commit will be squashed. --- ...r-error-handler.yml => 13414-otlp-receiver-error-handler.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .chloggen/{otlp-receiver-error-handler.yml => 13414-otlp-receiver-error-handler.yaml} (100%) diff --git a/.chloggen/otlp-receiver-error-handler.yml b/.chloggen/13414-otlp-receiver-error-handler.yaml similarity index 100% rename from .chloggen/otlp-receiver-error-handler.yml rename to .chloggen/13414-otlp-receiver-error-handler.yaml From 3fef80b18fc78956413a8a34bd49f7d323156a04 Mon Sep 17 00:00:00 2001 From: Marko Bakovic Date: Thu, 17 Jul 2025 21:30:09 +0100 Subject: [PATCH 5/5] add test --- receiver/otlpreceiver/otlp_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/receiver/otlpreceiver/otlp_test.go b/receiver/otlpreceiver/otlp_test.go index 6180eafaade..e2f0d37d1e0 100644 --- a/receiver/otlpreceiver/otlp_test.go +++ b/receiver/otlpreceiver/otlp_test.go @@ -495,6 +495,35 @@ func TestOTLPReceiverInvalidContentEncoding(t *testing.T) { } } +func TestOTLPReceiverNoContentType(t *testing.T) { + addr := testutil.GetAvailableLocalAddress(t) + + // Set the buffer count to 1 to make it flush the test span immediately. + recv := newHTTPReceiver(t, componenttest.NewNopTelemetrySettings(), addr, consumertest.NewNop()) + + require.NoError(t, recv.Start(context.Background(), componenttest.NewNopHost()), "Failed to start trace receiver") + t.Cleanup(func() { require.NoError(t, recv.Shutdown(context.Background())) }) + + url := fmt.Sprintf("http://%s%s", addr, defaultTracesURLPath) + + t.Run("NoContentType", func(t *testing.T) { + body := bytes.NewBuffer([]byte(`{"key": "value"}`)) + + req, err := http.NewRequest(http.MethodPost, url, body) + require.NoError(t, err, "Error creating trace POST request: %v", err) + + // Set invalid encoding to trigger an error + req.Header.Set("Content-Encoding", "invalid") + + resp, err := http.DefaultClient.Do(req) + require.NoError(t, err, "Error posting to server: %v", err) + // Don't care about the response body, just check the content type + defer resp.Body.Close() + + require.Equal(t, fallbackContentType, resp.Header.Get("Content-Type"), "Unexpected response Content-Type") + }) +} + func TestGRPCNewPortAlreadyUsed(t *testing.T) { addr := testutil.GetAvailableLocalAddress(t) ln, err := net.Listen("tcp", addr)