Skip to content

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

Open
WatchTree-19 wants to merge 7 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 7 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
herin049 and others added 2 commits July 4, 2026 21:39
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>
@WatchTree-19

Copy link
Copy Markdown
Author

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?

@herin049

herin049 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

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 Span set_status method will be a no-op if the status is already set to OK. It's probably best to change the check to explicitly check if the status is UNSET.

@WatchTree-19

WatchTree-19 commented Jul 7, 2026

Copy link
Copy Markdown
Author

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>
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