Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- Only send userid in `baggage` if `sendDefaultPii` is `true` ([#2145](https://github.com/getsentry/sentry-java/pull/2145))

### Features

- New package `sentry-android-navigation` for AndroidX Navigation support ([#2136](https://github.com/getsentry/sentry-java/pull/2136))
Expand Down
10 changes: 5 additions & 5 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public final class io/sentry/Attachment {
}

public final class io/sentry/Baggage {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Ljava/util/Map;Lio/sentry/ILogger;)V
public static fun fromHeader (Ljava/lang/String;Lio/sentry/ILogger;)Lio/sentry/Baggage;
public static fun fromHeader (Ljava/util/List;Lio/sentry/ILogger;)Lio/sentry/Baggage;
public fun <init> (Lio/sentry/SentryOptions;)V
public fun <init> (Ljava/util/Map;Lio/sentry/SentryOptions;)V
public static fun fromHeader (Ljava/lang/String;Lio/sentry/SentryOptions;)Lio/sentry/Baggage;
public static fun fromHeader (Ljava/util/List;Lio/sentry/SentryOptions;)Lio/sentry/Baggage;
public fun get (Ljava/lang/String;)Ljava/lang/String;
public fun set (Ljava/lang/String;Ljava/lang/String;)V
public fun setEnvironment (Ljava/lang/String;)V
Expand Down Expand Up @@ -1634,7 +1634,7 @@ public final class io/sentry/TraceContext : io/sentry/JsonSerializable, io/sentr
public fun getUserSegment ()Ljava/lang/String;
public fun serialize (Lio/sentry/JsonObjectWriter;Lio/sentry/ILogger;)V
public fun setUnknown (Ljava/util/Map;)V
public fun toBaggage (Lio/sentry/ILogger;)Lio/sentry/Baggage;
public fun toBaggage (Lio/sentry/SentryOptions;)Lio/sentry/Baggage;
}

public final class io/sentry/TraceContext$Deserializer : io/sentry/JsonDeserializer {
Expand Down
88 changes: 54 additions & 34 deletions sentry/src/main/java/io/sentry/Baggage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ public final class Baggage {
static final @NotNull Integer MAX_BAGGAGE_LIST_MEMBER_COUNT = 64;

final @NotNull Map<String, String> keyValues;
final @NotNull ILogger logger;
final @NotNull SentryOptions options;

public static Baggage fromHeader(
final @Nullable List<String> headerValues, final @NotNull ILogger logger) {
final @Nullable List<String> headerValues, final @NotNull SentryOptions options) {
final Map<String, String> keyValues = new HashMap<>();

if (headerValues != null) {
for (final @Nullable String headerValue : headerValues) {
final Map<String, String> keyValuesToAdd =
extractKeyValuesFromBaggageString(headerValue, logger);
extractKeyValuesFromBaggageString(headerValue, options);
keyValues.putAll(keyValuesToAdd);
}
}

return new Baggage(keyValues, logger);
return new Baggage(keyValues, options);
}

public static Baggage fromHeader(
final @Nullable String headerValue, final @NotNull ILogger logger) {
final Map<String, String> keyValues = extractKeyValuesFromBaggageString(headerValue, logger);
return new Baggage(keyValues, logger);
final @Nullable String headerValue, final @NotNull SentryOptions options) {
final Map<String, String> keyValues = extractKeyValuesFromBaggageString(headerValue, options);
return new Baggage(keyValues, options);
}

private static Map<String, String> extractKeyValuesFromBaggageString(
final @Nullable String headerValue, final @NotNull ILogger logger) {
final @Nullable String headerValue, final @NotNull SentryOptions options) {
final @NotNull Map<String, String> keyValues = new HashMap<>();

if (headerValue != null) {
Expand All @@ -62,28 +62,40 @@ private static Map<String, String> extractKeyValuesFromBaggageString(

keyValues.put(keyDecoded, valueDecoded);
} else {
logger.log(
SentryLevel.ERROR, "Unable to decode baggage key value pair %s", keyValueString);
options
.getLogger()
.log(
SentryLevel.ERROR,
"Unable to decode baggage key value pair %s",
keyValueString);
}
} catch (Throwable e) {
logger.log(
SentryLevel.ERROR, e, "Unable to decode baggage key value pair %s", keyValueString);
options
.getLogger()
.log(
SentryLevel.ERROR,
e,
"Unable to decode baggage key value pair %s",
keyValueString);
}
}
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, e, "Unable to decode baggage header %s", headerValue);
options
.getLogger()
.log(SentryLevel.ERROR, e, "Unable to decode baggage header %s", headerValue);
}
}
return keyValues;
}

public Baggage(final @NotNull ILogger logger) {
this(new HashMap<>(), logger);
public Baggage(final @NotNull SentryOptions options) {
this(new HashMap<>(), options);
}

public Baggage(final @NotNull Map<String, String> keyValues, final @NotNull ILogger logger) {
public Baggage(
final @NotNull Map<String, String> keyValues, final @NotNull SentryOptions options) {
this.keyValues = keyValues;
this.logger = logger;
this.options = options;
}

public @NotNull String toHeaderString() {
Expand All @@ -97,11 +109,13 @@ public Baggage(final @NotNull Map<String, String> keyValues, final @NotNull ILog

if (value != null) {
if (listMemberCount >= MAX_BAGGAGE_LIST_MEMBER_COUNT) {
logger.log(
SentryLevel.ERROR,
"Not adding baggage value %s as the total number of list members would exceed the maximum of %s.",
key,
MAX_BAGGAGE_LIST_MEMBER_COUNT);
options
.getLogger()
.log(
SentryLevel.ERROR,
"Not adding baggage value %s as the total number of list members would exceed the maximum of %s.",
key,
MAX_BAGGAGE_LIST_MEMBER_COUNT);
} else {
try {
final String encodedKey = encode(key);
Expand All @@ -111,23 +125,27 @@ public Baggage(final @NotNull Map<String, String> keyValues, final @NotNull ILog
final int valueLength = encodedKeyValue.length();
final int totalLengthIfValueAdded = sb.length() + valueLength;
if (totalLengthIfValueAdded > MAX_BAGGAGE_STRING_LENGTH) {
logger.log(
SentryLevel.ERROR,
"Not adding baggage value %s as the total header value length would exceed the maximum of %s.",
key,
MAX_BAGGAGE_STRING_LENGTH);
options
.getLogger()
.log(
SentryLevel.ERROR,
"Not adding baggage value %s as the total header value length would exceed the maximum of %s.",
key,
MAX_BAGGAGE_STRING_LENGTH);
} else {
listMemberCount++;
sb.append(encodedKeyValue);
separator = ",";
}
} catch (Throwable e) {
logger.log(
SentryLevel.ERROR,
e,
"Unable to encode baggage key value pair (key=%s,value=%s).",
key,
value);
options
.getLogger()
.log(
SentryLevel.ERROR,
e,
"Unable to encode baggage key value pair (key=%s,value=%s).",
key,
value);
}
}
}
Expand Down Expand Up @@ -169,7 +187,9 @@ public void setRelease(final @Nullable String release) {
}

public void setUserId(final @Nullable String userId) {
set("sentry-user_id", userId);
if (options.isSendDefaultPii()) {
set("sentry-user_id", userId);
}
}

public void setUserSegment(final @Nullable String userSegment) {
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/SentryTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public void finish(@Nullable SpanStatus status) {
public @Nullable BaggageHeader toBaggageHeader() {
final TraceContext traceContext = traceContext();
if (hub.getOptions().isTraceSampling() && traceContext != null) {
final Baggage baggage = traceContext.toBaggage(hub.getOptions().getLogger());
final Baggage baggage = traceContext.toBaggage(hub.getOptions());
return new BaggageHeader(baggage);
} else {
return null;
Expand Down
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/TraceContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public final class TraceContext implements JsonUnknown, JsonSerializable {
return sampleRate;
}

public @NotNull Baggage toBaggage(@NotNull ILogger logger) {
Baggage baggage = new Baggage(logger);
public @NotNull Baggage toBaggage(@NotNull SentryOptions options) {
Baggage baggage = new Baggage(options);

baggage.setTraceId(traceId.toString());
baggage.setPublicKey(publicKey);
Expand Down
Loading