Skip to content
Draft
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
16 changes: 9 additions & 7 deletions sentry/src/main/java/io/sentry/Baggage.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,23 @@ public Baggage(final @NotNull Baggage baggage) {

@ApiStatus.Internal
static @NotNull Baggage copyWithOverrides(
final @NotNull Baggage baggage,
final @Nullable Baggage baggage,
final @NotNull SentryId traceId,
final @Nullable Double sampleRand) {
final @NotNull Baggage source =
baggage == null ? new Baggage(NoOpLogger.getInstance()) : baggage;
final @NotNull ConcurrentHashMap<String, String> keyValues =
new ConcurrentHashMap<>(baggage.keyValues);
new ConcurrentHashMap<>(source.keyValues);
keyValues.put(DSCKeys.TRACE_ID, traceId.toString());

return new Baggage(
keyValues,
baggage.sampleRate,
source.sampleRate,
sampleRand,
baggage.thirdPartyHeader,
true,
false,
baggage.logger);
source.thirdPartyHeader,
source.mutable,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer unfreezing here since we can rely on Baggage still being mutable when creating a new transaction in head of trace scenario.

source.shouldFreeze,
source.logger);
}

@ApiStatus.Internal
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/TransactionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static TransactionContext fromPropagationContext(
final @NotNull TransactionContext transactionContext) {
final @NotNull Baggage baggage =
Baggage.copyWithOverrides(
propagationContext.getBaggage(),
transactionContext.getBaggage(),
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use the transactions own Baggage and copy over desired values from Scope instead of the other way around.

propagationContext.getTraceId(),
propagationContext.getSampleRand());

Expand Down
16 changes: 13 additions & 3 deletions sentry/src/test/java/io/sentry/BaggageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ class BaggageTest {
}

@Test
fun `copy with overrides creates mutable baggage`() {
fun `copy with overrides preserves frozen state`() {
val baggage =
Baggage.fromHeader(
"sentry-sample_rand=0.1,sentry-trace_id=75302ac48a024bde9a3b3734a82e36c8",
Expand All @@ -450,14 +450,24 @@ class BaggageTest {

val copy = Baggage.copyWithOverrides(baggage, SentryId("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), 0.2)

assertTrue(copy.isMutable)
assertFalse(copy.isShouldFreeze)
assertFalse(copy.isMutable)
assertTrue(copy.isShouldFreeze)
assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", copy.traceId)
assertEquals(0.2, copy.sampleRand!!, 0.0001)
assertEquals("75302ac48a024bde9a3b3734a82e36c8", baggage.traceId)
assertEquals(0.1, baggage.sampleRand!!, 0.0001)
}

@Test
fun `copy with overrides creates baggage when source is null`() {
val copy = Baggage.copyWithOverrides(null, SentryId("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), 0.2)

assertTrue(copy.isMutable)
assertFalse(copy.isShouldFreeze)
assertEquals("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", copy.traceId)
assertEquals(0.2, copy.sampleRand!!, 0.0001)
}

@Test
fun `if header contains sentry values baggage is marked as shouldFreeze`() {
val baggage =
Expand Down
3 changes: 3 additions & 0 deletions sentry/src/test/java/io/sentry/TransactionContextTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class TransactionContextTest {
fun `fromPropagationContextAsRoot copies non trace state`() {
val propagationBaggage = Baggage(NoOpLogger.getInstance())
propagationBaggage.sampleRand = 0.42
propagationBaggage.publicKey = "propagation-public-key"
val propagationContext =
PropagationContext(
SentryId("75302ac48a024bde9a3b3734a82e36c8"),
Expand All @@ -109,6 +110,7 @@ class TransactionContextTest {
)
val samplingDecision = TracesSamplingDecision(true, 0.3, true, 0.4)
val transactionContext = TransactionContext("name", "op", samplingDecision)
transactionContext.baggage!!.publicKey = "transaction-public-key"
transactionContext.transactionNameSource = TransactionNameSource.ROUTE
transactionContext.description = "description"
transactionContext.status = SpanStatus.OK
Expand Down Expand Up @@ -145,6 +147,7 @@ class TransactionContextTest {
assertEquals(0.4, context.samplingDecision!!.profileSampleRate)
assertEquals(0.42, context.baggage!!.sampleRand)
assertEquals(propagationContext.traceId.toString(), context.baggage!!.traceId)
assertEquals("transaction-public-key", context.baggage!!.publicKey)
assertNull(context.featureFlagBuffer.featureFlags)
}

Expand Down
Loading