Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Add ClientErrorsAsSpanErrors config to otelecho middleware #6973

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Oloruntobi1
Copy link

@Oloruntobi1 Oloruntobi1 commented Mar 21, 2025

Closes: #6975

This PR introduces a ClientErrorsAsSpanErrors configuration option to the otelecho middleware. When enabled, HTTP 4xx and 5xx status codes are treated as span errors (setting otel.status_code=ERROR and error=true in the root span). The default remains false, where only 5xx codes are errors, preserving existing behavior.

Motivation:
While debugging with Jaeger, I found that traces with 4xx status codes (e.g., 401 Unauthorized) weren’t tagged as errors in the root span, making them hard to filter. For example:

  • curl 'http://localhost:16686/api/traces?service=jaeger-nl-demo&tags={%22http.method%22:%22POST%22}' returns a trace with http.status_code=401 at /v1/auth/login, but no error=true in the root span. Child spans like validateCredentials have error=true, but the root span doesn’t reflect this.
  • Adding an error filter, like curl 'http://localhost:16686/api/traces?service=jaeger-nl-demo&tags={%22http.method%22:%22POST%22,%22error%22:%22true%22}' or ...?tags={"http.method":"POST","error":"route"}, returns no results because the middleware doesn’t mark 4xx as errors.
  • Even combining filters like http.route=/v1/auth/login with "error":"true" fails for the same reason.

This disconnect complicates debugging, as users expect error filters to catch application-level failures (e.g., authentication errors). The new option lets users opt into treating 4xx as errors, aligning the root span with downstream error signals.

Changes:

  • Added ClientErrorsAsSpanErrors to config.go with a WithClientErrorsAsSpanErrors setter.
  • Modified echo.go to set codes.Error for 4xx/5xx when enabled.
  • Default behavior unchanged (only 5xx are errors).

Impact:

  • With ClientErrorsAsSpanErrors=true, a 401 response at /v1/auth/login now includes otel.status_code=ERROR and error=true in the root span, making it visible in Jaeger error queries.
  • Tested locally with Jaeger; queries like tags={"error":"true"} now return 4xx traces as expected.

Testing:

  • No unit tests added yet—existing status code tests may cover this, but I can add a specific 4xx case if requested.

Feedback welcome!

@Oloruntobi1 Oloruntobi1 requested a review from a team as a code owner March 21, 2025 22:14
Copy link

linux-foundation-easycla bot commented Mar 21, 2025

CLA Signed


The committers listed above are authorized under a signed CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

otelecho: Root spans don’t mark 4xx status codes as errors, complicating Jaeger debugging
1 participant