From 01f4ceb2f7feafd440ecf2da33f80af77224d204 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Fri, 2 May 2025 14:40:16 +0200 Subject: [PATCH 1/2] Add logger.debug for sampler decisions for root spans --- sentry_sdk/opentelemetry/sampler.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/opentelemetry/sampler.py b/sentry_sdk/opentelemetry/sampler.py index fb68b644b5..ab3defe3de 100644 --- a/sentry_sdk/opentelemetry/sampler.py +++ b/sentry_sdk/opentelemetry/sampler.py @@ -234,7 +234,7 @@ def should_sample( ) else: logger.debug( - f"[Tracing] Ignoring sampled param for non-root span {name}" + f"[Tracing.Sampler] Ignoring sampled param for non-root span {name}" ) # Check if there is a traces_sampler @@ -264,7 +264,7 @@ def should_sample( # If the sample rate is invalid, drop the span if not is_valid_sample_rate(sample_rate, source=self.__class__.__name__): logger.warning( - f"[Tracing] Discarding {name} because of invalid sample rate." + f"[Tracing.Sampler] Discarding {name} because of invalid sample rate." ) return dropped_result(parent_span_context, attributes) @@ -279,6 +279,11 @@ def should_sample( sampled = sample_rand < Decimal.from_float(sample_rate) if sampled: + if is_root_span: + logger.debug( + f"[Tracing.Sampler] Sampled #{name} with sample_rate: {sample_rate} and sample_rand: {sample_rand}" + ) + return sampled_result( parent_span_context, attributes, @@ -286,6 +291,11 @@ def should_sample( sample_rand=None if sample_rand == parent_sample_rand else sample_rand, ) else: + if is_root_span: + logger.debug( + f"[Tracing.Sampler] Dropped #{name} with sample_rate: {sample_rate} and sample_rand: {sample_rand}" + ) + return dropped_result( parent_span_context, attributes, From 454cafd7d9f32fd8885417cd976421a5632166ef Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Fri, 2 May 2025 15:26:15 +0200 Subject: [PATCH 2/2] Respect parent_sampled decision in propagation_context sentry-trace header --- sentry_sdk/scope.py | 6 +----- sentry_sdk/tracing_utils.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 975ac6fe04..dec8e70e22 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -505,11 +505,7 @@ def get_traceparent(self, *args, **kwargs): # If this scope has a propagation context, return traceparent from there if self._propagation_context is not None: - traceparent = "%s-%s" % ( - self._propagation_context.trace_id, - self._propagation_context.span_id, - ) - return traceparent + return self._propagation_context.to_traceparent() # Fall back to isolation scope's traceparent. It always has one return self.get_isolation_scope().get_traceparent() diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index a323b84199..20b88f1e32 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -432,6 +432,21 @@ def span_id(self, value): # type: (str) -> None self._span_id = value + def to_traceparent(self): + # type: () -> str + if self.parent_sampled is True: + sampled = "1" + elif self.parent_sampled is False: + sampled = "0" + else: + sampled = None + + traceparent = "%s-%s" % (self.trace_id, self.span_id) + if sampled is not None: + traceparent += "-%s" % (sampled,) + + return traceparent + def update(self, other_dict): # type: (Dict[str, Any]) -> None """