Skip to content

Commit 3541762

Browse files
committed
mypy
1 parent 221bed4 commit 3541762

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
156156
- `profiles_sample_rate` and `profiler_mode` were removed from options available via `_experiments`. Use the top-level `profiles_sample_rate` and `profiler_mode` options instead.
157157
- `Transport.capture_event` has been removed. Use `Transport.capture_envelope` instead.
158158
- Function transports are no longer supported. Subclass the `Transport` instead.
159+
- `start_transaction` (`start_span`) no longer takes a `baggage` argument. Use the `continue_trace()` context manager instead to propagate baggage.
159160

160161
### Deprecated
161162

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def should_sample(
197197
lower, upper = _sample_rand_range(parent_sampled, parent_sample_rate)
198198

199199
try:
200-
sample_rand = _generate_sample_rand(trace_id, (lower, upper))
200+
sample_rand = _generate_sample_rand(str(trace_id), (lower, upper))
201201
except ValueError:
202202
# ValueError is raised if the interval is invalid, i.e. lower >= upper.
203203
# lower >= upper might happen if the incoming trace's sampled flag

sentry_sdk/tracing_utils.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -457,25 +457,35 @@ def _fill_sample_rand(self):
457457
458458
This function does nothing if there is no dynamic_sampling_context.
459459
"""
460-
if self.dynamic_sampling_context is None:
460+
if self.dynamic_sampling_context is None or self.baggage is None:
461461
return
462462

463-
try:
464-
sample_rand = Decimal(self.baggage.sentry_items.get("sample_rand"))
465-
except (TypeError, ValueError, InvalidOperation):
466-
sample_rand = None
463+
sentry_baggage = self.baggage.sentry_items
464+
465+
sample_rand = None
466+
if sentry_baggage.get("sample_rand"):
467+
try:
468+
sample_rand = Decimal(sentry_baggage["sample_rand"])
469+
except (ValueError, InvalidOperation):
470+
logger.debug(
471+
f"Failed to convert incoming sample_rand to int: {sample_rand}"
472+
)
467473

468474
if sample_rand is not None and 0 <= sample_rand < 1:
469475
# sample_rand is present and valid, so don't overwrite it
470476
return
471477

472-
# Get the sample rate and compute the transformation that will map the random value
473-
# to the desired range: [0, 1), [0, sample_rate), or [sample_rate, 1).
474-
try:
475-
sample_rate = float(self.baggage.sentry_items.get("sample_rate"))
476-
except (TypeError, ValueError):
477-
sample_rate = None
478+
sample_rate = None
479+
if sentry_baggage.get("sample_rate"):
480+
try:
481+
sample_rate = float(sentry_baggage["sample_rate"])
482+
except (ValueError, KeyError):
483+
logger.debug(
484+
f"Failed to convert incoming sample_rate to float: {sample_rate}"
485+
)
478486

487+
# Compute the transformation that will map the random value
488+
# to the desired range: [0, 1), [0, sample_rate), or [sample_rate, 1).
479489
lower, upper = _sample_rand_range(self.parent_sampled, sample_rate)
480490

481491
try:
@@ -753,7 +763,7 @@ def get_current_span(scope=None):
753763

754764

755765
def _generate_sample_rand(
756-
trace_id, # type: Optional[int]
766+
trace_id, # type: Optional[str]
757767
interval=(0.0, 1.0), # type: tuple[float, float]
758768
):
759769
# type: (...) -> decimal.Decimal

0 commit comments

Comments
 (0)