Skip to content

Commit c017b01

Browse files
committed
Sample on root span level
1 parent fccf50b commit c017b01

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
1111
- The SDK now supports Python 3.7 and higher.
1212
- `sentry_sdk.start_span` now only takes keyword arguments.
1313
- `sentry_sdk.start_span` no longer takes an explicit `span` argument.
14+
- The `sampled` argument of `sentry_sdk.start_span` is only taken into account for root spans (previously known as transactions).
1415
- The `Span()` constructor does not accept a `hub` parameter anymore.
1516
- `Span.finish()` does not accept a `hub` parameter anymore.
1617
- The `Profile()` constructor does not accept a `hub` parameter anymore.

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,28 @@ def should_sample(
120120
if not has_tracing_enabled(client.options):
121121
return dropped_result(parent_span_context, attributes)
122122

123+
has_parent = parent_span_context.is_valid
124+
is_root_span = not has_parent or parent_span_context.is_remote
125+
123126
# Explicit sampled value provided at start_span
124127
if attributes.get(SentrySpanAttribute.CUSTOM_SAMPLED) is not None:
125-
sample_rate = float(attributes[SentrySpanAttribute.CUSTOM_SAMPLED])
126-
if sample_rate > 0:
127-
return sampled_result(parent_span_context, attributes, sample_rate)
128+
if is_root_span:
129+
sample_rate = float(attributes[SentrySpanAttribute.CUSTOM_SAMPLED])
130+
if sample_rate > 0:
131+
return sampled_result(parent_span_context, attributes, sample_rate)
132+
else:
133+
return dropped_result(parent_span_context, attributes)
128134
else:
129-
return dropped_result(parent_span_context, attributes)
135+
logger.debug(
136+
f"[Tracing] Ignoring sampled param for non-root span {name}"
137+
)
130138

131139
sample_rate = None
132140

133141
# Check if there is a traces_sampler
134142
# Traces_sampler is responsible to check parent sampled to have full transactions.
135143
has_traces_sampler = callable(client.options.get("traces_sampler"))
136-
if has_traces_sampler:
144+
if is_root_span and has_traces_sampler:
137145
sampling_context = {
138146
"transaction_context": {
139147
"name": name,
@@ -161,8 +169,7 @@ def should_sample(
161169
return dropped_result(parent_span_context, attributes)
162170

163171
# Down-sample in case of back pressure monitor says so
164-
# TODO: this should only be done for transactions (aka root spans)
165-
if client.monitor:
172+
if is_root_span and client.monitor:
166173
sample_rate /= 2**client.monitor.downsample_factor
167174

168175
# Roll the dice on sample rate

0 commit comments

Comments
 (0)