@@ -86,6 +86,20 @@ def inner(**kwargs):
86
86
return inner
87
87
88
88
89
+ def mock_transaction_envelope (span_count ):
90
+ # type: (int) -> Envelope
91
+ event = defaultdict (
92
+ mock .MagicMock ,
93
+ type = "transaction" ,
94
+ spans = [mock .MagicMock () for _ in range (span_count )],
95
+ )
96
+
97
+ envelope = Envelope ()
98
+ envelope .add_transaction (event )
99
+
100
+ return envelope
101
+
102
+
89
103
@pytest .mark .forked
90
104
@pytest .mark .parametrize ("debug" , (True , False ))
91
105
@pytest .mark .parametrize ("client_flush_method" , ["close" , "flush" ])
@@ -628,3 +642,59 @@ class TestCustomHubClass(sentry_sdk.Hub):
628
642
629
643
with pytest .deprecated_call ():
630
644
assert transport .hub_cls is TestCustomHubClass
645
+
646
+
647
+ @pytest .mark .parametrize ("quantity" , (1 , 2 , 10 ))
648
+ def test_record_lost_event_quantity (capturing_server , make_client , quantity ):
649
+ client = make_client ()
650
+ transport = client .transport
651
+
652
+ transport .record_lost_event (reason = "test" , data_category = "span" , quantity = quantity )
653
+ client .flush ()
654
+
655
+ (captured ,) = capturing_server .captured # Should only be one envelope
656
+ envelope = captured .envelope
657
+ (item ,) = envelope .items # Envelope should only have one item
658
+
659
+ assert item .type == "client_report"
660
+
661
+ report = parse_json (item .get_bytes ())
662
+
663
+ assert report ["discarded_events" ] == [
664
+ {"category" : "span" , "reason" : "test" , "quantity" : quantity }
665
+ ]
666
+
667
+
668
+ @pytest .mark .parametrize ("span_count" , (0 , 1 , 2 , 10 ))
669
+ def test_record_lost_event_transaction_item (capturing_server , make_client , span_count ):
670
+ client = make_client ()
671
+ transport = client .transport
672
+
673
+ envelope = mock_transaction_envelope (span_count )
674
+ (transaction_item ,) = envelope .items
675
+
676
+ transport .record_lost_event (reason = "test" , item = transaction_item )
677
+ client .flush ()
678
+
679
+ (captured ,) = capturing_server .captured # Should only be one envelope
680
+ envelope = captured .envelope
681
+ (item ,) = envelope .items # Envelope should only have one item
682
+
683
+ assert item .type == "client_report"
684
+
685
+ report = parse_json (item .get_bytes ())
686
+ discarded_events = report ["discarded_events" ]
687
+
688
+ assert len (discarded_events ) == 2
689
+
690
+ assert {
691
+ "category" : "transaction" ,
692
+ "reason" : "test" ,
693
+ "quantity" : 1 ,
694
+ } in discarded_events
695
+
696
+ assert {
697
+ "category" : "span" ,
698
+ "reason" : "test" ,
699
+ "quantity" : span_count + 1 ,
700
+ } in discarded_events
0 commit comments