@@ -202,14 +202,15 @@ def test_httplib_misuse(sentry_init, capture_events, request):
202202 )
203203
204204
205- def test_outgoing_trace_headers (sentry_init , monkeypatch ):
205+ def test_outgoing_trace_headers (sentry_init , capture_events , monkeypatch ):
206206 # HTTPSConnection.send is passed a string containing (among other things)
207207 # the headers on the request. Mock it so we can check the headers, and also
208208 # so it doesn't try to actually talk to the internet.
209209 mock_send = mock .Mock ()
210210 monkeypatch .setattr (HTTPSConnection , "send" , mock_send )
211211
212212 sentry_init (traces_sample_rate = 1.0 )
213+ events = capture_events ()
213214
214215 headers = {
215216 "sentry-trace" : "771a43a4192642f0b136d5159a501700-1234567890abcdef-1" ,
@@ -237,38 +238,42 @@ def test_outgoing_trace_headers(sentry_init, monkeypatch):
237238 key , val = line .split (": " )
238239 request_headers [key ] = val
239240
240- request_span = transaction ._span_recorder .spans [- 1 ]
241- expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}" .format (
242- trace_id = transaction .trace_id ,
243- parent_span_id = request_span .span_id ,
244- sampled = 1 ,
245- )
246- assert request_headers ["sentry-trace" ] == expected_sentry_trace
247-
248- expected_outgoing_baggage = (
249- "sentry-trace_id=771a43a4192642f0b136d5159a501700,"
250- "sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
251- "sentry-sample_rate=1.0,"
252- "sentry-user_id=Am%C3%A9lie,"
253- "sentry-sample_rand=0.132521102938283"
254- )
241+ (event ,) = events
242+ request_span = event ["spans" ][- 1 ]
243+ expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}" .format (
244+ trace_id = transaction .trace_id ,
245+ parent_span_id = request_span .span_id ,
246+ sampled = 1 ,
247+ )
248+ assert request_headers ["sentry-trace" ] == expected_sentry_trace
249+
250+ expected_outgoing_baggage = (
251+ "sentry-trace_id=771a43a4192642f0b136d5159a501700,"
252+ "sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
253+ "sentry-sample_rate=1.0,"
254+ "sentry-user_id=Am%C3%A9lie,"
255+ "sentry-sample_rand=0.132521102938283"
256+ )
255257
256- assert request_headers ["baggage" ] == expected_outgoing_baggage
258+ assert request_headers ["baggage" ] == expected_outgoing_baggage
257259
258260
259- def test_outgoing_trace_headers_head_sdk (sentry_init , monkeypatch ):
261+ def test_outgoing_trace_headers_head_sdk (sentry_init , capture_events , monkeypatch ):
260262 # HTTPSConnection.send is passed a string containing (among other things)
261263 # the headers on the request. Mock it so we can check the headers, and also
262264 # so it doesn't try to actually talk to the internet.
263265 mock_send = mock .Mock ()
264266 monkeypatch .setattr (HTTPSConnection , "send" , mock_send )
265267
266268 sentry_init (traces_sample_rate = 0.5 , release = "foo" )
269+ events = capture_events ()
270+
267271 with mock .patch ("sentry_sdk.tracing_utils.Random.randrange" , return_value = 250000 ):
268272 transaction = continue_trace ({})
269273
270274 with start_transaction (transaction = transaction , name = "Head SDK tx" ) as transaction :
271- HTTPSConnection ("www.squirrelchasers.com" ).request ("GET" , "/top-chasers" )
275+ connection = HTTPSConnection ("www.squirrelchasers.com" )
276+ connection .request ("GET" , "/top-chasers" )
272277
273278 (request_str ,) = mock_send .call_args [0 ]
274279 request_headers = {}
@@ -277,24 +282,25 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch):
277282 key , val = line .split (": " )
278283 request_headers [key ] = val
279284
280- request_span = transaction ._span_recorder .spans [- 1 ]
281- expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}" .format (
282- trace_id = transaction .trace_id ,
283- parent_span_id = request_span .span_id ,
284- sampled = 1 ,
285- )
286- assert request_headers ["sentry-trace" ] == expected_sentry_trace
287-
288- expected_outgoing_baggage = (
289- "sentry-trace_id=%s,"
290- "sentry-sample_rand=0.250000,"
291- "sentry-environment=production,"
292- "sentry-release=foo,"
293- "sentry-sample_rate=0.5,"
294- "sentry-sampled=%s"
295- ) % (transaction .trace_id , "true" if transaction .sampled else "false" )
296-
297- assert request_headers ["baggage" ] == expected_outgoing_baggage
285+ (event ,) = events
286+ request_span = event ["spans" ][0 ]
287+ expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}" .format (
288+ trace_id = transaction .trace_id ,
289+ parent_span_id = request_span .span_id ,
290+ sampled = 1 ,
291+ )
292+ assert request_headers ["sentry-trace" ] == expected_sentry_trace
293+
294+ expected_outgoing_baggage = (
295+ "sentry-trace_id=%s,"
296+ "sentry-sample_rand=0.250000,"
297+ "sentry-environment=production,"
298+ "sentry-release=foo,"
299+ "sentry-sample_rate=0.5,"
300+ "sentry-sampled=%s"
301+ ) % (transaction .trace_id , "true" if transaction .sampled else "false" )
302+
303+ assert request_headers ["baggage" ] == expected_outgoing_baggage
298304
299305
300306@pytest .mark .parametrize (
0 commit comments