From 3e9a012cb785904ab3aaeab9292d5ac5c466a4ca Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 8 Oct 2024 16:55:04 +0200 Subject: [PATCH 1/2] Instead of getting span from span_recorder get it from envelope --- tests/integrations/aiohttp/test_aiohttp.py | 27 +++++--- tests/integrations/httpx/test_httpx.py | 61 +++++++++++------- tests/integrations/stdlib/test_httplib.py | 73 +++++++++++++--------- 3 files changed, 97 insertions(+), 64 deletions(-) diff --git a/tests/integrations/aiohttp/test_aiohttp.py b/tests/integrations/aiohttp/test_aiohttp.py index 5b25629a83..6678110452 100644 --- a/tests/integrations/aiohttp/test_aiohttp.py +++ b/tests/integrations/aiohttp/test_aiohttp.py @@ -517,12 +517,16 @@ async def handler(request): @pytest.mark.asyncio -async def test_outgoing_trace_headers(sentry_init, aiohttp_raw_server, aiohttp_client): +async def test_outgoing_trace_headers( + sentry_init, aiohttp_raw_server, aiohttp_client, capture_envelopes +): sentry_init( integrations=[AioHttpIntegration()], traces_sample_rate=1.0, ) + envelopes = capture_envelopes() + async def handler(request): return web.Response(text="OK") @@ -536,15 +540,18 @@ async def handler(request): ) as transaction: client = await aiohttp_client(raw_server) resp = await client.get("/") - request_span = transaction._span_recorder.spans[-1] - - assert resp.request_info.headers[ - "sentry-trace" - ] == "{trace_id}-{parent_span_id}-{sampled}".format( - trace_id=transaction.trace_id, - parent_span_id=request_span.span_id, - sampled=1, - ) + + envelope = envelopes[0] + transaction = envelope.get_transaction_event() + request_span = transaction["spans"][-1] + + assert resp.request_info.headers[ + "sentry-trace" + ] == "{trace_id}-{parent_span_id}-{sampled}".format( + trace_id=transaction["contexts"]["trace"]["trace_id"], + parent_span_id=request_span["span_id"], + sampled=1, + ) @pytest.mark.asyncio diff --git a/tests/integrations/httpx/test_httpx.py b/tests/integrations/httpx/test_httpx.py index 17bf7017a5..88935bf0bf 100644 --- a/tests/integrations/httpx/test_httpx.py +++ b/tests/integrations/httpx/test_httpx.py @@ -61,8 +61,13 @@ def before_breadcrumb(crumb, hint): "httpx_client", (httpx.Client(), httpx.AsyncClient()), ) -def test_outgoing_trace_headers(sentry_init, httpx_client): - sentry_init(traces_sample_rate=1.0, integrations=[HttpxIntegration()]) +def test_outgoing_trace_headers(sentry_init, httpx_client, capture_envelopes): + sentry_init( + traces_sample_rate=1.0, + integrations=[HttpxIntegration()], + ) + + envelopes = capture_envelopes() url = "http://example.com/" responses.add(responses.GET, url, status=200) @@ -79,27 +84,34 @@ def test_outgoing_trace_headers(sentry_init, httpx_client): else: response = httpx_client.get(url) - request_span = transaction._span_recorder.spans[-1] - assert response.request.headers[ - "sentry-trace" - ] == "{trace_id}-{parent_span_id}-{sampled}".format( - trace_id=transaction.trace_id, - parent_span_id=request_span.span_id, - sampled=1, - ) + envelope = envelopes[0] + transaction = envelope.get_transaction_event() + request_span = transaction["spans"][-1] + + assert response.request.headers[ + "sentry-trace" + ] == "{trace_id}-{parent_span_id}-{sampled}".format( + trace_id=transaction["contexts"]["trace"]["trace_id"], + parent_span_id=request_span["span_id"], + sampled=1, + ) @pytest.mark.parametrize( "httpx_client", (httpx.Client(), httpx.AsyncClient()), ) -def test_outgoing_trace_headers_append_to_baggage(sentry_init, httpx_client): +def test_outgoing_trace_headers_append_to_baggage( + sentry_init, httpx_client, capture_envelopes +): sentry_init( traces_sample_rate=1.0, integrations=[HttpxIntegration()], release="d08ebdb9309e1b004c6f52202de58a09c2268e42", ) + envelopes = capture_envelopes() + url = "http://example.com/" responses.add(responses.GET, url, status=200) @@ -115,18 +127,21 @@ def test_outgoing_trace_headers_append_to_baggage(sentry_init, httpx_client): else: response = httpx_client.get(url, headers={"baGGage": "custom=data"}) - request_span = transaction._span_recorder.spans[-1] - assert response.request.headers[ - "sentry-trace" - ] == "{trace_id}-{parent_span_id}-{sampled}".format( - trace_id=transaction.trace_id, - parent_span_id=request_span.span_id, - sampled=1, - ) - assert ( - response.request.headers["baggage"] - == "custom=data,sentry-trace_id=01234567890123456789012345678901,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true" - ) + envelope = envelopes[0] + transaction = envelope.get_transaction_event() + request_span = transaction["spans"][-1] + + assert response.request.headers[ + "sentry-trace" + ] == "{trace_id}-{parent_span_id}-{sampled}".format( + trace_id=transaction["contexts"]["trace"]["trace_id"], + parent_span_id=request_span["span_id"], + sampled=1, + ) + assert ( + response.request.headers["baggage"] + == "custom=data,sentry-trace_id=01234567890123456789012345678901,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true" + ) @pytest.mark.parametrize( diff --git a/tests/integrations/stdlib/test_httplib.py b/tests/integrations/stdlib/test_httplib.py index c327331608..4fd6ba9e47 100644 --- a/tests/integrations/stdlib/test_httplib.py +++ b/tests/integrations/stdlib/test_httplib.py @@ -130,7 +130,7 @@ def test_httplib_misuse(sentry_init, capture_events, request): ) -def test_outgoing_trace_headers(sentry_init, monkeypatch): +def test_outgoing_trace_headers(sentry_init, monkeypatch, capture_envelopes): # HTTPSConnection.send is passed a string containing (among other things) # the headers on the request. Mock it so we can check the headers, and also # so it doesn't try to actually talk to the internet. @@ -139,6 +139,8 @@ def test_outgoing_trace_headers(sentry_init, monkeypatch): sentry_init(traces_sample_rate=1.0) + envelopes = capture_envelopes() + headers = {} headers["baggage"] = ( "other-vendor-value-1=foo;bar;baz, sentry-trace_id=771a43a4192642f0b136d5159a501700, " @@ -163,25 +165,28 @@ def test_outgoing_trace_headers(sentry_init, monkeypatch): key, val = line.split(": ") request_headers[key] = val - request_span = transaction._span_recorder.spans[-1] - expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format( - trace_id=transaction.trace_id, - parent_span_id=request_span.span_id, - sampled=1, - ) - assert request_headers["sentry-trace"] == expected_sentry_trace + envelope = envelopes[0] + transaction = envelope.get_transaction_event() + request_span = transaction["spans"][-1] - expected_outgoing_baggage = ( - "sentry-trace_id=771a43a4192642f0b136d5159a501700," - "sentry-public_key=49d0f7386ad645858ae85020e393bef3," - "sentry-sample_rate=0.01337," - "sentry-user_id=Am%C3%A9lie" - ) + expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format( + trace_id=transaction["contexts"]["trace"]["trace_id"], + parent_span_id=request_span["span_id"], + sampled=1, + ) + assert request_headers["sentry-trace"] == expected_sentry_trace + + expected_outgoing_baggage = ( + "sentry-trace_id=771a43a4192642f0b136d5159a501700," + "sentry-public_key=49d0f7386ad645858ae85020e393bef3," + "sentry-sample_rate=0.01337," + "sentry-user_id=Am%C3%A9lie" + ) - assert request_headers["baggage"] == expected_outgoing_baggage + assert request_headers["baggage"] == expected_outgoing_baggage -def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch): +def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch, capture_envelopes): # HTTPSConnection.send is passed a string containing (among other things) # the headers on the request. Mock it so we can check the headers, and also # so it doesn't try to actually talk to the internet. @@ -192,6 +197,9 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch): monkeypatch.setattr(random, "random", lambda: 0.1) sentry_init(traces_sample_rate=0.5, release="foo") + + envelopes = capture_envelopes() + transaction = Transaction.continue_from_headers({}) with start_transaction(transaction=transaction, name="Head SDK tx") as transaction: @@ -204,23 +212,26 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch): key, val = line.split(": ") request_headers[key] = val - request_span = transaction._span_recorder.spans[-1] - expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format( - trace_id=transaction.trace_id, - parent_span_id=request_span.span_id, - sampled=1, - ) - assert request_headers["sentry-trace"] == expected_sentry_trace + envelope = envelopes[0] + transaction = envelope.get_transaction_event() + request_span = transaction["spans"][-1] + + expected_sentry_trace = "{trace_id}-{parent_span_id}-{sampled}".format( + trace_id=transaction["contexts"]["trace"]["trace_id"], + parent_span_id=request_span["span_id"], + sampled=1, + ) + assert request_headers["sentry-trace"] == expected_sentry_trace - expected_outgoing_baggage = ( - "sentry-trace_id=%s," - "sentry-environment=production," - "sentry-release=foo," - "sentry-sample_rate=0.5," - "sentry-sampled=%s" - ) % (transaction.trace_id, "true" if transaction.sampled else "false") + expected_outgoing_baggage = ( + "sentry-trace_id=%s," + "sentry-environment=production," + "sentry-release=foo," + "sentry-sample_rate=0.5," + "sentry-sampled=%s" + ) % (transaction.trace_id, "true" if transaction.sampled else "false") - assert request_headers["baggage"] == expected_outgoing_baggage + assert request_headers["baggage"] == expected_outgoing_baggage @pytest.mark.parametrize( From 13c96befe7978d1f69aa7fa7ccf3917ac1f3109a Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Tue, 8 Oct 2024 16:58:58 +0200 Subject: [PATCH 2/2] Make test fail easier --- tests/integrations/aiohttp/test_aiohttp.py | 2 +- tests/integrations/httpx/test_httpx.py | 4 ++-- tests/integrations/stdlib/test_httplib.py | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/integrations/aiohttp/test_aiohttp.py b/tests/integrations/aiohttp/test_aiohttp.py index 6678110452..c73d1697ad 100644 --- a/tests/integrations/aiohttp/test_aiohttp.py +++ b/tests/integrations/aiohttp/test_aiohttp.py @@ -541,7 +541,7 @@ async def handler(request): client = await aiohttp_client(raw_server) resp = await client.get("/") - envelope = envelopes[0] + (envelope,) = envelopes transaction = envelope.get_transaction_event() request_span = transaction["spans"][-1] diff --git a/tests/integrations/httpx/test_httpx.py b/tests/integrations/httpx/test_httpx.py index 88935bf0bf..f31a665245 100644 --- a/tests/integrations/httpx/test_httpx.py +++ b/tests/integrations/httpx/test_httpx.py @@ -84,7 +84,7 @@ def test_outgoing_trace_headers(sentry_init, httpx_client, capture_envelopes): else: response = httpx_client.get(url) - envelope = envelopes[0] + (envelope,) = envelopes transaction = envelope.get_transaction_event() request_span = transaction["spans"][-1] @@ -127,7 +127,7 @@ def test_outgoing_trace_headers_append_to_baggage( else: response = httpx_client.get(url, headers={"baGGage": "custom=data"}) - envelope = envelopes[0] + (envelope,) = envelopes transaction = envelope.get_transaction_event() request_span = transaction["spans"][-1] diff --git a/tests/integrations/stdlib/test_httplib.py b/tests/integrations/stdlib/test_httplib.py index 4fd6ba9e47..3d4aa988f6 100644 --- a/tests/integrations/stdlib/test_httplib.py +++ b/tests/integrations/stdlib/test_httplib.py @@ -165,7 +165,7 @@ def test_outgoing_trace_headers(sentry_init, monkeypatch, capture_envelopes): key, val = line.split(": ") request_headers[key] = val - envelope = envelopes[0] + (envelope,) = envelopes transaction = envelope.get_transaction_event() request_span = transaction["spans"][-1] @@ -212,7 +212,7 @@ def test_outgoing_trace_headers_head_sdk(sentry_init, monkeypatch, capture_envel key, val = line.split(": ") request_headers[key] = val - envelope = envelopes[0] + (envelope,) = envelopes transaction = envelope.get_transaction_event() request_span = transaction["spans"][-1]