Skip to content

Cleanup span is not None checks for TWP #3765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions sentry_sdk/integrations/opentelemetry/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,17 @@ def start_span(self, **kwargs):
return POTelSpan(**kwargs, scope=self)


_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)
_INITIAL_CURRENT_SCOPE = None
_INITIAL_ISOLATION_SCOPE = None


def _setup_initial_scopes():
global _INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)


_setup_initial_scopes()


@contextmanager
Expand Down
8 changes: 3 additions & 5 deletions sentry_sdk/integrations/rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,9 @@ def sentry_patched_handle_exception(self, job, *exc_info, **kwargs):
@ensure_integration_enabled(RqIntegration, old_enqueue_job)
def sentry_patched_enqueue_job(self, job, **kwargs):
# type: (Queue, Any, **Any) -> Any
scope = sentry_sdk.get_current_scope()
if scope.span is not None:
job.meta["_sentry_trace_headers"] = dict(
scope.iter_trace_propagation_headers()
)
job.meta["_sentry_trace_headers"] = dict(
sentry_sdk.get_current_scope().iter_trace_propagation_headers()
)

return old_enqueue_job(self, job, **kwargs)

Expand Down
20 changes: 16 additions & 4 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,11 @@ def get_traceparent(self, *args, **kwargs):
client = self.get_client()

# If we have an active span, return traceparent from there
if has_tracing_enabled(client.options) and self.span is not None:
if (
has_tracing_enabled(client.options)
and self.span is not None
and self.span.is_valid
):
return self.span.to_traceparent()

# If this scope has a propagation context, return traceparent from there
Expand All @@ -521,7 +525,11 @@ def get_baggage(self, *args, **kwargs):
client = self.get_client()

# If we have an active span, return baggage from there
if has_tracing_enabled(client.options) and self.span is not None:
if (
has_tracing_enabled(client.options)
and self.span is not None
and self.span.is_valid
):
return self.span.to_baggage()

# If this scope has a propagation context, return baggage from there
Expand Down Expand Up @@ -610,7 +618,7 @@ def iter_trace_propagation_headers(self, *args, **kwargs):
span = kwargs.pop("span", None)
span = span or self.span

if has_tracing_enabled(client.options) and span is not None:
if has_tracing_enabled(client.options) and span is not None and span.is_valid:
for header in span.iter_headers():
yield header
else:
Expand Down Expand Up @@ -1311,7 +1319,11 @@ def _apply_contexts_to_event(self, event, hint, options):

# Add "trace" context
if contexts.get("trace") is None:
if has_tracing_enabled(options) and self._span is not None:
if (
has_tracing_enabled(options)
and self._span is not None
and self._span.is_valid
):
contexts["trace"] = self._span.get_trace_context()
else:
contexts["trace"] = self.get_trace_context()
Expand Down
5 changes: 5 additions & 0 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,11 @@ def span_id(self):
# type: () -> str
return format_span_id(self._otel_span.get_span_context().span_id)

@property
def is_valid(self):
# type: () -> bool
return self._otel_span.get_span_context().is_valid

@property
def sampled(self):
# type: () -> Optional[bool]
Expand Down
3 changes: 1 addition & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def clean_scopes():
scope._isolation_scope.set(None)
scope._current_scope.set(None)

potel_scope._INITIAL_CURRENT_SCOPE.clear()
potel_scope._INITIAL_ISOLATION_SCOPE.clear()
potel_scope._setup_initial_scopes()


@pytest.fixture(autouse=True)
Expand Down
Loading