Skip to content

fix(testutils): Add reset_trace_context() helper to clear active span#112799

Draft
joshuarli wants to merge 1 commit intomasterfrom
fix/testutils-sdk-reset-trace
Draft

fix(testutils): Add reset_trace_context() helper to clear active span#112799
joshuarli wants to merge 1 commit intomasterfrom
fix/testutils-sdk-reset-trace

Conversation

@joshuarli
Copy link
Copy Markdown
Member

Problem

sentry_sdk.isolation_scope() shallow-copies the active span into the new scope. Tests that assert get_trace_id() is None (e.g. when testing code paths that run outside a trace) still see a live trace ID inside an isolation_scope() block because the copied span is non-None.

Change

New file src/sentry/testutils/helpers/sdk.py — exports reset_trace_context(), a context manager that:

  1. Enters sentry_sdk.isolation_scope() to isolate from the outer scope.
  2. Sets scope.span = None to clear the inherited span reference.

Usage:

from sentry.testutils.helpers.sdk import reset_trace_context

with reset_trace_context():
    assert get_trace_id() is None

Test plan

Used by tests/sentry/logging/test_handler.py which asserts that log records emitted outside an active span carry no trace ID.

… 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.
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant