2828
2929
3030@pytest .fixture
31- def mock_putheader (monkeypatch ):
31+ def _mock_putheader (monkeypatch ):
3232 """
3333 Mock HTTPConnection.putheader to capture calls to it.
3434 """
@@ -59,7 +59,7 @@ def mock_putheader_fn(self, header, value):
5959 ],
6060)
6161def test_trace_propagation_no_incoming_trace (
62- sentry_init , capture_events , mock_putheader , traces_sample_rate
62+ sentry_init , capture_events , _mock_putheader , traces_sample_rate
6363):
6464 init_kwargs = {}
6565 if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE :
@@ -80,7 +80,7 @@ def test_trace_propagation_no_incoming_trace(
8080 else :
8181 assert len (events ) == 0
8282
83- outgoing_request_headers = {key : value for key , value in mock_putheader }
83+ outgoing_request_headers = {key : value for key , value in _mock_putheader }
8484 assert "custom-header" in outgoing_request_headers
8585
8686 # CHECK if trace information is added to the outgoing request
@@ -109,7 +109,7 @@ def test_trace_propagation_no_incoming_trace(
109109 ],
110110)
111111def test_trace_propagation_with_incoming_trace (
112- sentry_init , capture_events , mock_putheader , traces_sample_rate
112+ sentry_init , capture_events , _mock_putheader , traces_sample_rate
113113):
114114 init_kwargs = {}
115115 if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE :
@@ -130,7 +130,7 @@ def test_trace_propagation_with_incoming_trace(
130130 else :
131131 assert len (events ) == 0
132132
133- outgoing_request_headers = {key : value for key , value in mock_putheader }
133+ outgoing_request_headers = {key : value for key , value in _mock_putheader }
134134 assert "custom-header" in outgoing_request_headers
135135
136136 # CHECK if trace information is added to the outgoing request
@@ -146,3 +146,101 @@ def test_trace_propagation_with_incoming_trace(
146146 # do NOT continue the incoming trace
147147 assert INCOMING_TRACE_ID not in outgoing_request_headers ["sentry-trace" ]
148148 assert INCOMING_TRACE_ID not in outgoing_request_headers ["baggage" ]
149+
150+
151+ @pytest .mark .parametrize (
152+ "traces_sample_rate" ,
153+ [
154+ USE_DEFAULT_TRACES_SAMPLE_RATE ,
155+ None ,
156+ 0 ,
157+ 1.0 ,
158+ ],
159+ ids = [
160+ "default traces_sample_rate" ,
161+ "traces_sample_rate=None" ,
162+ "traces_sample_rate=0" ,
163+ "traces_sample_rate=1" ,
164+ ],
165+ )
166+ def test_trace_propagation_no_incoming_trace_and_targets_not_matching (
167+ sentry_init , capture_events , _mock_putheader , traces_sample_rate
168+ ):
169+ init_kwargs = {
170+ "trace_propagation_targets" : [
171+ "http://someothersite.com" ,
172+ ],
173+ }
174+ if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE :
175+ init_kwargs ["traces_sample_rate" ] = traces_sample_rate
176+ sentry_init (** init_kwargs )
177+
178+ events = capture_events ()
179+
180+ with sentry_sdk .continue_trace ({}):
181+ with sentry_sdk .start_span (op = "test" , name = "test" ):
182+ requests .get (
183+ "http://example.com" , headers = {"custom-header" : "custom-value" }
184+ )
185+
186+ # CHECK if performance data (a transaction/span) is sent to Sentry
187+ if traces_sample_rate == 1 :
188+ assert len (events ) == 1
189+ else :
190+ assert len (events ) == 0
191+
192+ outgoing_request_headers = {key : value for key , value in _mock_putheader }
193+ assert "custom-header" in outgoing_request_headers
194+
195+ # CHECK if trace information is added to the outgoing request
196+ assert "sentry-trace" not in outgoing_request_headers
197+ assert "baggage" not in outgoing_request_headers
198+
199+
200+ @pytest .mark .parametrize (
201+ "traces_sample_rate" ,
202+ [
203+ USE_DEFAULT_TRACES_SAMPLE_RATE ,
204+ None ,
205+ 0 ,
206+ 1.0 ,
207+ ],
208+ ids = [
209+ "default traces_sample_rate" ,
210+ "traces_sample_rate=None" ,
211+ "traces_sample_rate=0" ,
212+ "traces_sample_rate=1" ,
213+ ],
214+ )
215+ def test_trace_propagation_with_incoming_trace_and_targets_not_matching (
216+ sentry_init , capture_events , _mock_putheader , traces_sample_rate
217+ ):
218+ init_kwargs = {
219+ "trace_propagation_targets" : [
220+ "http://someothersite.com" ,
221+ ],
222+ }
223+ if traces_sample_rate != USE_DEFAULT_TRACES_SAMPLE_RATE :
224+ init_kwargs ["traces_sample_rate" ] = traces_sample_rate
225+ sentry_init (** init_kwargs )
226+
227+ events = capture_events ()
228+
229+ with sentry_sdk .continue_trace (INCOMING_HEADERS ):
230+ with sentry_sdk .start_span (op = "test" , name = "test" ):
231+ requests .get (
232+ "http://example.com" , headers = {"custom-header" : "custom-value" }
233+ )
234+
235+ # CHECK if performance data (a transaction/span) is sent to Sentry
236+ if traces_sample_rate == 1 :
237+ assert len (events ) == 1
238+ else :
239+ assert len (events ) == 0
240+
241+ outgoing_request_headers = {key : value for key , value in _mock_putheader }
242+ assert "custom-header" in outgoing_request_headers
243+
244+ # CHECK if trace information is added to the outgoing request
245+ assert "sentry-trace" not in outgoing_request_headers
246+ assert "baggage" not in outgoing_request_headers
0 commit comments