diff --git a/.chloggen/13414-otlp-receiver-error-handler.yaml b/.chloggen/13414-otlp-receiver-error-handler.yaml new file mode 100644 index 00000000000..701894c6932 --- /dev/null +++ b/.chloggen/13414-otlp-receiver-error-handler.yaml @@ -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: [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. +# 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] diff --git a/receiver/otlpreceiver/otlp_test.go b/receiver/otlpreceiver/otlp_test.go index 269266bf187..b0d08922f21 100644 --- a/receiver/otlpreceiver/otlp_test.go +++ b/receiver/otlpreceiver/otlp_test.go @@ -496,6 +496,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) 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