instrumentation: don't override an existing error span status in _set…#4769
instrumentation: don't override an existing error span status in _set…#4769WatchTree-19 wants to merge 7 commits into
Conversation
…_status _set_status unconditionally calls span.set_status(Status(status)) at the end of the HTTP status handling. The SDK already ignores an UNSET status and keeps an existing OK, but it will replace an existing ERROR status, dropping any description the application or another instrumentation set. On a 4xx/5xx response this overwrote a user-set error description to None. Skip the call when the span already carries an ERROR status so the more specific status is preserved. Successful/redirect responses (UNSET) and spans without a readable status are unaffected. Fixes open-telemetry#3713 Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com>
Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com>
| # An ERROR status already set on the span (e.g. by the application or | ||
| # another instrumentation) must not be overwritten by _set_status, which | ||
| # would drop its description. See issue #3713. | ||
| span = Mock() |
There was a problem hiding this comment.
All of these tests should be rewritten to use real spans and not Mock objects.
| # will replace an existing ERROR, dropping any description set by | ||
| # the application or another instrumentation. Skip in that case so | ||
| # the more specific status is preserved (see issue #3713). | ||
| current_status = getattr(span, "status", None) |
There was a problem hiding this comment.
This doesn't look correct to me, look at the Span implementation in the SDK.
Rewrite the _set_status status-precedence tests to exercise real recording SDK spans instead of Mock objects, asserting on the resulting span.status. Covers: an existing ERROR+description is preserved on a 5xx (open-telemetry#3713), a fresh span records ERROR on 5xx, a 2xx leaves the status UNSET, and an app-set OK is preserved. Addresses review feedback on open-telemetry#4769. Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com>
|
thanks @herin049. tests: rewritten to use real recording SDK spans instead of Mocks (e0a5df5), asserting on the resulting span.status. covers the #3713 case (an app-set ERROR + description survives a 5xx), plus fresh -> ERROR on 5xx, 2xx leaving the status UNSET, and an app-set OK preserved. on the fix, what looks off to you? the SDK Span.set_status already ignores an UNSET status and keeps an existing OK, but for an existing ERROR it just does self._status = status, which replaces it and drops the description. that's the #3713 report: an app-set ERROR + description overwritten to None by the instrumentation's Status(status). the guard only skips that one case, and the new real-span tests cover the behaviour across UNSET/OK/ERROR. reading span.status is the only way to see the current status here, since the trace API doesn't expose a getter on Span (only the SDK ReadableSpan does), and we're already inside is_recording(). if you'd rather not read span.status from this helper, happy to do it another way, what did you have in mind? |
It looks like the |
|
good call - switched to an explicit UNSET check (3d1ba13). the SDK already treats OK as final and ignores a later set_status, so guarding on UNSET is clearer than skipping only ERROR, and it still preserves an app-set ERROR + description on a later 5xx (the #3713 case). real-span tests unchanged, 65 pass. |
per review: the SDK treats OK as final, so checking for UNSET is clearer than skipping only ERROR, and still preserves an app-set ERROR + description. Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com>
Description
_set_status(the shared HTTP semconv helper) ends by unconditionally callingspan.set_status(Status(status)). The SDK already ignores anUNSETstatus and keeps an existingOK, but it still replaces an existingERRORstatus, dropping any description the application or another instrumentation set. On a 4xx/5xx response this overwrote a user-set error description toNone, which is the behaviour reported in #3713.This change reads the span's current status and skips the
set_statuscall when the span already carries anERROR, so the more specific status is preserved. Successful/redirect responses (which map toUNSET) and spans that do not expose a readable status are unaffected, so behaviour is unchanged for the common case.Picking this up after it sat idle for a few months. Thanks @RiyaChaturvedi37 for flagging the approach back in April; happy to hand it back if you are still on it.
Fixes #3713
Type of change
How Has This Been Tested?
Added unit tests in
opentelemetry-instrumentation/tests/test_semconv.py:test_set_status_preserves_existing_error_status: an ERROR status already set on the span is not overwritten on a 500.test_set_status_sets_error_when_status_unset: an ERROR is still recorded when the span status is UNSET.test_set_status_sets_error_when_no_status_attribute: spans without a readable status (getattr->None) are still updated.The full
test_semconv.pysuite passes (64 tests).Does This PR Require a Core Repo Change?
Checklist: