Skip to content

fix(span-first): Set user.ip_address on all streamed spans#6434

Merged
sentrivana merged 7 commits into
masterfrom
ivana/attach-user-ip-address-to-all-spans
May 29, 2026
Merged

fix(span-first): Set user.ip_address on all streamed spans#6434
sentrivana merged 7 commits into
masterfrom
ivana/attach-user-ip-address-to-all-spans

Conversation

@sentrivana
Copy link
Copy Markdown
Contributor

@sentrivana sentrivana commented May 28, 2026

user.ip_address should be attached to all spans as per https://develop.sentry.dev/sdk/telemetry/spans/span-protocol/#common-attribute-keys, not just segments. Set it on the isolation scope so that it gets applied to all spans active within it. Had to move setting the attr in a couple integrations so that it's set early enough for the child spans to also get it.

Use SPANDATA.USER_IP_ADDRESS constant and set it via
sentry_sdk.get_isolation_scope().set_attribute() so the IP address
is available on all spans, not just the server root span.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 28, 2026

Codecov Results 📊

67 passed | Total: 67 | Pass Rate: 100% | Execution Time: 5.87s

All tests are passing successfully.

❌ Patch coverage is 41.18%. Project has 15191 uncovered lines.

Files with missing lines (5)
File Patch % Lines
sentry_sdk/integrations/aiohttp.py 0.00% ⚠️ 4 Missing
sentry_sdk/integrations/asgi.py 75.00% ⚠️ 1 Missing and 1 partials
sentry_sdk/integrations/sanic.py 0.00% ⚠️ 2 Missing
sentry_sdk/integrations/tornado.py 0.00% ⚠️ 2 Missing
sentry_sdk/integrations/wsgi.py 80.00% ⚠️ 1 Missing and 1 partials

Generated by Codecov Action

Add test_user_ip_address_on_all_spans to wsgi, asgi, aiohttp, tornado,
and sanic. Each test creates a child span and verifies user.ip_address
is present on both server and child spans when send_default_pii=True.

Fix tornado and sanic to set user.ip_address on the isolation scope
before the handler runs, so child spans inherit the attribute.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@sentrivana sentrivana changed the title fix(span-first): Set user.ip_address on isolation scope instead of span attributes fix(span-first): Set user.ip_address on all streamed spans May 28, 2026
if client and should_send_default_pii():
ip = _get_ip(asgi_scope)
attributes["client.address"] = ip
attributes["user.ip_address"] = ip
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved to asgi.py

@sentrivana sentrivana marked this pull request as ready for review May 29, 2026 08:20
@sentrivana sentrivana requested a review from a team as a code owner May 29, 2026 08:20
@sentrivana sentrivana enabled auto-merge (squash) May 29, 2026 09:33
@sentrivana sentrivana merged commit 8d442df into master May 29, 2026
166 of 168 checks passed
@sentrivana sentrivana deleted the ivana/attach-user-ip-address-to-all-spans branch May 29, 2026 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants