8000 Respect parent_sampled decision in propagation_context sentry-trace header by sl0thentr0py · Pull Request #4356 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

Respect parent_sampled decision in propagation_context sentry-trace header #4356

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 2 commits into from
May 5, 2025
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
14 changes: 12 additions & 2 deletions sentry_sdk/opentelemetry/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -279,13 +279,23 @@ 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,
sample_rate=sample_rate_to_propagate,
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,
Expand Down
6 changes: 1 addition & 5 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions sentry_sdk/tracing_utils.py
622A
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
Loading
0