Skip to content

Commit ad8da22

Browse files
romtsnclaude
andauthored
fix(logs,metrics): Attach user attributes to logs and metrics regardless of sendDefaultPii (#5099)
* fix: Attach user attributes to logs and metrics regardless of sendDefaultPii When a user is explicitly set on the scope, user.id, user.name, and user.email are now always attached to log and metric attributes, matching the existing behavior for error events (SentryClient.applyScope). Fixes #5078 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * changelog: Add entry for user attributes fix in logs/metrics Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a08aa02 commit ad8da22

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- When merging tombstones with Native SDK, use the tombstone message if the Native SDK didn't explicitly provide one. ([#5095](https://github.com/getsentry/sentry-java/pull/5095))
1212
- Fix thread leak caused by eager creation of `SentryExecutorService` in `SentryOptions` ([#5093](https://github.com/getsentry/sentry-java/pull/5093))
1313
- There were cases where we created options that ended up unused but we failed to clean those up.
14+
- Attach user attributes to logs and metrics regardless of `sendDefaultPii` ([#5099](https://github.com/getsentry/sentry-java/pull/5099))
1415
- No longer log a warning if a logging integration cannot initialize Sentry due to missing DSN ([#5075](https://github.com/getsentry/sentry-java/pull/5075))
1516
- While this may have been useful to some, it caused lots of confusion.
1617
- Session Replay: Add `androidx.camera.view.PreviewView` to default `maskedViewClasses` to mask camera previews by default. ([#5097](https://github.com/getsentry/sentry-java/pull/5097))

sentry/src/main/java/io/sentry/logger/LoggerApi.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,7 @@ private void captureLog(
243243
setServerName(attributes);
244244
}
245245

246-
if (scopes.getOptions().isSendDefaultPii()) {
247-
setUser(attributes);
248-
}
246+
setUser(attributes);
249247

250248
return attributes;
251249
}

sentry/src/main/java/io/sentry/metrics/MetricsApi.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ private void captureMetrics(
230230
setServerName(attributes);
231231
}
232232

233-
if (scopes.getOptions().isSendDefaultPii()) {
234-
setUser(attributes);
235-
}
233+
setUser(attributes);
236234

237235
return attributes;
238236
}

sentry/src/test/java/io/sentry/ScopesTest.kt

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,7 +2953,7 @@ class ScopesTest {
29532953
}
29542954

29552955
@Test
2956-
fun `does not add user fields to log attributes by default`() {
2956+
fun `adds user fields to log attributes even if sendDefaultPii is false`() {
29572957
val (sut, mockClient) =
29582958
getEnabledScopes {
29592959
it.logs.isEnabled = true
@@ -2975,9 +2975,17 @@ class ScopesTest {
29752975
check {
29762976
assertEquals("log message", it.body)
29772977

2978-
assertNull(it.attributes?.get("user.id"))
2979-
assertNull(it.attributes?.get("user.name"))
2980-
assertNull(it.attributes?.get("user.email"))
2978+
val userId = it.attributes?.get("user.id")!!
2979+
assertEquals("usrid", userId.value)
2980+
assertEquals("string", userId.type)
2981+
2982+
val userName = it.attributes?.get("user.name")!!
2983+
assertEquals("usrname", userName.value)
2984+
assertEquals("string", userName.type)
2985+
2986+
val userEmail = it.attributes?.get("user.email")!!
2987+
assertEquals("user@sentry.io", userEmail.value)
2988+
assertEquals("string", userEmail.type)
29812989
},
29822990
anyOrNull(),
29832991
)
@@ -2988,7 +2996,6 @@ class ScopesTest {
29882996
val (sut, mockClient) =
29892997
getEnabledScopes {
29902998
it.logs.isEnabled = true
2991-
it.isSendDefaultPii = true
29922999
it.distinctId = "distinctId"
29933000
}
29943001

@@ -3012,7 +3019,6 @@ class ScopesTest {
30123019
val (sut, mockClient) =
30133020
getEnabledScopes {
30143021
it.logs.isEnabled = true
3015-
it.isSendDefaultPii = true
30163022
it.distinctId = null
30173023
}
30183024

@@ -3919,7 +3925,7 @@ class ScopesTest {
39193925
}
39203926

39213927
@Test
3922-
fun `does not add user fields to metric attributes by default`() {
3928+
fun `adds user fields to metric attributes even if sendDefaultPii is false`() {
39233929
val (sut, mockClient) = getEnabledScopes { it.distinctId = "distinctId" }
39243930

39253931
sut.configureScope { scope ->
@@ -3937,9 +3943,17 @@ class ScopesTest {
39373943
check {
39383944
assertEquals("metric name", it.name)
39393945

3940-
assertNull(it.attributes?.get("user.id"))
3941-
assertNull(it.attributes?.get("user.name"))
3942-
assertNull(it.attributes?.get("user.email"))
3946+
val userId = it.attributes?.get("user.id")!!
3947+
assertEquals("usrid", userId.value)
3948+
assertEquals("string", userId.type)
3949+
3950+
val userName = it.attributes?.get("user.name")!!
3951+
assertEquals("usrname", userName.value)
3952+
assertEquals("string", userName.type)
3953+
3954+
val userEmail = it.attributes?.get("user.email")!!
3955+
assertEquals("user@sentry.io", userEmail.value)
3956+
assertEquals("string", userEmail.type)
39433957
},
39443958
anyOrNull(),
39453959
anyOrNull(),
@@ -3948,11 +3962,7 @@ class ScopesTest {
39483962

39493963
@Test
39503964
fun `unset user does provide distinct-id as user-id for metrics`() {
3951-
val (sut, mockClient) =
3952-
getEnabledScopes {
3953-
it.isSendDefaultPii = true
3954-
it.distinctId = "distinctId"
3955-
}
3965+
val (sut, mockClient) = getEnabledScopes { it.distinctId = "distinctId" }
39563966

39573967
sut.metrics().count("metric name")
39583968

@@ -3972,11 +3982,7 @@ class ScopesTest {
39723982

39733983
@Test
39743984
fun `unset user does provide null user-id when distinct-id is missing for metrics`() {
3975-
val (sut, mockClient) =
3976-
getEnabledScopes {
3977-
it.isSendDefaultPii = true
3978-
it.distinctId = null
3979-
}
3985+
val (sut, mockClient) = getEnabledScopes { it.distinctId = null }
39803986

39813987
sut.metrics().count("metric name")
39823988

0 commit comments

Comments
 (0)