Skip to content

Commit ab3d868

Browse files
committed
fix
1 parent 248306f commit ab3d868

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,22 @@ def should_sample(
195195
sample_rand = parent_sample_rand
196196
else:
197197
lower, upper = _sample_rand_range(parent_sampled, parent_sample_rate)
198-
sample_rand = _generate_sample_rand(trace_id, (lower, upper))
198+
199+
try:
200+
sample_rand = _generate_sample_rand(trace_id, (lower, upper))
201+
except ValueError:
202+
# ValueError is raised if the interval is invalid, i.e. lower >= upper.
203+
# lower >= upper might happen if the incoming trace's sampled flag
204+
# and sample_rate are inconsistent, e.g. sample_rate=0.0 but sampled=True.
205+
# We cannot generate a sensible sample_rand value in this case.
206+
logger.debug(
207+
f"Could not backfill sample_rand, since parent_sampled={parent_sampled} "
208+
f"and sample_rate={sample_rate}."
209+
)
210+
logger.warning(
211+
f"[Tracing] Discarding {name} because of invalid sample rate/sampled."
212+
)
213+
return dropped_result(parent_span_context, attributes)
199214

200215
# Explicit sampled value provided at start_span
201216
custom_sampled = cast(

tests/test_dsc.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ def my_traces_sampler(sampling_context):
176176
}
177177

178178
# We continue the incoming trace and start a new transaction
179-
monkeypatch.setattr(random, "random", lambda: 0.125)
180-
with sentry_sdk.continue_trace(incoming_http_headers):
181-
with sentry_sdk.start_span(name="foo"):
182-
pass
179+
with mock.patch("sentry_sdk.tracing_utils.Random.uniform", return_value=0.125):
180+
with sentry_sdk.continue_trace(incoming_http_headers):
181+
with sentry_sdk.start_span(name="foo"):
182+
pass
183183

184184
assert len(envelopes) == 1
185185

0 commit comments

Comments
 (0)