Skip to content

Commit

Permalink
Merge branch 'potel-base' into ivana/custom-sampling-context-integrat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
sentrivana committed Nov 13, 2024
2 parents 52f918a + fccf50b commit 6b063cf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
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
2 changes: 2 additions & 0 deletions tests/integrations/starlette/test_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,7 @@ def test_transaction_http_method_default(sentry_init, capture_events):
"""
sentry_init(
traces_sample_rate=1.0,
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
integrations=[StarletteIntegration()],
)
events = capture_events()
Expand All @@ -1269,6 +1270,7 @@ def test_transaction_http_method_default(sentry_init, capture_events):
def test_transaction_http_method_custom(sentry_init, capture_events):
sentry_init(
traces_sample_rate=1.0,
auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request.
integrations=[
StarletteIntegration(
http_methods_to_capture=(
Expand Down

0 comments on commit 6b063cf

Please sign in to comment.