Skip to content

Commit ece81f7

Browse files
authored
Improve public facing API for creating Baggage from header (#2284)
1 parent 4dd88fe commit ece81f7

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Improve public facing API for creating Baggage from header ([#2284](https://github.com/getsentry/sentry-java/pull/2284))
8+
39
## 6.5.0-beta.3
410

511
### Features

sentry/api/sentry.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public final class io/sentry/Baggage {
2424
public fun <init> (Lio/sentry/ILogger;)V
2525
public fun <init> (Ljava/util/Map;Ljava/lang/String;ZLio/sentry/ILogger;)V
2626
public fun freeze ()V
27+
public static fun fromHeader (Ljava/lang/String;)Lio/sentry/Baggage;
2728
public static fun fromHeader (Ljava/lang/String;Lio/sentry/ILogger;)Lio/sentry/Baggage;
2829
public static fun fromHeader (Ljava/lang/String;ZLio/sentry/ILogger;)Lio/sentry/Baggage;
30+
public static fun fromHeader (Ljava/util/List;)Lio/sentry/Baggage;
2931
public static fun fromHeader (Ljava/util/List;Lio/sentry/ILogger;)Lio/sentry/Baggage;
3032
public static fun fromHeader (Ljava/util/List;ZLio/sentry/ILogger;)Lio/sentry/Baggage;
3133
public fun get (Ljava/lang/String;)Ljava/lang/String;

sentry/src/main/java/io/sentry/Baggage.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,32 @@ public final class Baggage {
3434
private boolean mutable;
3535
final @NotNull ILogger logger;
3636

37+
@NotNull
38+
public static Baggage fromHeader(final String headerValue) {
39+
return Baggage.fromHeader(
40+
headerValue, false, HubAdapter.getInstance().getOptions().getLogger());
41+
}
42+
43+
@NotNull
44+
public static Baggage fromHeader(final @Nullable List<String> headerValues) {
45+
return Baggage.fromHeader(
46+
headerValues, false, HubAdapter.getInstance().getOptions().getLogger());
47+
}
48+
49+
@ApiStatus.Internal
50+
@NotNull
51+
public static Baggage fromHeader(final String headerValue, final @NotNull ILogger logger) {
52+
return Baggage.fromHeader(headerValue, false, logger);
53+
}
54+
55+
@ApiStatus.Internal
3756
@NotNull
3857
public static Baggage fromHeader(
3958
final @Nullable List<String> headerValues, final @NotNull ILogger logger) {
4059
return Baggage.fromHeader(headerValues, false, logger);
4160
}
4261

62+
@ApiStatus.Internal
4363
@NotNull
4464
public static Baggage fromHeader(
4565
final @Nullable List<String> headerValues,
@@ -53,11 +73,7 @@ public static Baggage fromHeader(
5373
}
5474
}
5575

56-
@NotNull
57-
public static Baggage fromHeader(final String headerValue, final @NotNull ILogger logger) {
58-
return Baggage.fromHeader(headerValue, false, logger);
59-
}
60-
76+
@ApiStatus.Internal
6177
@NotNull
6278
public static Baggage fromHeader(
6379
final @Nullable String headerValue,
@@ -104,10 +120,12 @@ public static Baggage fromHeader(
104120
return new Baggage(keyValues, thirdPartyHeader, mutable, logger);
105121
}
106122

123+
@ApiStatus.Internal
107124
public Baggage(final @NotNull ILogger logger) {
108125
this(new HashMap<>(), null, true, logger);
109126
}
110127

128+
@ApiStatus.Internal
111129
public Baggage(
112130
final @NotNull Map<String, String> keyValues,
113131
final @Nullable String thirdPartyHeader,
@@ -119,10 +137,12 @@ public Baggage(
119137
this.thirdPartyHeader = thirdPartyHeader;
120138
}
121139

140+
@ApiStatus.Internal
122141
public void freeze() {
123142
this.mutable = false;
124143
}
125144

145+
@ApiStatus.Internal
126146
public boolean isMutable() {
127147
return mutable;
128148
}
@@ -196,6 +216,7 @@ private static String decode(final @NotNull String value) throws UnsupportedEnco
196216
return URLDecoder.decode(value, CHARSET);
197217
}
198218

219+
@ApiStatus.Internal
199220
public @Nullable String get(final @Nullable String key) {
200221
if (key == null) {
201222
return null;
@@ -204,76 +225,94 @@ private static String decode(final @NotNull String value) throws UnsupportedEnco
204225
return keyValues.get(key);
205226
}
206227

228+
@ApiStatus.Internal
207229
public @Nullable String getTraceId() {
208230
return get(DSCKeys.TRACE_ID);
209231
}
210232

233+
@ApiStatus.Internal
211234
public void setTraceId(final @Nullable String traceId) {
212235
set(DSCKeys.TRACE_ID, traceId);
213236
}
214237

238+
@ApiStatus.Internal
215239
public @Nullable String getPublicKey() {
216240
return get(DSCKeys.PUBLIC_KEY);
217241
}
218242

243+
@ApiStatus.Internal
219244
public void setPublicKey(final @Nullable String publicKey) {
220245
set(DSCKeys.PUBLIC_KEY, publicKey);
221246
}
222247

248+
@ApiStatus.Internal
223249
public @Nullable String getEnvironment() {
224250
return get(DSCKeys.ENVIRONMENT);
225251
}
226252

253+
@ApiStatus.Internal
227254
public void setEnvironment(final @Nullable String environment) {
228255
set(DSCKeys.ENVIRONMENT, environment);
229256
}
230257

258+
@ApiStatus.Internal
231259
public @Nullable String getRelease() {
232260
return get(DSCKeys.RELEASE);
233261
}
234262

263+
@ApiStatus.Internal
235264
public void setRelease(final @Nullable String release) {
236265
set(DSCKeys.RELEASE, release);
237266
}
238267

268+
@ApiStatus.Internal
239269
public @Nullable String getUserId() {
240270
return get(DSCKeys.USER_ID);
241271
}
242272

273+
@ApiStatus.Internal
243274
public void setUserId(final @Nullable String userId) {
244275
set(DSCKeys.USER_ID, userId);
245276
}
246277

278+
@ApiStatus.Internal
247279
public @Nullable String getUserSegment() {
248280
return get(DSCKeys.USER_SEGMENT);
249281
}
250282

283+
@ApiStatus.Internal
251284
public void setUserSegment(final @Nullable String userSegment) {
252285
set(DSCKeys.USER_SEGMENT, userSegment);
253286
}
254287

288+
@ApiStatus.Internal
255289
public @Nullable String getTransaction() {
256290
return get(DSCKeys.TRANSACTION);
257291
}
258292

293+
@ApiStatus.Internal
259294
public void setTransaction(final @Nullable String transaction) {
260295
set(DSCKeys.TRANSACTION, transaction);
261296
}
262297

298+
@ApiStatus.Internal
263299
public @Nullable String getSampleRate() {
264300
return get(DSCKeys.SAMPLE_RATE);
265301
}
266302

303+
@ApiStatus.Internal
267304
public void setSampleRate(final @Nullable String sampleRate) {
268305
set(DSCKeys.SAMPLE_RATE, sampleRate);
269306
}
270307

308+
@ApiStatus.Internal
271309
public void set(final @NotNull String key, final @Nullable String value) {
272310
if (mutable) {
273311
this.keyValues.put(key, value);
274312
}
275313
}
276314

315+
@ApiStatus.Internal
277316
public void setValuesFromTransaction(
278317
final @NotNull ITransaction transaction,
279318
final @Nullable User user,
@@ -328,6 +367,7 @@ private static boolean isHighQualityTransactionName(
328367
&& !TransactionNameSource.URL.equals(transactionNameSource);
329368
}
330369

370+
@ApiStatus.Internal
331371
public @Nullable Double getSampleRateDouble() {
332372
final String sampleRateString = getSampleRate();
333373
if (sampleRateString != null) {
@@ -343,6 +383,7 @@ private static boolean isHighQualityTransactionName(
343383
return null;
344384
}
345385

386+
@ApiStatus.Internal
346387
@Nullable
347388
public TraceContext toTraceContext() {
348389
final String traceIdString = getTraceId();
@@ -363,6 +404,7 @@ public TraceContext toTraceContext() {
363404
}
364405
}
365406

407+
@ApiStatus.Internal
366408
public static final class DSCKeys {
367409
public static final String TRACE_ID = "sentry-trace_id";
368410
public static final String PUBLIC_KEY = "sentry-public_key";

sentry/src/test/java/io/sentry/BaggageTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,18 @@ class BaggageTest {
512512
assertEquals("%22%28%29%2C%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%7B%7D=value", baggage.toHeaderString(null))
513513
}
514514

515+
@Test
516+
fun `can skip logger for header from single string`() {
517+
val baggage = Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction")
518+
assertEquals("sentry-trace_id=a,sentry-transaction=sentryTransaction", baggage.toHeaderString(null))
519+
}
520+
521+
@Test
522+
fun `can skip logger for header from list of strings`() {
523+
val baggage = Baggage.fromHeader(listOf("sentry-trace_id=a", "sentry-transaction=sentryTransaction"))
524+
assertEquals("sentry-trace_id=a,sentry-transaction=sentryTransaction", baggage.toHeaderString(null))
525+
}
526+
515527
/**
516528
* token = 1*tchar
517529
* tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"

0 commit comments

Comments
 (0)