1
- import contextlib
2
1
import os
3
2
import json
4
3
import subprocess
@@ -1223,48 +1222,6 @@ class TestSpanClientReports:
1223
1222
Tests for client reports related to spans.
1224
1223
"""
1225
1224
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
-
1268
1225
@staticmethod
1269
1226
def span_dropper (spans_to_drop ):
1270
1227
"""
@@ -1293,23 +1250,19 @@ def __init__(self, span_count):
1293
1250
"""Configures a test case with the number of spans dropped and whether the transaction was dropped."""
1294
1251
self .span_count = span_count
1295
1252
self .expected_record_lost_event_calls = Counter ()
1296
- self .expected_record_lost_transaction_calls = Counter ()
1297
1253
self .before_send = lambda event , _ : event
1298
1254
self .event_processor = lambda event , _ : event
1299
- self .already_dropped_spans = 0
1300
1255
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 ):
1304
1257
"""
1305
1258
Updates the expected calls with the given resulting calls.
1306
1259
"""
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
1310
1264
1311
- elif drops_spans is not None :
1312
- self .already_dropped_spans += drops_spans
1265
+ if drops_spans > 0 :
1313
1266
self .expected_record_lost_event_calls [
1314
1267
(reason , "span" , None , drops_spans )
1315
1268
] += 1
@@ -1318,14 +1271,13 @@ def with_before_send(
1318
1271
self ,
1319
1272
before_send ,
1320
1273
* ,
1321
- drops_transaction = False ,
1322
- drops_spans = None ,
1274
+ drops_transactions = 0 ,
1275
+ drops_spans = 0 ,
1323
1276
):
1324
- """drops_transaction and drops_spans are mutually exclusive."""
1325
1277
self .before_send = before_send
1326
1278
self ._update_resulting_calls (
1327
1279
"before_send" ,
1328
- drops_transaction ,
1280
+ drops_transactions ,
1329
1281
drops_spans ,
1330
1282
)
1331
1283
@@ -1335,73 +1287,87 @@ def with_event_processor(
1335
1287
self ,
1336
1288
event_processor ,
1337
1289
* ,
1338
- drops_transaction = False ,
1339
- drops_spans = None ,
1290
+ drops_transactions = 0 ,
1291
+ drops_spans = 0 ,
1340
1292
):
1341
1293
self .event_processor = event_processor
1342
1294
self ._update_resulting_calls (
1343
1295
"event_processor" ,
1344
- drops_transaction ,
1296
+ drops_transactions ,
1345
1297
drops_spans ,
1346
1298
)
1347
1299
1348
1300
return self
1349
1301
1350
- def run (self ):
1302
+ def run (self , sentry_init , capture_record_lost_event_calls ):
1351
1303
"""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 ()
1353
1306
1354
1307
with sentry_sdk .isolation_scope () as scope :
1355
1308
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 )
1359
1311
1360
1312
# 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
1369
1314
1370
1315
1371
1316
@pytest .mark .parametrize (
1372
1317
"test_config" ,
1373
1318
(
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 ,
1377
1324
),
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 ,
1380
1329
),
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 ,
1383
1333
),
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 ,
1386
1337
),
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 ,
1389
1342
),
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 ,
1392
1346
),
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 ,
1395
1359
),
1396
1360
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
+ )
1401
1365
.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
+ ),
1404
1370
),
1405
1371
)
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 )
0 commit comments