From fb37f2302120cbd57257b3caee43cd920c7aa792 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 29 Jun 2023 13:58:59 +0200 Subject: [PATCH 1/2] Append to existing aiohttp baggage Do not override custom baggage when using aiohttp as a client. --- sentry_sdk/integrations/aiohttp.py | 14 ++++++++-- tests/integrations/aiohttp/test_aiohttp.py | 30 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index c6f26cace9..af8cb66102 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -12,7 +12,11 @@ _filter_headers, request_body_within_bounds, ) -from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE +from sentry_sdk.tracing import ( + BAGGAGE_HEADER_NAME, + SOURCE_FOR_STYLE, + TRANSACTION_SOURCE_ROUTE, +) from sentry_sdk.tracing_utils import should_propagate_trace from sentry_sdk.utils import ( capture_internal_exceptions, @@ -220,7 +224,13 @@ async def on_request_start(session, trace_config_ctx, params): key=key, value=value, url=params.url ) ) - params.headers[key] = value + if key == BAGGAGE_HEADER_NAME and params.headers.get( + BAGGAGE_HEADER_NAME + ): + # do not overwrite any existing baggage, just append to it + params.headers[key] += "," + value + else: + params.headers[key] = value trace_config_ctx.span = span diff --git a/tests/integrations/aiohttp/test_aiohttp.py b/tests/integrations/aiohttp/test_aiohttp.py index 29f4cd47ef..0860289b69 100644 --- a/tests/integrations/aiohttp/test_aiohttp.py +++ b/tests/integrations/aiohttp/test_aiohttp.py @@ -505,3 +505,33 @@ async def handler(request): parent_span_id=request_span.span_id, sampled=1, ) + + +@pytest.mark.asyncio +async def test_outgoing_trace_headers_append_to_baggage( + sentry_init, aiohttp_raw_server, aiohttp_client +): + sentry_init( + integrations=[AioHttpIntegration()], + traces_sample_rate=1.0, + release="d08ebdb9309e1b004c6f52202de58a09c2268e42", + ) + + async def handler(request): + return web.Response(text="OK") + + raw_server = await aiohttp_raw_server(handler) + + with start_transaction( + name="/interactions/other-dogs/new-dog", + op="greeting.sniff", + trace_id="0123456789012345678901234567890", + ) as transaction: + client = await aiohttp_client(raw_server) + resp = await client.get("/", headers={"bagGage": "custom=value"}) + + assert resp.request_info.headers[ + "baggage" + ] == "custom=value,sentry-trace_id={trace_id},sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0".format( + trace_id=transaction.trace_id, + ) From b13a539067c18de71ebb175ea22583a852671709 Mon Sep 17 00:00:00 2001 From: Ivana Kellyerova Date: Thu, 29 Jun 2023 14:01:06 +0200 Subject: [PATCH 2/2] . --- tests/integrations/aiohttp/test_aiohttp.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/integrations/aiohttp/test_aiohttp.py b/tests/integrations/aiohttp/test_aiohttp.py index 0860289b69..84d84c9a44 100644 --- a/tests/integrations/aiohttp/test_aiohttp.py +++ b/tests/integrations/aiohttp/test_aiohttp.py @@ -526,12 +526,11 @@ async def handler(request): name="/interactions/other-dogs/new-dog", op="greeting.sniff", trace_id="0123456789012345678901234567890", - ) as transaction: + ): client = await aiohttp_client(raw_server) resp = await client.get("/", headers={"bagGage": "custom=value"}) - assert resp.request_info.headers[ - "baggage" - ] == "custom=value,sentry-trace_id={trace_id},sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0".format( - trace_id=transaction.trace_id, + assert ( + resp.request_info.headers["baggage"] + == "custom=value,sentry-trace_id=0123456789012345678901234567890,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0" )