Skip to content

Commit

Permalink
Pass through unknown sentry baggage keys into SentryEnvelopeHeader (#…
Browse files Browse the repository at this point in the history
…2618)

Co-authored-by: Anton Pirker <[email protected]>
  • Loading branch information
adinauer and antonpirker authored Mar 28, 2023
1 parent d390819 commit 2ec7569
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

- Fix Automatic UI transactions having wrong durations ([#2623](https://github.com/getsentry/sentry-java/pull/2623))
- Fix wrong default environment in Session ([#2610](https://github.com/getsentry/sentry-java/pull/2610))
- Pass through unknown sentry baggage keys into SentryEnvelopeHeader ([#2618](https://github.com/getsentry/sentry-java/pull/2618))

## 6.16.0

Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class io/sentry/Baggage {
public fun getThirdPartyHeader ()Ljava/lang/String;
public fun getTraceId ()Ljava/lang/String;
public fun getTransaction ()Ljava/lang/String;
public fun getUnknown ()Ljava/util/Map;
public fun getUserId ()Ljava/lang/String;
public fun getUserSegment ()Ljava/lang/String;
public fun isMutable ()Z
Expand All @@ -62,6 +63,7 @@ public final class io/sentry/Baggage {
}

public final class io/sentry/Baggage$DSCKeys {
public static final field ALL Ljava/util/List;
public static final field ENVIRONMENT Ljava/lang/String;
public static final field PUBLIC_KEY Ljava/lang/String;
public static final field RELEASE Ljava/lang/String;
Expand Down
51 changes: 42 additions & 9 deletions sentry/src/main/java/io/sentry/Baggage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -312,6 +314,23 @@ public void set(final @NotNull String key, final @Nullable String value) {
}
}

@ApiStatus.Internal
public @NotNull Map<String, Object> getUnknown() {
final @NotNull Map<String, Object> unknown = new ConcurrentHashMap<>();
for (Map.Entry<String, String> keyValue : this.keyValues.entrySet()) {
final @NotNull String key = keyValue.getKey();
final @Nullable String value = keyValue.getValue();
if (!DSCKeys.ALL.contains(key)) {
if (value != null) {
final @NotNull String unknownKey = key.replaceFirst(SENTRY_BAGGAGE_PREFIX, "");
unknown.put(unknownKey, value);
}
}
}

return unknown;
}

@ApiStatus.Internal
public void setValuesFromTransaction(
final @NotNull ITransaction transaction,
Expand Down Expand Up @@ -390,15 +409,18 @@ public TraceContext toTraceContext() {
final String publicKey = getPublicKey();

if (traceIdString != null && publicKey != null) {
return new TraceContext(
new SentryId(traceIdString),
publicKey,
getRelease(),
getEnvironment(),
getUserId(),
getUserSegment(),
getTransaction(),
getSampleRate());
final @NotNull TraceContext traceContext =
new TraceContext(
new SentryId(traceIdString),
publicKey,
getRelease(),
getEnvironment(),
getUserId(),
getUserSegment(),
getTransaction(),
getSampleRate());
traceContext.setUnknown(getUnknown());
return traceContext;
} else {
return null;
}
Expand All @@ -414,5 +436,16 @@ public static final class DSCKeys {
public static final String USER_SEGMENT = "sentry-user_segment";
public static final String TRANSACTION = "sentry-transaction";
public static final String SAMPLE_RATE = "sentry-sample_rate";

public static final List<String> ALL =
Arrays.asList(
TRACE_ID,
PUBLIC_KEY,
RELEASE,
USER_ID,
ENVIRONMENT,
USER_SEGMENT,
TRANSACTION,
SAMPLE_RATE);
}
}
14 changes: 14 additions & 0 deletions sentry/src/test/java/io/sentry/BaggageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,20 @@ class BaggageTest {
assertEquals("sentry-trace_id=a,sentry-transaction=sentryTransaction", baggage.toHeaderString(null))
}

@Test
fun `unknown returns sentry- prefixed keys that are not known and passes them on to TraceContext`() {
val baggage = Baggage.fromHeader(listOf("sentry-trace_id=${SentryId()},sentry-public_key=b, sentry-replay_id=def", "sentry-transaction=sentryTransaction, sentry-anewkey=abc"))
val unknown = baggage.unknown
assertEquals(2, unknown.size)
assertEquals("def", unknown["replay_id"])
assertEquals("abc", unknown["anewkey"])

val traceContext = baggage.toTraceContext()!!
assertEquals(2, traceContext.unknown!!.size)
assertEquals("def", traceContext.unknown!!["replay_id"])
assertEquals("abc", traceContext.unknown!!["anewkey"])
}

/**
* token = 1*tchar
* tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
Expand Down

0 comments on commit 2ec7569

Please sign in to comment.