Skip to content

Commit f325411

Browse files
committed
Count outcomes only for root-spans
1 parent 662deba commit f325411

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

sentry_sdk/integrations/opentelemetry/potel_span_processor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ def on_end(self, span):
5959
if is_sentry_span(span):
6060
return
6161

62-
if span.parent and not span.parent.is_remote:
63-
self._children_spans[span.parent.span_id].append(span)
64-
else:
62+
is_root_span = not (span.parent and not span.parent.is_remote)
63+
if is_root_span:
6564
# if have a root span ending, we build a transaction and send it
6665
self._flush_root_span(span)
66+
else:
67+
self._children_spans[span.parent.span_id].append(span)
6768

6869
# TODO-neel-potel not sure we need a clear like JS
6970
def shutdown(self):

sentry_sdk/integrations/opentelemetry/sampler.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,19 @@ def dropped_result(span_context, attributes, sample_rate=None):
5454
if sample_rate:
5555
trace_state = trace_state.add(TRACESTATE_SAMPLE_RATE_KEY, str(sample_rate))
5656

57-
# Tell Sentry why we dropped the transaction/span
58-
client = sentry_sdk.get_client()
59-
if client.monitor and client.monitor.downsample_factor > 0:
60-
reason = "backpressure"
61-
else:
62-
reason = "sample_rate"
57+
is_root_span = not (span_context.is_valid and not span_context.is_remote)
58+
if is_root_span:
59+
# Tell Sentry why we dropped the transaction/root-span
60+
client = sentry_sdk.get_client()
61+
if client.monitor and client.monitor.downsample_factor > 0:
62+
reason = "backpressure"
63+
else:
64+
reason = "sample_rate"
6365

64-
client.transport.record_lost_event(reason, data_category="transaction")
66+
client.transport.record_lost_event(reason, data_category="transaction")
6567

66-
# Only one span (the transaction itself) is discarded, since we did not record any spans here.
67-
client.transport.record_lost_event(reason, data_category="span")
68+
# Only one span (the transaction itself) is discarded, since we did not record any spans here.
69+
client.transport.record_lost_event(reason, data_category="span")
6870

6971
return SamplingResult(
7072
Decision.DROP,

tests/test_monitor.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ def test_monitor_unhealthy(sentry_init):
5656

5757

5858
def test_transaction_uses_downsampled_rate(
59-
sentry_init, capture_record_lost_event_calls, monkeypatch
59+
sentry_init, capture_envelopes, capture_record_lost_event_calls, monkeypatch
6060
):
6161
sentry_init(
6262
traces_sample_rate=1.0,
6363
transport=UnhealthyTestTransport(),
6464
)
6565

66+
envelopes = capture_envelopes()
67+
6668
record_lost_event_calls = capture_record_lost_event_calls()
6769

6870
monitor = sentry_sdk.get_client().monitor
@@ -77,15 +79,32 @@ def test_transaction_uses_downsampled_rate(
7779
assert monitor.downsample_factor == 1
7880

7981
with sentry_sdk.start_transaction(name="foobar") as transaction:
82+
with sentry_sdk.start_span(name="foospan"):
83+
with sentry_sdk.start_span(name="foospan2"):
84+
with sentry_sdk.start_span(name="foospan3"):
85+
...
86+
8087
assert transaction.sampled is False
8188
assert (
8289
transaction.sample_rate == 0.5
8390
) # TODO: this fails until we put the sample_rate in the POTelSpan
8491

92+
assert len(envelopes) == 0
93+
8594
assert Counter(record_lost_event_calls) == Counter(
8695
[
87-
("backpressure", "transaction", None, 1),
88-
("backpressure", "span", None, 1),
96+
(
97+
"backpressure",
98+
"transaction",
99+
None,
100+
1,
101+
),
102+
(
103+
"backpressure",
104+
"span",
105+
None,
106+
1,
107+
), # Only one span (the transaction itself) is counted, since we did not record any spans in the first place.
89108
]
90109
)
91110

0 commit comments

Comments
 (0)