Skip to content

Commit

Permalink
Improve public facing API for creating Baggage from header (#2284)
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed Oct 10, 2022
1 parent 4dd88fe commit ece81f7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Improve public facing API for creating Baggage from header ([#2284](https://github.com/getsentry/sentry-java/pull/2284))

## 6.5.0-beta.3

### Features
Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public final class io/sentry/Baggage {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Ljava/util/Map;Ljava/lang/String;ZLio/sentry/ILogger;)V
public fun freeze ()V
public static fun fromHeader (Ljava/lang/String;)Lio/sentry/Baggage;
public static fun fromHeader (Ljava/lang/String;Lio/sentry/ILogger;)Lio/sentry/Baggage;
public static fun fromHeader (Ljava/lang/String;ZLio/sentry/ILogger;)Lio/sentry/Baggage;
public static fun fromHeader (Ljava/util/List;)Lio/sentry/Baggage;
public static fun fromHeader (Ljava/util/List;Lio/sentry/ILogger;)Lio/sentry/Baggage;
public static fun fromHeader (Ljava/util/List;ZLio/sentry/ILogger;)Lio/sentry/Baggage;
public fun get (Ljava/lang/String;)Ljava/lang/String;
Expand Down
52 changes: 47 additions & 5 deletions sentry/src/main/java/io/sentry/Baggage.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,32 @@ public final class Baggage {
private boolean mutable;
final @NotNull ILogger logger;

@NotNull
public static Baggage fromHeader(final String headerValue) {
return Baggage.fromHeader(
headerValue, false, HubAdapter.getInstance().getOptions().getLogger());
}

@NotNull
public static Baggage fromHeader(final @Nullable List<String> headerValues) {
return Baggage.fromHeader(
headerValues, false, HubAdapter.getInstance().getOptions().getLogger());
}

@ApiStatus.Internal
@NotNull
public static Baggage fromHeader(final String headerValue, final @NotNull ILogger logger) {
return Baggage.fromHeader(headerValue, false, logger);
}

@ApiStatus.Internal
@NotNull
public static Baggage fromHeader(
final @Nullable List<String> headerValues, final @NotNull ILogger logger) {
return Baggage.fromHeader(headerValues, false, logger);
}

@ApiStatus.Internal
@NotNull
public static Baggage fromHeader(
final @Nullable List<String> headerValues,
Expand All @@ -53,11 +73,7 @@ public static Baggage fromHeader(
}
}

@NotNull
public static Baggage fromHeader(final String headerValue, final @NotNull ILogger logger) {
return Baggage.fromHeader(headerValue, false, logger);
}

@ApiStatus.Internal
@NotNull
public static Baggage fromHeader(
final @Nullable String headerValue,
Expand Down Expand Up @@ -104,10 +120,12 @@ public static Baggage fromHeader(
return new Baggage(keyValues, thirdPartyHeader, mutable, logger);
}

@ApiStatus.Internal
public Baggage(final @NotNull ILogger logger) {
this(new HashMap<>(), null, true, logger);
}

@ApiStatus.Internal
public Baggage(
final @NotNull Map<String, String> keyValues,
final @Nullable String thirdPartyHeader,
Expand All @@ -119,10 +137,12 @@ public Baggage(
this.thirdPartyHeader = thirdPartyHeader;
}

@ApiStatus.Internal
public void freeze() {
this.mutable = false;
}

@ApiStatus.Internal
public boolean isMutable() {
return mutable;
}
Expand Down Expand Up @@ -196,6 +216,7 @@ private static String decode(final @NotNull String value) throws UnsupportedEnco
return URLDecoder.decode(value, CHARSET);
}

@ApiStatus.Internal
public @Nullable String get(final @Nullable String key) {
if (key == null) {
return null;
Expand All @@ -204,76 +225,94 @@ private static String decode(final @NotNull String value) throws UnsupportedEnco
return keyValues.get(key);
}

@ApiStatus.Internal
public @Nullable String getTraceId() {
return get(DSCKeys.TRACE_ID);
}

@ApiStatus.Internal
public void setTraceId(final @Nullable String traceId) {
set(DSCKeys.TRACE_ID, traceId);
}

@ApiStatus.Internal
public @Nullable String getPublicKey() {
return get(DSCKeys.PUBLIC_KEY);
}

@ApiStatus.Internal
public void setPublicKey(final @Nullable String publicKey) {
set(DSCKeys.PUBLIC_KEY, publicKey);
}

@ApiStatus.Internal
public @Nullable String getEnvironment() {
return get(DSCKeys.ENVIRONMENT);
}

@ApiStatus.Internal
public void setEnvironment(final @Nullable String environment) {
set(DSCKeys.ENVIRONMENT, environment);
}

@ApiStatus.Internal
public @Nullable String getRelease() {
return get(DSCKeys.RELEASE);
}

@ApiStatus.Internal
public void setRelease(final @Nullable String release) {
set(DSCKeys.RELEASE, release);
}

@ApiStatus.Internal
public @Nullable String getUserId() {
return get(DSCKeys.USER_ID);
}

@ApiStatus.Internal
public void setUserId(final @Nullable String userId) {
set(DSCKeys.USER_ID, userId);
}

@ApiStatus.Internal
public @Nullable String getUserSegment() {
return get(DSCKeys.USER_SEGMENT);
}

@ApiStatus.Internal
public void setUserSegment(final @Nullable String userSegment) {
set(DSCKeys.USER_SEGMENT, userSegment);
}

@ApiStatus.Internal
public @Nullable String getTransaction() {
return get(DSCKeys.TRANSACTION);
}

@ApiStatus.Internal
public void setTransaction(final @Nullable String transaction) {
set(DSCKeys.TRANSACTION, transaction);
}

@ApiStatus.Internal
public @Nullable String getSampleRate() {
return get(DSCKeys.SAMPLE_RATE);
}

@ApiStatus.Internal
public void setSampleRate(final @Nullable String sampleRate) {
set(DSCKeys.SAMPLE_RATE, sampleRate);
}

@ApiStatus.Internal
public void set(final @NotNull String key, final @Nullable String value) {
if (mutable) {
this.keyValues.put(key, value);
}
}

@ApiStatus.Internal
public void setValuesFromTransaction(
final @NotNull ITransaction transaction,
final @Nullable User user,
Expand Down Expand Up @@ -328,6 +367,7 @@ private static boolean isHighQualityTransactionName(
&& !TransactionNameSource.URL.equals(transactionNameSource);
}

@ApiStatus.Internal
public @Nullable Double getSampleRateDouble() {
final String sampleRateString = getSampleRate();
if (sampleRateString != null) {
Expand All @@ -343,6 +383,7 @@ private static boolean isHighQualityTransactionName(
return null;
}

@ApiStatus.Internal
@Nullable
public TraceContext toTraceContext() {
final String traceIdString = getTraceId();
Expand All @@ -363,6 +404,7 @@ public TraceContext toTraceContext() {
}
}

@ApiStatus.Internal
public static final class DSCKeys {
public static final String TRACE_ID = "sentry-trace_id";
public static final String PUBLIC_KEY = "sentry-public_key";
Expand Down
12 changes: 12 additions & 0 deletions sentry/src/test/java/io/sentry/BaggageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,18 @@ class BaggageTest {
assertEquals("%22%28%29%2C%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%7B%7D=value", baggage.toHeaderString(null))
}

@Test
fun `can skip logger for header from single string`() {
val baggage = Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction")
assertEquals("sentry-trace_id=a,sentry-transaction=sentryTransaction", baggage.toHeaderString(null))
}

@Test
fun `can skip logger for header from list of strings`() {
val baggage = Baggage.fromHeader(listOf("sentry-trace_id=a", "sentry-transaction=sentryTransaction"))
assertEquals("sentry-trace_id=a,sentry-transaction=sentryTransaction", baggage.toHeaderString(null))
}

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

0 comments on commit ece81f7

Please sign in to comment.