Skip to content

Commit 4b09dd4

Browse files
(wip): No more separate record_lost_transaction
1 parent 57bcbd2 commit 4b09dd4

File tree

7 files changed

+44
-61
lines changed

7 files changed

+44
-61
lines changed

sentry_sdk/client.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,15 @@ def _prepare_event(
454454
# one of the event/error processors returned None
455455
if event_ is None:
456456
if self.transport:
457+
self.transport.record_lost_event(
458+
"event_processor",
459+
data_category=("transaction" if is_transaction else "error"),
460+
)
457461
if is_transaction:
458-
self.transport.record_lost_transaction(
459-
"event_processor", len(event.get("spans", []))
460-
)
461-
else:
462462
self.transport.record_lost_event(
463463
"event_processor",
464-
data_category="error",
464+
data_category="span",
465+
quantity=spans_before + 1, # +1 for the transaction itself
465466
)
466467
return None
467468

@@ -559,8 +560,13 @@ def _prepare_event(
559560
if new_event is None:
560561
logger.info("before send transaction dropped event")
561562
if self.transport:
562-
self.transport.record_lost_transaction(
563-
reason="before_send", span_count=len(event.get("spans", []))
563+
self.transport.record_lost_event(
564+
reason="before_send", data_category="transaction"
565+
)
566+
self.transport.record_lost_event(
567+
reason="before_send",
568+
data_category="span",
569+
quantity=spans_before + 1, # +1 for the transaction itself
564570
)
565571
else:
566572
spans_delta = spans_before - len(new_event.get("spans", []))

sentry_sdk/tracing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,10 @@ def finish(self, hub=None, end_timestamp=None):
854854
else:
855855
reason = "sample_rate"
856856

857-
# No spans are discarded here because they were never recorded in the first place
858-
client.transport.record_lost_transaction(reason, span_count=0)
857+
client.transport.record_lost_event(reason, data_category="transaction")
859858

859+
# Only one span (the transaction itself) is discarded, since we did not record any spans here.
860+
client.transport.record_lost_event(reason, data_category="span")
860861
return None
861862

862863
if not self.name:

sentry_sdk/transport.py

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,12 @@ def record_lost_event(
144144
extracted from the item, and the values passed for
145145
data_category and quantity are ignored.
146146
147-
data_category="transaction" should use record_lost_transaction,
148-
instead. Items containing a transaction are okay to pass, since
149-
we can get the number of spans from the transaction.
150-
"""
151-
return None
152-
153-
def record_lost_transaction(
154-
self,
155-
reason, # type: str
156-
span_count, # type: int
157-
):
158-
# type: (...) -> None
159-
"""This increments a counter for loss of a transaction and the transaction's spans.
160-
161-
The span_count should only include the number of spans contained in the transaction, EXCLUDING the transaction
162-
itself.
147+
When recording a lost transaction via data_category="transaction",
148+
the calling code should also record the lost spans via this method.
149+
When recording lost spans, `quantity` should be set to the number
150+
of contained spans, plus one for the transaction itself. When
151+
passing an Item containing a transaction via the `item` parameter,
152+
this method automatically records the lost spans.
163153
"""
164154
return None
165155

@@ -254,49 +244,28 @@ def record_lost_event(
254244
if not self.options["send_client_reports"]:
255245
return
256246

257-
if data_category == "transaction":
258-
warnings.warn(
259-
"Use record_lost_transaction for transactions to ensure lost spans are counted.",
260-
stacklevel=2,
261-
)
262-
263247
if item is not None:
264248
data_category = item.data_category
249+
quantity = 1 # If an item is provided, we always count it as 1 (except for attachments, handled below).
265250

266251
if data_category == "transaction":
267-
# Handle transactions through record_lost_transaction.
252+
# Also record the lost spans
268253
event = item.get_transaction_event() or {}
269-
span_count = len(event.get("spans") or [])
270-
self.record_lost_transaction(reason, span_count)
271-
return
254+
255+
# +1 for the transaction itself
256+
span_count = len(event.get("spans") or []) + 1
257+
self.record_lost_event(reason, "span", quantity=span_count)
272258

273259
elif data_category == "attachment":
274260
# quantity of 0 is actually 1 as we do not want to count
275261
# empty attachments as actually empty.
276262
quantity = len(item.get_bytes()) or 1
277263

278-
else:
279-
# Any other item data category should be counted as 1, since the one item corresponds to one event.
280-
quantity = 1
281-
282264
elif data_category is None:
283265
raise TypeError("data category not provided")
284266

285267
self._discarded_events[data_category, reason] += quantity
286268

287-
def record_lost_transaction(
288-
self,
289-
reason, # type: str
290-
span_count, # type: int
291-
): # type: (...) -> None
292-
if not self.options["send_client_reports"]:
293-
return
294-
295-
self._discarded_events["transaction", reason] += 1
296-
self._discarded_events["span", reason] += (
297-
span_count + 1
298-
) # Also count the transaction itself
299-
300269
def _update_rate_limits(self, response):
301270
# type: (urllib3.BaseHTTPResponse) -> None
302271

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ def inner():
253253
calls = []
254254
test_client = sentry_sdk.get_client()
255255

256-
def record_lost_event(reason, data_category=None, item=None):
257-
calls.append((reason, data_category, item))
256+
def record_lost_event(reason, data_category=None, item=None, *, quantity=1):
257+
calls.append((reason, data_category, item, quantity))
258258

259259
monkeypatch.setattr(
260260
test_client.transport, "record_lost_event", record_lost_event

tests/profiler/test_transaction_profiler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def test_profiles_sample_rate(
162162
elif profile_count:
163163
assert record_lost_event_calls == []
164164
else:
165-
assert record_lost_event_calls == [("sample_rate", "profile", None)]
165+
assert record_lost_event_calls == [("sample_rate", "profile", None, 1)]
166166

167167

168168
@pytest.mark.parametrize(
@@ -231,7 +231,7 @@ def test_profiles_sampler(
231231
if profile_count:
232232
assert record_lost_event_calls == []
233233
else:
234-
assert record_lost_event_calls == [("sample_rate", "profile", None)]
234+
assert record_lost_event_calls == [("sample_rate", "profile", None, 1)]
235235

236236

237237
def test_minimum_unique_samples_required(
@@ -260,7 +260,7 @@ def test_minimum_unique_samples_required(
260260
# because we dont leave any time for the profiler to
261261
# take any samples, it should be not be sent
262262
assert len(items["profile"]) == 0
263-
assert record_lost_event_calls == [("insufficient_data", "profile", None)]
263+
assert record_lost_event_calls == [("insufficient_data", "profile", None, 1)]
264264

265265

266266
@pytest.mark.forked

tests/test_basics.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def test_dedupe_event_processor_drop_records_client_report(
570570

571571
assert event["level"] == "error"
572572
assert "exception" in event
573-
assert lost_event_call == ("event_processor", "error", None)
573+
assert lost_event_call == ("event_processor", "error", None, 1)
574574

575575

576576
def test_event_processor_drop_records_client_report(
@@ -602,8 +602,9 @@ def foo(event, hint):
602602
# Using Counter because order of record_lost_event calls does not matter
603603
assert Counter(record_lost_event_calls) == Counter(
604604
[
605-
("event_processor", "error", None),
606-
("event_processor", "transaction", None),
605+
("event_processor", "error", None, 1),
606+
("event_processor", "transaction", None, 1),
607+
("event_processor", "span", None, 1),
607608
]
608609
)
609610

tests/test_monitor.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
from collections import Counter
23
from unittest import mock
34

45
import sentry_sdk
@@ -79,7 +80,12 @@ def test_transaction_uses_downsampled_rate(
7980
assert transaction.sampled is False
8081
assert transaction.sample_rate == 0.5
8182

82-
assert record_lost_event_calls == [("backpressure", "transaction", None)]
83+
assert Counter(record_lost_event_calls) == Counter(
84+
[
85+
("backpressure", "transaction", None, 1),
86+
("backpressure", "span", None, 1),
87+
]
88+
)
8389

8490

8591
def test_monitor_no_thread_on_shutdown_no_errors(sentry_init):

0 commit comments

Comments
 (0)