Skip to content

Commit fcec2f2

Browse files
authored
fix(logger): fallback to distinct-id when user is not set (#4847)
* fix(logger): fallback to distinct-id when user is not set * Update Changelog * Update Changelog.md
1 parent 2124a46 commit fcec2f2

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Improvements
6+
7+
- Fallback to distinct-id as user.id logging attribute when user is not set ([#4847](https://github.com/getsentry/sentry-java/pull/4847))
8+
39
## 8.25.0
410

511
### Fixes

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,14 @@ private void setServerName(
270270

271271
private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes) {
272272
final @Nullable User user = scopes.getCombinedScopeView().getUser();
273-
if (user != null) {
273+
if (user == null) {
274+
// In case no user is set, we should fallback to the distinct id, known as installation id,
275+
// which is used on Android as default user id
276+
final @Nullable String id = scopes.getOptions().getDistinctId();
277+
if (id != null) {
278+
attributes.put("user.id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, id));
279+
}
280+
} else {
274281
final @Nullable String id = user.getId();
275282
if (id != null) {
276283
attributes.put("user.id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, id));

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

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,7 +2875,11 @@ class ScopesTest {
28752875

28762876
@Test
28772877
fun `adds user fields to log attributes`() {
2878-
val (sut, mockClient) = getEnabledScopes { it.logs.isEnabled = true }
2878+
val (sut, mockClient) =
2879+
getEnabledScopes {
2880+
it.logs.isEnabled = true
2881+
it.distinctId = "distinctId"
2882+
}
28792883

28802884
sut.configureScope { scope ->
28812885
scope.user =
@@ -2909,11 +2913,12 @@ class ScopesTest {
29092913
}
29102914

29112915
@Test
2912-
fun `missing user does not break attributes`() {
2916+
fun `unset user does provide distinct-id as user-id`() {
29132917
val (sut, mockClient) =
29142918
getEnabledScopes {
29152919
it.logs.isEnabled = true
29162920
it.isSendDefaultPii = true
2921+
it.distinctId = "distinctId"
29172922
}
29182923

29192924
sut.logger().log(SentryLogLevel.WARN, "log message")
@@ -2923,6 +2928,29 @@ class ScopesTest {
29232928
check {
29242929
assertEquals("log message", it.body)
29252930

2931+
assertEquals("distinctId", it.attributes?.get("user.id")?.value)
2932+
assertNull(it.attributes?.get("user.name"))
2933+
assertNull(it.attributes?.get("user.email"))
2934+
},
2935+
anyOrNull(),
2936+
)
2937+
}
2938+
2939+
@Test
2940+
fun `unset user does provide null user-id when distinct-id is missing`() {
2941+
val (sut, mockClient) =
2942+
getEnabledScopes {
2943+
it.logs.isEnabled = true
2944+
it.isSendDefaultPii = true
2945+
it.distinctId = null
2946+
}
2947+
2948+
sut.logger().log(SentryLogLevel.WARN, "log message")
2949+
2950+
verify(mockClient)
2951+
.captureLog(
2952+
check {
2953+
assertEquals("log message", it.body)
29262954
assertNull(it.attributes?.get("user.id"))
29272955
assertNull(it.attributes?.get("user.name"))
29282956
assertNull(it.attributes?.get("user.email"))
@@ -2937,6 +2965,7 @@ class ScopesTest {
29372965
getEnabledScopes {
29382966
it.logs.isEnabled = true
29392967
it.isSendDefaultPii = true
2968+
it.distinctId = "distinctId"
29402969
}
29412970

29422971
sut.configureScope { scope -> scope.user = User() }

0 commit comments

Comments
 (0)