Skip to content

Commit ac71829

Browse files
authored
Don't overwrite existing aiohttp baggage (getsentry#2214)
Do not override custom baggage when using aiohttp as a client.
1 parent ec14f94 commit ac71829

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

sentry_sdk/integrations/aiohttp.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
_filter_headers,
1313
request_body_within_bounds,
1414
)
15-
from sentry_sdk.tracing import SOURCE_FOR_STYLE, TRANSACTION_SOURCE_ROUTE
15+
from sentry_sdk.tracing import (
16+
BAGGAGE_HEADER_NAME,
17+
SOURCE_FOR_STYLE,
18+
TRANSACTION_SOURCE_ROUTE,
19+
)
1620
from sentry_sdk.tracing_utils import should_propagate_trace
1721
from sentry_sdk.utils import (
1822
capture_internal_exceptions,
@@ -220,7 +224,13 @@ async def on_request_start(session, trace_config_ctx, params):
220224
key=key, value=value, url=params.url
221225
)
222226
)
223-
params.headers[key] = value
227+
if key == BAGGAGE_HEADER_NAME and params.headers.get(
228+
BAGGAGE_HEADER_NAME
229+
):
230+
# do not overwrite any existing baggage, just append to it
231+
params.headers[key] += "," + value
232+
else:
233+
params.headers[key] = value
224234

225235
trace_config_ctx.span = span
226236

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,3 +505,32 @@ async def handler(request):
505505
parent_span_id=request_span.span_id,
506506
sampled=1,
507507
)
508+
509+
510+
@pytest.mark.asyncio
511+
async def test_outgoing_trace_headers_append_to_baggage(
512+
sentry_init, aiohttp_raw_server, aiohttp_client
513+
):
514+
sentry_init(
515+
integrations=[AioHttpIntegration()],
516+
traces_sample_rate=1.0,
517+
release="d08ebdb9309e1b004c6f52202de58a09c2268e42",
518+
)
519+
520+
async def handler(request):
521+
return web.Response(text="OK")
522+
523+
raw_server = await aiohttp_raw_server(handler)
524+
525+
with start_transaction(
526+
name="/interactions/other-dogs/new-dog",
527+
op="greeting.sniff",
528+
trace_id="0123456789012345678901234567890",
529+
):
530+
client = await aiohttp_client(raw_server)
531+
resp = await client.get("/", headers={"bagGage": "custom=value"})
532+
533+
assert (
534+
resp.request_info.headers["baggage"]
535+
== "custom=value,sentry-trace_id=0123456789012345678901234567890,sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0"
536+
)

0 commit comments

Comments
 (0)