[FEAT] Add ClientErrorsAsSpanErrors config to otelecho middleware #6973
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes: #6975
This PR introduces a
ClientErrorsAsSpanErrors
configuration option to theotelecho
middleware. When enabled, HTTP 4xx and 5xx status codes are treated as span errors (settingotel.status_code=ERROR
anderror=true
in the root span). The default remainsfalse
, 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 withhttp.status_code=401
at/v1/auth/login
, but noerror=true
in the root span. Child spans likevalidateCredentials
haveerror=true
, but the root span doesn’t reflect this.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.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:
ClientErrorsAsSpanErrors
toconfig.go
with aWithClientErrorsAsSpanErrors
setter.echo.go
to setcodes.Error
for 4xx/5xx when enabled.Impact:
ClientErrorsAsSpanErrors=true
, a 401 response at/v1/auth/login
now includesotel.status_code=ERROR
anderror=true
in the root span, making it visible in Jaeger error queries.tags={"error":"true"}
now return 4xx traces as expected.Testing:
Feedback welcome!