Skip to content

instrumentation: don't override an existing error span status in _set…#4769

Open
WatchTree-19 wants to merge 4 commits into
open-telemetry:mainfrom
WatchTree-19:fix-3713-set-status-override
Open

instrumentation: don't override an existing error span status in _set…#4769
WatchTree-19 wants to merge 4 commits into
open-telemetry:mainfrom
WatchTree-19:fix-3713-set-status-override

Conversation

@WatchTree-19

Copy link
Copy Markdown

Description

_set_status (the shared HTTP semconv helper) ends by unconditionally calling span.set_status(Status(status)). The SDK already ignores an UNSET status and keeps an existing OK, but it still replaces 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, which is the behaviour reported in #3713.

This change reads the span's current status and skips the set_status call when the span already carries an ERROR, so the more specific status is preserved. Successful/redirect responses (which map to UNSET) 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

  • Bug fix (non-breaking change which fixes an issue)

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.py suite passes (64 tests).

Does This PR Require a Core Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added

…_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>
@WatchTree-19 WatchTree-19 requested a review from a team as a code owner July 1, 2026 13:23
WatchTree-19 and others added 2 commits July 1, 2026 14:23
Signed-off-by: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com>
@emdneto emdneto moved this to Ready for review in Python PR digest Jul 2, 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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This doesn't look correct to me, look at the Span implementation in the SDK.

@github-project-automation github-project-automation Bot moved this from Ready for review to Reviewed PRs that need fixes in Python PR digest Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Reviewed PRs that need fixes

Development

Successfully merging this pull request may close these issues.

_set_status semconv helper can override span status if already defined

3 participants