Skip to content

Commit 81c12f4

Browse files
sentrivanaclaude
andcommitted
feat: Set user.ip_address on isolation scope instead of span attributes
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>
1 parent a96301d commit 81c12f4

5 files changed

Lines changed: 19 additions & 13 deletions

File tree

sentry_sdk/integrations/_asgi_common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import urllib
22
from typing import TYPE_CHECKING
33

4+
import sentry_sdk
5+
from sentry_sdk.consts import SPANDATA
46
from sentry_sdk.integrations._wsgi_common import _filter_headers
57
from sentry_sdk.scope import should_send_default_pii
68

@@ -133,6 +135,6 @@ def _get_request_attributes(asgi_scope: "Any") -> "dict[str, Any]":
133135
if client and should_send_default_pii():
134136
ip = _get_ip(asgi_scope)
135137
attributes["client.address"] = ip
136-
attributes["user.ip_address"] = ip
138+
sentry_sdk.get_isolation_scope().set_attribute(SPANDATA.USER_IP_ADDRESS, ip)
137139

138140
return attributes

sentry_sdk/integrations/aiohttp.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,12 @@ async def sentry_app_handle(
165165
else {}
166166
)
167167

168-
client_address_attributes = (
169-
{
170-
"client.address": request.remote,
171-
"user.ip_address": request.remote,
172-
}
173-
if should_send_default_pii() and request.remote
174-
else {}
175-
)
168+
client_address_attributes = {}
169+
if should_send_default_pii() and request.remote:
170+
client_address_attributes["client.address"] = request.remote
171+
sentry_sdk.get_isolation_scope().set_attribute(
172+
SPANDATA.USER_IP_ADDRESS, request.remote
173+
)
176174

177175
span_ctx = sentry_sdk.traces.start_span(
178176
# If this name makes it to the UI, AIOHTTP's URL

sentry_sdk/integrations/sanic.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,9 @@ def _get_request_attributes(request: "Request") -> "Dict[str, Any]":
375375

376376
if should_send_default_pii() and request.remote_addr:
377377
attributes[SPANDATA.CLIENT_ADDRESS] = request.remote_addr
378-
attributes[SPANDATA.USER_IP_ADDRESS] = request.remote_addr
378+
sentry_sdk.get_isolation_scope().set_attribute(
379+
SPANDATA.USER_IP_ADDRESS, request.remote_addr
380+
)
379381

380382
return attributes
381383

sentry_sdk/integrations/tornado.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ def _get_request_attributes(request: "Any") -> "Dict[str, Any]":
204204

205205
if should_send_default_pii() and request.remote_ip:
206206
attributes[SPANDATA.CLIENT_ADDRESS] = request.remote_ip
207-
attributes[SPANDATA.USER_IP_ADDRESS] = request.remote_ip
207+
sentry_sdk.get_isolation_scope().set_attribute(
208+
SPANDATA.USER_IP_ADDRESS, request.remote_ip
209+
)
208210

209211
with capture_internal_exceptions():
210212
raw_data = _get_tornado_request_data(request)

sentry_sdk/integrations/wsgi.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sentry_sdk
66
from sentry_sdk._werkzeug import _get_headers, get_host
77
from sentry_sdk.api import continue_trace
8-
from sentry_sdk.consts import OP
8+
from sentry_sdk.consts import OP, SPANDATA
99
from sentry_sdk.integrations._wsgi_common import (
1010
DEFAULT_HTTP_METHODS_TO_CAPTURE,
1111
_filter_headers,
@@ -412,6 +412,8 @@ def _get_request_attributes(
412412
client_ip = get_client_ip(environ)
413413
if client_ip:
414414
attributes["client.address"] = client_ip
415-
attributes["user.ip_address"] = client_ip
415+
sentry_sdk.get_isolation_scope().set_attribute(
416+
SPANDATA.USER_IP_ADDRESS, client_ip
417+
)
416418

417419
return attributes

0 commit comments

Comments
 (0)