Skip to content

Commit 5fde6b6

Browse files
Add user.id, user.name and user.email to log attributes (#4486)
* Add user id, username and email to log attributes * changelog * use enum for attribute type * no longer check isSendDefaultPii * Format code * ignore IntelliJ / Android Studio run configs * suppress deprecation warning for user.name --------- Co-authored-by: Sentry Github Bot <bot+github-bot@sentry.io>
1 parent 8bfa9cc commit 5fde6b6

File tree

6 files changed

+86
-10
lines changed

6 files changed

+86
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
.idea/
33
.gradle/
4+
.run/
45
build/
56
artifacts/
67
out/

CHANGELOG.md

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

2323
SentryUserFeedbackDialog.Builder(context).create().show()
2424
```
25+
- Add `user.id`, `user.name` and `user.email` to log attributes ([#4486](https://github.com/getsentry/sentry-java/pull/4486))
2526
- Serialize `preContext` and `postContext` in `SentryStackFrame` ([#4482](https://github.com/getsentry/sentry-java/pull/4482))
2627

2728
## 8.13.3

sentry-android-core/src/main/java/io/sentry/android/core/SentryUserFeedbackDialog.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public void setCancelable(boolean cancelable) {
4343
}
4444

4545
@Override
46+
@SuppressWarnings("deprecation")
4647
protected void onCreate(Bundle savedInstanceState) {
4748
super.onCreate(savedInstanceState);
4849
setContentView(R.layout.sentry_dialog_user_feedback);

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.sentry.SpanId;
1818
import io.sentry.protocol.SdkVersion;
1919
import io.sentry.protocol.SentryId;
20+
import io.sentry.protocol.User;
2021
import io.sentry.util.Platform;
2122
import io.sentry.util.TracingUtils;
2223
import java.util.HashMap;
@@ -184,35 +185,43 @@ private void captureLog(
184185
}
185186
if (i > 0) {
186187
attributes.put(
187-
"sentry.message.template", new SentryLogEventAttributeValue("string", message));
188+
"sentry.message.template",
189+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, message));
188190
}
189191
}
190192

191193
final @Nullable SdkVersion sdkVersion = scopes.getOptions().getSdkVersion();
192194
if (sdkVersion != null) {
193195
attributes.put(
194-
"sentry.sdk.name", new SentryLogEventAttributeValue("string", sdkVersion.getName()));
196+
"sentry.sdk.name",
197+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, sdkVersion.getName()));
195198
attributes.put(
196199
"sentry.sdk.version",
197-
new SentryLogEventAttributeValue("string", sdkVersion.getVersion()));
200+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, sdkVersion.getVersion()));
198201
}
199202

200203
final @Nullable String environment = scopes.getOptions().getEnvironment();
201204
if (environment != null) {
202-
attributes.put("sentry.environment", new SentryLogEventAttributeValue("string", environment));
205+
attributes.put(
206+
"sentry.environment",
207+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, environment));
203208
}
204209
final @Nullable String release = scopes.getOptions().getRelease();
205210
if (release != null) {
206-
attributes.put("sentry.release", new SentryLogEventAttributeValue("string", release));
211+
attributes.put(
212+
"sentry.release", new SentryLogEventAttributeValue(SentryAttributeType.STRING, release));
207213
}
208214

209215
attributes.put(
210-
"sentry.trace.parent_span_id", new SentryLogEventAttributeValue("string", spanId));
216+
"sentry.trace.parent_span_id",
217+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, spanId));
211218

212219
if (Platform.isJvm()) {
213220
setServerName(attributes);
214221
}
215222

223+
setUser(attributes);
224+
216225
return attributes;
217226
}
218227

@@ -222,11 +231,34 @@ private void setServerName(
222231
final @Nullable String optionsServerName = options.getServerName();
223232
if (optionsServerName != null) {
224233
attributes.put(
225-
"server.address", new SentryLogEventAttributeValue("string", optionsServerName));
234+
"server.address",
235+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, optionsServerName));
226236
} else if (options.isAttachServerName()) {
227237
final @Nullable String hostname = HostnameCache.getInstance().getHostname();
228238
if (hostname != null) {
229-
attributes.put("server.address", new SentryLogEventAttributeValue("string", hostname));
239+
attributes.put(
240+
"server.address",
241+
new SentryLogEventAttributeValue(SentryAttributeType.STRING, hostname));
242+
}
243+
}
244+
}
245+
246+
private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes) {
247+
final @Nullable User user = scopes.getCombinedScopeView().getUser();
248+
if (user != null) {
249+
final @Nullable String id = user.getId();
250+
if (id != null) {
251+
attributes.put("user.id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, id));
252+
}
253+
final @Nullable String username = user.getUsername();
254+
if (username != null) {
255+
attributes.put(
256+
"user.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, username));
257+
}
258+
final @Nullable String email = user.getEmail();
259+
if (email != null) {
260+
attributes.put(
261+
"user.email", new SentryLogEventAttributeValue(SentryAttributeType.STRING, email));
230262
}
231263
}
232264
}

sentry/src/main/java/io/sentry/protocol/User.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public final class User implements JsonUnknown, JsonSerializable {
3737
/** Remote IP address of the user. */
3838
private @Nullable String ipAddress;
3939

40-
/** Human readable name. */
41-
private @Nullable String name;
40+
/**
41+
* @deprecated please use {@link User#username} Human readable name.
42+
*/
43+
@Deprecated private @Nullable String name;
4244

4345
/** User geo location. */
4446
private @Nullable Geo geo;
@@ -215,7 +217,9 @@ public void setIpAddress(final @Nullable String ipAddress) {
215217
* Get human readable name.
216218
*
217219
* @return Human readable name
220+
* @deprecated please use {@link User#getUsername()}
218221
*/
222+
@Deprecated
219223
public @Nullable String getName() {
220224
return name;
221225
}
@@ -224,7 +228,9 @@ public void setIpAddress(final @Nullable String ipAddress) {
224228
* Set human readable name.
225229
*
226230
* @param name Human readable name
231+
* @deprecated please use {@link User#setUsername(String)}
227232
*/
233+
@Deprecated
228234
public void setName(final @Nullable String name) {
229235
this.name = name;
230236
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,41 @@ class ScopesTest {
28512851
)
28522852
}
28532853

2854+
@Test
2855+
fun `adds user fields to log attributes`() {
2856+
val (sut, mockClient) = getEnabledScopes {
2857+
it.logs.isEnabled = true
2858+
}
2859+
2860+
sut.configureScope { scope ->
2861+
scope.user = User().also {
2862+
it.id = "usrid"
2863+
it.username = "usrname"
2864+
it.email = "user@sentry.io"
2865+
}
2866+
}
2867+
sut.logger().log(SentryLogLevel.WARN, "log message")
2868+
2869+
verify(mockClient).captureLog(
2870+
check {
2871+
assertEquals("log message", it.body)
2872+
2873+
val userId = it.attributes?.get("user.id")!!
2874+
assertEquals("usrid", userId.value)
2875+
assertEquals("string", userId.type)
2876+
2877+
val userName = it.attributes?.get("user.name")!!
2878+
assertEquals("usrname", userName.value)
2879+
assertEquals("string", userName.type)
2880+
2881+
val userEmail = it.attributes?.get("user.email")!!
2882+
assertEquals("user@sentry.io", userEmail.value)
2883+
assertEquals("string", userEmail.type)
2884+
},
2885+
anyOrNull()
2886+
)
2887+
}
2888+
28542889
//endregion
28552890

28562891
@Test

0 commit comments

Comments
 (0)