Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve public facing API for creating Baggage from header #2284

Merged
merged 3 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features
adinauer marked this conversation as resolved.
Show resolved Hide resolved

- 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
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
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