Skip to content

Commit e850994

Browse files
committed
improvements
1 parent 918609c commit e850994

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

sentry_sdk/traces.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,19 @@ def _end(self, end_timestamp: "Optional[Union[float, datetime]]" = None) -> None
487487
if self._finished:
488488
return
489489

490-
client = sentry_sdk.get_client()
491-
if client.is_active() and client.transport:
492-
logger.debug("Discarding span because sampled = False")
493-
client.transport.record_lost_event(
494-
reason=self._unsampled_reason or "sample_rate",
495-
data_category="span",
496-
quantity=1,
497-
)
490+
if self._unsampled_reason is not None:
491+
client = sentry_sdk.get_client()
492+
if client.is_active() and client.transport:
493+
logger.debug(
494+
f"Discarding span because sampled=False (reason: {self._unsampled_reason})"
495+
)
496+
client.transport.record_lost_event(
497+
reason=self._unsampled_reason,
498+
data_category="span",
499+
quantity=1,
500+
)
498501

499-
if self._scope:
502+
if self._scope and hasattr(self, "_previous_span_on_scope"):
500503
with capture_internal_exceptions():
501504
old_span = self._previous_span_on_scope
502505
del self._previous_span_on_scope

sentry_sdk/tracing_utils.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,21 +1409,21 @@ def _make_sampling_decision(
14091409
if sample_rand is None:
14101410
sample_rand = _generate_sample_rand(propagation_context.trace_id)
14111411

1412-
sampling_context = {
1413-
"name": name,
1414-
"trace_id": propagation_context.trace_id,
1415-
"parent_span_id": propagation_context.parent_span_id,
1416-
"parent_sampled": propagation_context.parent_sampled,
1417-
"attributes": attributes or {},
1418-
}
1419-
14201412
# If there's a traces_sampler, use that; otherwise use traces_sample_rate
14211413
traces_sampler_defined = callable(client.options.get("traces_sampler"))
14221414
if traces_sampler_defined:
1415+
sampling_context = {
1416+
"name": name,
1417+
"trace_id": propagation_context.trace_id,
1418+
"parent_span_id": propagation_context.parent_span_id,
1419+
"parent_sampled": propagation_context.parent_sampled,
1420+
"attributes": dict(attributes) if attributes else {},
1421+
}
1422+
14231423
sample_rate = client.options["traces_sampler"](sampling_context)
14241424
else:
1425-
if sampling_context["parent_sampled"] is not None:
1426-
sample_rate = sampling_context["parent_sampled"]
1425+
if propagation_context.parent_sampled is not None:
1426+
sample_rate = propagation_context.parent_sampled
14271427
else:
14281428
sample_rate = client.options["traces_sample_rate"]
14291429

@@ -1434,26 +1434,22 @@ def _make_sampling_decision(
14341434
return False, None, None, None
14351435

14361436
sample_rate = float(sample_rate)
1437-
1438-
# Adjust sample rate if we're under backpressure
1439-
if client.monitor:
1440-
sample_rate /= 2**client.monitor.downsample_factor
1441-
1442-
outcome: "Optional[str]" = None
1443-
14441437
if not sample_rate:
14451438
if traces_sampler_defined:
14461439
reason = "traces_sampler returned 0 or False"
14471440
else:
14481441
reason = "traces_sample_rate is set to 0"
14491442

14501443
logger.debug(f"[Tracing] Discarding {name} because {reason}")
1451-
if client.monitor and client.monitor.downsample_factor > 0:
1452-
outcome = "backpressure"
1453-
else:
1454-
outcome = "sample_rate"
1444+
return False, 0.0, None, "sample_rate"
1445+
1446+
# Adjust sample rate if we're under backpressure
1447+
if client.monitor:
1448+
sample_rate /= 2**client.monitor.downsample_factor
14551449

1456-
return False, 0.0, None, outcome
1450+
if not sample_rate:
1451+
logger.debug(f"[Tracing] Discarding {name} because backpressure")
1452+
return False, 0.0, None, "backpressure"
14571453

14581454
sampled = sample_rand < sample_rate
14591455

0 commit comments

Comments
 (0)