Skip to content

Commit 13cd8e8

Browse files
authored
Merge branch 'main' into markushi/feat/log-timber-tags
2 parents 517bbce + fcec2f2 commit 13cd8e8

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Improvements
66

7+
- Fallback to distinct-id as user.id logging attribute when user is not set ([#4847](https://github.com/getsentry/sentry-java/pull/4847))
78
- Report Timber.tag() as `timber.tag` log attribute ([#4845](https://github.com/getsentry/sentry-java/pull/4845))
89

910
## 8.25.0

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)