Skip to content

Commit 4c8dfff

Browse files
(wip) test: No more separate record_lost_transaction
1 parent 3ad116d commit 4c8dfff

File tree

2 files changed

+74
-141
lines changed

2 files changed

+74
-141
lines changed

tests/test_client.py

+62-96
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import contextlib
21
import os
32
import json
43
import subprocess
@@ -1223,48 +1222,6 @@ class TestSpanClientReports:
12231222
Tests for client reports related to spans.
12241223
"""
12251224

1226-
class LostEventCapturingTransport(sentry_sdk.Transport):
1227-
"""
1228-
A transport that captures lost events.
1229-
"""
1230-
1231-
def __init__(self):
1232-
self.record_lost_event_calls = []
1233-
self.record_lost_transaction_calls = []
1234-
1235-
def capture_envelope(self, _):
1236-
pass
1237-
1238-
def record_lost_event(
1239-
self,
1240-
reason,
1241-
data_category=None,
1242-
item=None,
1243-
*,
1244-
quantity=1,
1245-
):
1246-
self.record_lost_event_calls.append((reason, data_category, item, quantity))
1247-
1248-
def record_lost_transaction(
1249-
self,
1250-
reason, # type: str
1251-
span_count, # type: int
1252-
): # type: (...) -> None
1253-
self.record_lost_transaction_calls.append((reason, span_count))
1254-
1255-
@staticmethod
1256-
@contextlib.contextmanager
1257-
def patch_transport():
1258-
"""Patches the transport with a new LostEventCapturingTransport, which we yield."""
1259-
old_transport = sentry_sdk.get_client().transport
1260-
new_transport = TestSpanClientReports.LostEventCapturingTransport()
1261-
sentry_sdk.get_client().transport = new_transport
1262-
1263-
try:
1264-
yield new_transport
1265-
finally:
1266-
sentry_sdk.get_client().transport = old_transport
1267-
12681225
@staticmethod
12691226
def span_dropper(spans_to_drop):
12701227
"""
@@ -1293,23 +1250,19 @@ def __init__(self, span_count):
12931250
"""Configures a test case with the number of spans dropped and whether the transaction was dropped."""
12941251
self.span_count = span_count
12951252
self.expected_record_lost_event_calls = Counter()
1296-
self.expected_record_lost_transaction_calls = Counter()
12971253
self.before_send = lambda event, _: event
12981254
self.event_processor = lambda event, _: event
1299-
self.already_dropped_spans = 0
13001255

1301-
def _update_resulting_calls(
1302-
self, reason, drops_transaction=False, drops_spans=None
1303-
):
1256+
def _update_resulting_calls(self, reason, drops_transactions=0, drops_spans=0):
13041257
"""
13051258
Updates the expected calls with the given resulting calls.
13061259
"""
1307-
if drops_transaction:
1308-
dropped_spans = self.span_count - self.already_dropped_spans
1309-
self.expected_record_lost_transaction_calls[(reason, dropped_spans)] += 1
1260+
if drops_transactions > 0:
1261+
self.expected_record_lost_event_calls[
1262+
(reason, "transaction", None, drops_transactions)
1263+
] += 1
13101264

1311-
elif drops_spans is not None:
1312-
self.already_dropped_spans += drops_spans
1265+
if drops_spans > 0:
13131266
self.expected_record_lost_event_calls[
13141267
(reason, "span", None, drops_spans)
13151268
] += 1
@@ -1318,14 +1271,13 @@ def with_before_send(
13181271
self,
13191272
before_send,
13201273
*,
1321-
drops_transaction=False,
1322-
drops_spans=None,
1274+
drops_transactions=0,
1275+
drops_spans=0,
13231276
):
1324-
"""drops_transaction and drops_spans are mutually exclusive."""
13251277
self.before_send = before_send
13261278
self._update_resulting_calls(
13271279
"before_send",
1328-
drops_transaction,
1280+
drops_transactions,
13291281
drops_spans,
13301282
)
13311283

@@ -1335,73 +1287,87 @@ def with_event_processor(
13351287
self,
13361288
event_processor,
13371289
*,
1338-
drops_transaction=False,
1339-
drops_spans=None,
1290+
drops_transactions=0,
1291+
drops_spans=0,
13401292
):
13411293
self.event_processor = event_processor
13421294
self._update_resulting_calls(
13431295
"event_processor",
1344-
drops_transaction,
1296+
drops_transactions,
13451297
drops_spans,
13461298
)
13471299

