fix(testutils): Add reset_trace_context() helper to clear active span#112799
Draft
fix(testutils): Add reset_trace_context() helper to clear active span#112799
Conversation
… in tests sentry_sdk.isolation_scope() shallow-copies the active hub/span, so tests that call it still see a non-None span inside the block and get_trace_id() returns a real trace ID instead of None. reset_trace_context() wraps isolation_scope() and additionally sets scope.span = None, giving tests a clean slate for asserting on trace context absence.
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.
Problem
sentry_sdk.isolation_scope()shallow-copies the active span into the new scope. Tests that assertget_trace_id() is None(e.g. when testing code paths that run outside a trace) still see a live trace ID inside anisolation_scope()block because the copied span is non-None.Change
New file
src/sentry/testutils/helpers/sdk.py— exportsreset_trace_context(), a context manager that:sentry_sdk.isolation_scope()to isolate from the outer scope.scope.span = Noneto clear the inherited span reference.Usage:
Test plan
Used by
tests/sentry/logging/test_handler.pywhich asserts that log records emitted outside an active span carry no trace ID.