Skip to content

Commit dccae69

Browse files
authored
fix(user): Set user.ip_address on telemetry if present (#6555)
### Description If user data is set on scope, we should materialize it onto attributes-based telemetry (metrics, logs, spans v2) at the end. The code for that was there and working, but we were omitting `ip_address`. #### Issues <!-- * resolves: #1234 * resolves: LIN-1234 --> #### Reminders - Please add tests to validate your changes, and lint your code using `uv run ruff`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent 94ceb1a commit dccae69

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

sentry_sdk/scope.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,7 @@ def _apply_user_attributes_to_telemetry(
17411741
("user.id", "id"),
17421742
("user.name", "username"),
17431743
("user.email", "email"),
1744+
("user.ip_address", "ip_address"),
17441745
):
17451746
if user_attribute in self._user and attribute_name not in attributes:
17461747
attributes[attribute_name] = self._user[user_attribute]

tests/test_attributes.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,41 @@ class Cat:
174174
# Attrs originally from the scope are serialized when sent
175175
assert isinstance(metric["attributes"]["instance"], str)
176176
assert isinstance(metric["attributes"]["dictionary"], str)
177+
178+
179+
def test_user_attributes(sentry_init, capture_items):
180+
sentry_init(
181+
traces_sample_rate=1.0,
182+
send_default_pii=True,
183+
_experiments={
184+
"trace_lifecycle": "stream",
185+
},
186+
)
187+
188+
items = capture_items("trace_metric", "span")
189+
190+
sentry_sdk.set_user(
191+
{
192+
"id": "123456",
193+
"username": "username",
194+
"email": "username@example.com",
195+
"ip_address": "127.0.0.1",
196+
}
197+
)
198+
199+
with sentry_sdk.traces.start_span(name="login"):
200+
sentry_sdk.metrics.count("test", 1)
201+
202+
sentry_sdk.get_client().flush()
203+
204+
metric, span = [item.payload for item in items]
205+
206+
assert metric["attributes"]["user.id"] == "123456"
207+
assert metric["attributes"]["user.name"] == "username"
208+
assert metric["attributes"]["user.email"] == "username@example.com"
209+
assert metric["attributes"]["user.ip_address"] == "127.0.0.1"
210+
211+
assert span["attributes"]["user.id"] == "123456"
212+
assert span["attributes"]["user.name"] == "username"
213+
assert span["attributes"]["user.email"] == "username@example.com"
214+
assert span["attributes"]["user.ip_address"] == "127.0.0.1"

0 commit comments

Comments
 (0)