13481300
return self
13491301

1350-
def run(self):
1302+
def run(self, sentry_init, capture_record_lost_event_calls):
13511303
"""Runs the test case with the configured parameters."""
1352-
sentry_sdk.init(before_send_transaction=self.before_send)
1304+
sentry_init(before_send_transaction=self.before_send)
1305+
record_lost_event_calls = capture_record_lost_event_calls()
13531306

13541307
with sentry_sdk.isolation_scope() as scope:
13551308
scope.add_event_processor(self.event_processor)
1356-
with self.patch_transport() as transport:
1357-
event = self.mock_transaction_event(self.span_count)
1358-
sentry_sdk.get_client().capture_event(event, scope=scope)
1309+
event = self.mock_transaction_event(self.span_count)
1310+
sentry_sdk.get_client().capture_event(event, scope=scope)
13591311

13601312
# We use counters to ensure that the calls are made the expected number of times, disregarding order.
1361-
assert (
1362-
Counter(transport.record_lost_event_calls)
1363-
== self.expected_record_lost_event_calls
1364-
)
1365-
assert (
1366-
Counter(transport.record_lost_transaction_calls)
1367-
== self.expected_record_lost_transaction_calls
1368-
)
1313+
assert Counter(record_lost_event_calls) == self.expected_record_lost_event_calls
13691314

13701315

