Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .chloggen/13414-otlp-receiver-error-handler.yaml
Original file line number Diff line number Diff line change
@@ -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]
29 changes: 29 additions & 0 deletions receiver/otlpreceiver/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,35 @@ func TestOTLPReceiverInvalidContentEncoding(t *testing.T) {
}
}

func TestOTLPReceiverNoContentType(t *testing.T) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you'd prefer to add this to an existing test or test it differently. Bad encoding is a simple way to trigger an error, but TestOTLPReceioverInvalidContentEncoding tests for different things.

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)
Expand Down
6 changes: 5 additions & 1 deletion receiver/otlpreceiver/otlphttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading