instrumentation: don't override an existing error span status in _set…#4769
Open
WatchTree-19 wants to merge 4 commits into
Open
instrumentation: don't override an existing error span status in _set…#4769WatchTree-19 wants to merge 4 commits into
WatchTree-19 wants to merge 4 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>
herin049
requested changes
Jul 5, 2026
| # 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() |
Contributor
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) |
Contributor
There was a problem hiding this comment.
This doesn't look correct to me, look at the Span implementation in the SDK.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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: