Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

SentryUserFeedbackDialog.Builder(context).create().show()
```
- Add `user.id`, `user.name` and `user.email` to log attributes ([#4486](https://github.com/getsentry/sentry-java/pull/4486))

## 8.13.3

Expand Down
48 changes: 40 additions & 8 deletions sentry/src/main/java/io/sentry/logger/LoggerApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.sentry.SpanId;
import io.sentry.protocol.SdkVersion;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.User;
import io.sentry.util.Platform;
import io.sentry.util.TracingUtils;
import java.util.HashMap;
Expand Down Expand Up @@ -184,35 +185,43 @@ private void captureLog(
}
if (i > 0) {
attributes.put(
"sentry.message.template", new SentryLogEventAttributeValue("string", message));
"sentry.message.template",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, message));
}
}

final @Nullable SdkVersion sdkVersion = scopes.getOptions().getSdkVersion();
if (sdkVersion != null) {
attributes.put(
"sentry.sdk.name", new SentryLogEventAttributeValue("string", sdkVersion.getName()));
"sentry.sdk.name",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, sdkVersion.getName()));
attributes.put(
"sentry.sdk.version",
new SentryLogEventAttributeValue("string", sdkVersion.getVersion()));
new SentryLogEventAttributeValue(SentryAttributeType.STRING, sdkVersion.getVersion()));
}

final @Nullable String environment = scopes.getOptions().getEnvironment();
if (environment != null) {
attributes.put("sentry.environment", new SentryLogEventAttributeValue("string", environment));
attributes.put(
"sentry.environment",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, environment));
}
final @Nullable String release = scopes.getOptions().getRelease();
if (release != null) {
attributes.put("sentry.release", new SentryLogEventAttributeValue("string", release));
attributes.put(
"sentry.release", new SentryLogEventAttributeValue(SentryAttributeType.STRING, release));
}

attributes.put(
"sentry.trace.parent_span_id", new SentryLogEventAttributeValue("string", spanId));
"sentry.trace.parent_span_id",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, spanId));

if (Platform.isJvm()) {
setServerName(attributes);
}

setUser(attributes);

return attributes;
}

Expand All @@ -222,11 +231,34 @@ private void setServerName(
final @Nullable String optionsServerName = options.getServerName();
if (optionsServerName != null) {
attributes.put(
"server.address", new SentryLogEventAttributeValue("string", optionsServerName));
"server.address",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, optionsServerName));
} else if (options.isAttachServerName()) {
final @Nullable String hostname = HostnameCache.getInstance().getHostname();
if (hostname != null) {
attributes.put("server.address", new SentryLogEventAttributeValue("string", hostname));
attributes.put(
"server.address",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, hostname));
}
}
}

private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes) {
final @Nullable User user = scopes.getCombinedScopeView().getUser();
if (user != null) {
final @Nullable String id = user.getId();
if (id != null) {
attributes.put("user.id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, id));
}
final @Nullable String username = user.getUsername();
if (username != null) {
attributes.put(
"user.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, username));
}
final @Nullable String email = user.getEmail();
if (email != null) {
attributes.put(
"user.email", new SentryLogEventAttributeValue(SentryAttributeType.STRING, email));
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions sentry/src/main/java/io/sentry/protocol/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public final class User implements JsonUnknown, JsonSerializable {
/** Remote IP address of the user. */
private @Nullable String ipAddress;

/** Human readable name. */
private @Nullable String name;
/**
* @deprecated please use {@link User#username} Human readable name.
*/
@Deprecated private @Nullable String name;

/** User geo location. */
private @Nullable Geo geo;
Expand Down Expand Up @@ -215,7 +217,9 @@ public void setIpAddress(final @Nullable String ipAddress) {
* Get human readable name.
*
* @return Human readable name
* @deprecated please use {@link User#getUsername()}
*/
@Deprecated
public @Nullable String getName() {
return name;
}
Expand All @@ -224,7 +228,9 @@ public void setIpAddress(final @Nullable String ipAddress) {
* Set human readable name.
*
* @param name Human readable name
* @deprecated please use {@link User#setUsername(String)}
*/
@Deprecated
public void setName(final @Nullable String name) {
this.name = name;
}
Expand Down
35 changes: 35 additions & 0 deletions sentry/src/test/java/io/sentry/ScopesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2851,6 +2851,41 @@ class ScopesTest {
)
}

@Test
fun `adds user fields to log attributes`() {
val (sut, mockClient) = getEnabledScopes {
it.logs.isEnabled = true
}

sut.configureScope { scope ->
scope.user = User().also {
it.id = "usrid"
it.username = "usrname"
it.email = "user@sentry.io"
}
}
sut.logger().log(SentryLogLevel.WARN, "log message")

verify(mockClient).captureLog(
check {
assertEquals("log message", it.body)

val userId = it.attributes?.get("user.id")!!
assertEquals("usrid", userId.value)
assertEquals("string", userId.type)

val userName = it.attributes?.get("user.name")!!
assertEquals("usrname", userName.value)
assertEquals("string", userName.type)

val userEmail = it.attributes?.get("user.email")!!
assertEquals("user@sentry.io", userEmail.value)
assertEquals("string", userEmail.type)
},
anyOrNull()
)
}

//endregion

@Test
Expand Down