Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 23 additions & 0 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 @@ -213,6 +214,10 @@ private void captureLog(
setServerName(attributes);
}

if (scopes.getOptions().isSendDefaultPii()) {
Comment thread
adinauer marked this conversation as resolved.
Outdated
setUser(attributes);
}

return attributes;
}

Expand All @@ -231,6 +236,24 @@ private void setServerName(
}
}

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("string", id));
Comment thread
adinauer marked this conversation as resolved.
Outdated
}
final @Nullable String username = user.getUsername();
if (username != null) {
attributes.put("user.name", new SentryLogEventAttributeValue("string", username));
Comment thread
adinauer marked this conversation as resolved.
Outdated
}
final @Nullable String email = user.getEmail();
if (email != null) {
attributes.put("user.email", new SentryLogEventAttributeValue("string", email));
Comment thread
adinauer marked this conversation as resolved.
Outdated
}
}
}

private @NotNull SentryAttributeType getType(final @Nullable Object arg) {
if (arg instanceof Boolean) {
return SentryAttributeType.BOOLEAN;
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
63 changes: 63 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,69 @@ class ScopesTest {
)
}

@Test
fun `adds user fields to log attributes if sendDefaultPii is true`() {
val (sut, mockClient) = getEnabledScopes {
it.logs.isEnabled = true
it.isSendDefaultPii = 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()
)
}

@Test
fun `does not add user fields to log attributes by default`() {
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)

assertNull(it.attributes?.get("user.id"))
assertNull(it.attributes?.get("user.name"))
assertNull(it.attributes?.get("user.email"))
},
anyOrNull()
)
}

//endregion

@Test
Expand Down
Loading