@@ -58,7 +58,7 @@ def mock_putheader_fn(self, header, value):
5858 "traces_sample_rate=1" ,
5959 ],
6060)
61- def test_no_incoming_trace_and_trace_targets_matching (
61+ def test_no_incoming_trace_and_trace_propagation_targets_matching (
6262 sentry_init , capture_events , _mock_putheader , traces_sample_rate
6363):
6464 init_kwargs = {}
@@ -107,17 +107,23 @@ def test_no_incoming_trace_and_trace_targets_matching(
107107 "traces_sample_rate=1" ,
108108 ],
109109)
110- def test_with_incoming_trace_and_trace_targets_matching (
110+ def test_no_incoming_trace_and_trace_propagation_targets_not_matching (
111111 sentry_init , capture_events , _mock_putheader , traces_sample_rate
112112):
113- init_kwargs = {}
113+ init_kwargs = {
114+ "trace_propagation_targets" : [
115+ "http://someothersite.com" ,
116+ ],
117+ }
114118 if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE :
115119 init_kwargs ["traces_sample_rate" ] = traces_sample_rate
116120 sentry_init (** init_kwargs )
117121
118122 events = capture_events ()
119123
120- with sentry_sdk .continue_trace (INCOMING_HEADERS ):
124+ NO_INCOMING_HEADERS = {} # noqa: N806
125+
126+ with sentry_sdk .continue_trace (NO_INCOMING_HEADERS ):
121127 with sentry_sdk .start_span (op = "test" , name = "test" ):
122128 requests .get ("http://example.com" )
123129
@@ -130,18 +136,11 @@ def test_with_incoming_trace_and_trace_targets_matching(
130136 outgoing_request_headers = {key : value for key , value in _mock_putheader }
131137
132138 # CHECK if trace information is added to the outgoing request
133- assert "sentry-trace" in outgoing_request_headers
134- assert "baggage" in outgoing_request_headers
139+ assert "sentry-trace" not in outgoing_request_headers
140+ assert "baggage" not in outgoing_request_headers
135141
136142 # CHECK if incoming trace is continued
137- if traces_sample_rate in (0 , 1 , USE_DEFAULT_TRACES_SAMPLE_RATE ):
138- # continue the incoming trace
139- assert INCOMING_TRACE_ID in outgoing_request_headers ["sentry-trace" ]
140- assert INCOMING_TRACE_ID in outgoing_request_headers ["baggage" ]
141- elif traces_sample_rate is None :
142- # do NOT continue the incoming trace
143- assert INCOMING_TRACE_ID not in outgoing_request_headers ["sentry-trace" ]
144- assert INCOMING_TRACE_ID not in outgoing_request_headers ["baggage" ]
143+ # (no assert necessary, because the trace information is not added to the outgoing request (see previous asserts))
145144
146145
147146@pytest .mark .parametrize (
@@ -159,23 +158,30 @@ def test_with_incoming_trace_and_trace_targets_matching(
159158 "traces_sample_rate=1" ,
160159 ],
161160)
162- def test_no_incoming_trace_and_trace_targets_not_matching (
163- sentry_init , capture_events , _mock_putheader , traces_sample_rate
161+ @pytest .mark .parametrize (
162+ "incoming_parent_sampled" ,
163+ ["deferred" , "1" , "0" ],
164+ ids = [
165+ "incoming_parent_sampled=DEFERRED" ,
166+ "incoming_parent_sampled=1" ,
167+ "incoming_parent_sampled=0" ,
168+ ],
169+ )
170+ def test_with_incoming_trace_and_trace_propagation_targets_matching (
171+ sentry_init ,
172+ capture_events ,
173+ _mock_putheader ,
174+ incoming_parent_sampled ,
175+ traces_sample_rate ,
164176):
165- init_kwargs = {
166- "trace_propagation_targets" : [
167- "http://someothersite.com" ,
168- ],
169- }
177+ init_kwargs = {}
170178 if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE :
171179 init_kwargs ["traces_sample_rate" ] = traces_sample_rate
172180 sentry_init (** init_kwargs )
173181
174182 events = capture_events ()
175183
176- NO_INCOMING_HEADERS = {} # noqa: N806
177-
178- with sentry_sdk .continue_trace (NO_INCOMING_HEADERS ):
184+ with sentry_sdk .continue_trace (INCOMING_HEADERS ):
179185 with sentry_sdk .start_span (op = "test" , name = "test" ):
180186 requests .get ("http://example.com" )
181187
@@ -188,11 +194,18 @@ def test_no_incoming_trace_and_trace_targets_not_matching(
188194 outgoing_request_headers = {key : value for key , value in _mock_putheader }
189195
190196 # CHECK if trace information is added to the outgoing request
191- assert "sentry-trace" not in outgoing_request_headers
192- assert "baggage" not in outgoing_request_headers
197+ assert "sentry-trace" in outgoing_request_headers
198+ assert "baggage" in outgoing_request_headers
193199
194200 # CHECK if incoming trace is continued
195- # (no assert necessary, because the trace information is not added to the outgoing request (see previous asserts))
201+ if traces_sample_rate in (0 , 1 , USE_DEFAULT_TRACES_SAMPLE_RATE ):
202+ # continue the incoming trace
203+ assert INCOMING_TRACE_ID in outgoing_request_headers ["sentry-trace" ]
204+ assert INCOMING_TRACE_ID in outgoing_request_headers ["baggage" ]
205+ elif traces_sample_rate is None :
206+ # do NOT continue the incoming trace
207+ assert INCOMING_TRACE_ID not in outgoing_request_headers ["sentry-trace" ]
208+ assert INCOMING_TRACE_ID not in outgoing_request_headers ["baggage" ]
196209
197210
198211@pytest .mark .parametrize (
@@ -210,8 +223,21 @@ def test_no_incoming_trace_and_trace_targets_not_matching(
210223 "traces_sample_rate=1" ,
211224 ],
212225)
213- def test_with_incoming_trace_and_trace_targets_not_matching (
214- sentry_init , capture_events , _mock_putheader , traces_sample_rate
226+ @pytest .mark .parametrize (
227+ "incoming_parent_sampled" ,
228+ ["deferred" , "1" , "0" ],
229+ ids = [
230+ "incoming_parent_sampled=DEFERRED" ,
231+ "incoming_parent_sampled=1" ,
232+ "incoming_parent_sampled=0" ,
233+ ],
234+ )
235+ def test_with_incoming_trace_and_trace_propagation_targets_not_matching (
236+ sentry_init ,
237+ capture_events ,
238+ _mock_putheader ,
239+ incoming_parent_sampled ,
240+ traces_sample_rate ,
215241):
216242 init_kwargs = {
217243 "trace_propagation_targets" : [
0 commit comments