Skip to content

Commit f66429d

Browse files
committed
Port traces_sample_rate update to potel-base
1 parent 71e22b4 commit f66429d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ def dropped_result(parent_span_context, attributes, sample_rate=None):
6060
elif trace_state.get(TRACESTATE_SAMPLED_KEY) == "deferred":
6161
trace_state = trace_state.update(TRACESTATE_SAMPLED_KEY, "false")
6262

63-
if sample_rate and TRACESTATE_SAMPLE_RATE_KEY not in trace_state:
64-
trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate))
65-
6663
is_root_span = not (
6764
parent_span_context.is_valid and not parent_span_context.is_remote
6865
)
6966
if is_root_span:
67+
# Update the sample rate in the trace state
68+
if sample_rate is not None:
69+
if TRACESTATE_SAMPLE_RATE_KEY in trace_state:
70+
trace_state = trace_state.update(
71+
TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate)
72+
)
73+
else:
74+
trace_state = trace_state.add(
75+
TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate)
76+
)
77+
7078
# Tell Sentry why we dropped the transaction/root-span
7179
client = sentry_sdk.get_client()
7280
if client.monitor and client.monitor.downsample_factor > 0:
@@ -97,7 +105,9 @@ def sampled_result(span_context, attributes, sample_rate):
97105
elif trace_state.get(TRACESTATE_SAMPLED_KEY) == "deferred":
98106
trace_state = trace_state.update(TRACESTATE_SAMPLED_KEY, "true")
99107

100-
if TRACESTATE_SAMPLE_RATE_KEY not in trace_state:
108+
if TRACESTATE_SAMPLE_RATE_KEY in trace_state:
109+
trace_state = trace_state.update(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate))
110+
else:
101111
trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate))
102112

103113
return SamplingResult(
@@ -144,7 +154,7 @@ def should_sample(
144154
if sample_rate > 0:
145155
return sampled_result(parent_span_context, attributes, sample_rate)
146156
else:
147-
return dropped_result(parent_span_context, attributes)
157+
return dropped_result(parent_span_context, attributes, sample_rate)
148158
else:
149159
logger.debug(
150160
f"[Tracing] Ignoring sampled param for non-root span {name}"

0 commit comments

Comments
 (0)