13711316
@pytest.mark.parametrize(
13721317
"test_config",
13731318
(
1374-
TestSpanClientReports(10), # No spans dropped
1375-
TestSpanClientReports(0).with_before_send(
1376-
lambda e, _: None, drops_transaction=True
1319+
TestSpanClientReports(span_count=10), # No spans dropped
1320+
TestSpanClientReports(span_count=0).with_before_send(
1321+
lambda e, _: None,
1322+
drops_transactions=1,
1323+
drops_spans=1,
13771324
),
1378-
TestSpanClientReports(10).with_before_send(
1379-
lambda e, _: None, drops_transaction=True
1325+
TestSpanClientReports(span_count=10).with_before_send(
1326+
lambda e, _: None,
1327+
drops_transactions=1,
1328+
drops_spans=11,
13801329
),
1381-
TestSpanClientReports(10).with_before_send(
1382-
TestSpanClientReports.span_dropper(3), drops_spans=3
1330+
TestSpanClientReports(span_count=10).with_before_send(
1331+
TestSpanClientReports.span_dropper(3),
1332+
drops_spans=3,
13831333
),
1384-
TestSpanClientReports(10).with_before_send(
1385-
TestSpanClientReports.span_dropper(10), drops_spans=10
1334+
TestSpanClientReports(span_count=10).with_before_send(
1335+
TestSpanClientReports.span_dropper(10),
1336+
drops_spans=10,
13861337
),
1387-
TestSpanClientReports(10).with_event_processor(
1388-
lambda e, _: None, drops_transaction=True
1338+
TestSpanClientReports(span_count=10).with_event_processor(
1339+
lambda e, _: None,
1340+
drops_transactions=1,
1341+
drops_spans=11,
13891342
),
1390-
TestSpanClientReports(10).with_event_processor(
1391-
TestSpanClientReports.span_dropper(3), drops_spans=3
1343+
TestSpanClientReports(span_count=10).with_event_processor(
1344+
TestSpanClientReports.span_dropper(3),
1345+
drops_spans=3,
13921346
),
1393-
TestSpanClientReports(10).with_event_processor(
1394-
TestSpanClientReports.span_dropper(10), drops_spans=10
1347+
TestSpanClientReports(span_count=10).with_event_processor(
1348+
TestSpanClientReports.span_dropper(10),
1349+
drops_spans=10,
1350+
),
1351+
TestSpanClientReports(span_count=10)
1352+
.with_event_processor(
1353+
TestSpanClientReports.span_dropper(3),
1354+
drops_spans=3,
1355+
)
1356+
.with_before_send(
1357+
TestSpanClientReports.span_dropper(5),
1358+
drops_spans=5,
13951359
),
13961360
TestSpanClientReports(10)
1397-
.with_event_processor(TestSpanClientReports.span_dropper(3), drops_spans=3)
1398-
.with_before_send(TestSpanClientReports.span_dropper(5), drops_spans=5),
1399-
TestSpanClientReports(10)
1400-
.with_event_processor(TestSpanClientReports.span_dropper(3), drops_spans=3)
1361+
.with_event_processor(
1362+
TestSpanClientReports.span_dropper(3),
1363+
drops_spans=3,
1364+
)
14011365
.with_before_send(
1402-
lambda e, _: None, drops_transaction=True
1403-
), # Test proper number of spans with each reason
1366+
lambda e, _: None,
1367+
drops_transactions=1,
1368+
drops_spans=8, # 3 of the 11 (incl. transaction) spans already dropped
1369+
),
14041370
),
14051371
)
1406-
def test_dropped_transaction(test_config):
1407-
test_config.run()
1372+
def test_dropped_transaction(sentry_init, capture_record_lost_event_calls, test_config):
1373+
test_config.run(sentry_init, capture_record_lost_event_calls)

tests/test_transport.py

+12-45
Original file line numberDiff line numberDiff line change
@@ -645,14 +645,6 @@ class TestCustomHubClass(sentry_sdk.Hub):
645645
assert transport.hub_cls is TestCustomHubClass
646646

647647

648-
def test_record_lost_event_transaction_warning():
649-
transport = http_transport(send_client_reports=True)
650-
651-
# Ensure a warning related to lost transaction events is emitted
652-
with pytest.warns(UserWarning, match=r".+record_lost_transaction.+"):
653-
transport.record_lost_event(reason="foo", data_category="transaction")
654-
655-
656648
@pytest.mark.parametrize("quantity", (1, 2, 10))
657649
def test_record_lost_event_quantity(capturing_server, make_client, quantity):
658650
client = make_client()
@@ -674,11 +666,17 @@ def test_record_lost_event_quantity(capturing_server, make_client, quantity):
674666
]
675667

676668

677-
def assert_transaction_report(capturing_server, span_quantity):
678-
"""
679-
Convenience function to assert that the capturing server captured a client report with 1 transaction and
680-
`span_quantity` spans.
681-
"""
669+
@pytest.mark.parametrize("span_count", (0, 1, 2, 10))
670+
def test_record_lost_event_transaction_item(capturing_server, make_client, span_count):
671+
client = make_client()
672+
transport = client.transport
673+
674+
envelope = mock_transaction_envelope(span_count)
675+
(transaction_item,) = envelope.items
676+
677+
transport.record_lost_event(reason="test", item=transaction_item)
678+
client.flush()
679+
682680
(captured,) = capturing_server.captured # Should only be one envelope
683681
envelope = captured.envelope
684682
(item,) = envelope.items # Envelope should only have one item
@@ -699,36 +697,5 @@ def assert_transaction_report(capturing_server, span_quantity):
699697
assert {
700698
"category": "span",
701699
"reason": "test",
702-
"quantity": span_quantity,
700+
"quantity": span_count + 1,
703701
} in discarded_events
704-
705-
706-
@pytest.mark.parametrize("span_count", (0, 1, 2, 10))
707-
def test_record_lost_transaction(capturing_server, make_client, span_count):
708-
client = make_client()
709-
transport = client.transport
710-
711-
transport.record_lost_transaction(reason="test", span_count=span_count)
712-
client.flush()
713-
714-
assert_transaction_report(
715-
capturing_server,
716-
span_quantity=span_count + 1, # +1 for the transaction itself
717-
)
718-
719-
720-
@pytest.mark.parametrize("span_count", (0, 1, 2, 10))
721-
def test_record_lost_event_transaction_item(capturing_server, make_client, span_count):
722-
client = make_client()
723-
transport = client.transport
724-
725-
envelope = mock_transaction_envelope(span_count)
726-
(transaction_item,) = envelope.items
727-
728-
transport.record_lost_event(reason="test", item=transaction_item)
729-
client.flush()
730-
731-
assert_transaction_report(
732-
capturing_server,
733-
span_quantity=span_count + 1, # +1 for the transaction itself
734-
)

0 commit comments

Comments
 (0)