11import uuid
22
33import pytest
4- from sentry_sdk .envelope import Envelope , Item
4+ from sentry_sdk .envelope import Envelope , Item , PayloadRef
55import queue
66
77
@@ -462,16 +462,22 @@ def test_uses_trace_public_key(mini_sentry, relay):
462462 mini_sentry .captured_outcomes .get (timeout = 2 )
463463
464464
465- def test_fast_path (mini_sentry , relay ):
465+ @pytest .mark .parametrize (
466+ "rule_type, event_factory" ,
467+ [
468+ ("error" , _create_event_envelope ),
469+ ("transaction" , _create_transaction_envelope ),
470+ ("trace" , _create_transaction_envelope ),
471+ ],
472+ )
473+ def test_multi_item_envelope (mini_sentry , relay , rule_type , event_factory ):
466474 """
467- Tests that the fast path works .
475+ Associated items are removed together with event item .
468476
469- When the project config is already fetched and the envelope only
470- contains a transaction a fast evaluation path is used.
477+ The event is sent twice to account for both fast and slow paths.
471478
472- While this is an implementation detail of Relay that is not visible
473- here we test that Relay behaves normally for the conditions that we
474- know are going to trigger a fast-path evaluation
479+ When sampling decides to remove a transaction it should also remove all
480+ dependent items (attachments).
475481 """
476482 project_id = 42
477483 relay = relay (mini_sentry , _outcomes_enabled_config ())
@@ -480,14 +486,25 @@ def test_fast_path(mini_sentry, relay):
480486 config = mini_sentry .add_basic_project_config (project_id )
481487 # add a sampling rule to project config that removes all transactions (sample_rate=0)
482488 public_key = config ["publicKeys" ][0 ]["publicKey" ]
483- _add_sampling_config (config , sample_rate = 0 , rule_type = "trace" )
489+
490+ # add a sampling rule to project config that drops all events (sample_rate=0), it should be ignored
491+ # because there is an invalid rule in the configuration
492+ _add_sampling_config (config , sample_rate = 0 , rule_type = rule_type )
484493
485494 for i in range (2 ):
486495 # create an envelope with a trace context that is initiated by this project (for simplicity)
487496 envelope = Envelope ()
488- transaction , trace_id , event_id = _create_transaction_item ()
489- envelope .add_transaction (transaction )
490- _add_trace_info (envelope , trace_id = trace_id , public_key = public_key )
497+ # create an envelope with a trace context that is initiated by this project (for simplicity)
498+ envelope , trace_id , event_id = event_factory (public_key )
499+ envelope .add_item (
500+ Item (payload = PayloadRef (json = {"x" : "some attachment" }), type = "attachment" )
501+ )
502+ envelope .add_item (
503+ Item (
504+ payload = PayloadRef (json = {"y" : "some other attachment" }),
505+ type = "attachment" ,
506+ )
507+ )
491508
492509 # send the event, the transaction should be removed.
493510 relay .send_envelope (project_id , envelope )
@@ -497,50 +514,3 @@ def test_fast_path(mini_sentry, relay):
497514
498515 outcomes = mini_sentry .captured_outcomes .get (timeout = 2 )
499516 assert outcomes is not None
500-
501-
502- def test_multi_item_envelope (mini_sentry , relay ):
503- """
504- Test that multi item envelopes are handled correctly.
505-
506- Pass an envelope containing of a transaction that will be sampled and an event and
507- test
508-
509-
510- """
511- project_id = 42
512- relay = relay (mini_sentry , _outcomes_enabled_config ())
513-
514- # create a basic project config
515- config = mini_sentry .add_basic_project_config (project_id )
516- # add a sampling rule to project config that removes all transactions (sample_rate=0)
517- public_key = config ["publicKeys" ][0 ]["publicKey" ]
518- _add_sampling_config (config , sample_rate = 0 , rule_type = "trace" )
519-
520- # we'll run the test twice to make sure that the fast path works as well
521- for i in range (2 ):
522- # create an envelope with a trace context that is initiated by this project (for simplicity)
523- envelope = Envelope ()
524- transaction , trace_id , event_id = _create_transaction_item ()
525- envelope .add_transaction (transaction )
526- envelope .add_event ({"message" : "Hello" })
527- _add_trace_info (envelope , trace_id = trace_id , public_key = public_key )
528-
529- # send the event, the transaction should be removed.
530- relay .send_envelope (project_id , envelope )
531-
532- msg = mini_sentry .captured_events .get (timeout = 1 )
533- assert msg is not None
534- transaction = msg .get_transaction_event ()
535- event = msg .get_event ()
536-
537- # check that the transaction was removed during the transaction sampling process
538- assert transaction is None
539- # but the event was kept
540- assert event is not None
541- assert event .setdefault ("logentry" , {}).get ("formatted" ) == "Hello"
542-
543- # no outcome should be generated since we forward the event to upstream
544- # NOTE this might change in the future so we might get outcomes here in the future
545- with pytest .raises (queue .Empty ):
546- mini_sentry .captured_outcomes .get (timeout = 2 )
0 commit comments