Skip to content

Commit

Permalink
Respect incoming parent sampled decision when continuing a trace (#2311)
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer authored Oct 20, 2022
1 parent f50fb16 commit 418a49e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Remove verbose FrameMetricsAggregator failure logging ([#2293](https://github.com/getsentry/sentry-java/pull/2293))
- Ignore broken regex for tracePropagationTarget ([#2288](https://github.com/getsentry/sentry-java/pull/2288))
- Fix `SentryFileWriter`/`SentryFileOutputStream` append overwrites file contents ([#2304](https://github.com/getsentry/sentry-java/pull/2304))
- Respect incoming parent sampled decision when continuing a trace ([#2311](https://github.com/getsentry/sentry-java/pull/2311))

### Features

Expand Down
5 changes: 3 additions & 2 deletions sentry/src/main/java/io/sentry/TransactionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ public final class TransactionContext extends SpanContext {
baggage.freeze();

Double sampleRate = baggage.getSampleRateDouble();
Boolean sampled = parentSampled != null ? parentSampled.booleanValue() : true;
if (sampleRate != null) {
samplingDecision = new TracesSamplingDecision(true, sampleRate);
samplingDecision = new TracesSamplingDecision(sampled, sampleRate);
} else {
samplingDecision = new TracesSamplingDecision(true);
samplingDecision = new TracesSamplingDecision(sampled);
}
}

Expand Down
46 changes: 46 additions & 0 deletions sentry/src/test/java/io/sentry/TransactionContextTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.sentry

import io.sentry.protocol.SentryId
import io.sentry.protocol.TransactionNameSource
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNull
import kotlin.test.assertTrue

Expand All @@ -26,4 +28,48 @@ class TransactionContextTest {
assertNull(context.profileSampled)
assertTrue(context.parentSampled!!)
}

@Test
fun `when context is created from trace header and baggage header, parent sampling decision of false is set from trace header`() {
val traceHeader = SentryTraceHeader(SentryId(), SpanId(), false)
val baggageHeader = Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction,sentry-sample_rate=0.3")
val context = TransactionContext.fromSentryTrace("name", TransactionNameSource.CUSTOM, "op", traceHeader, baggageHeader)
assertNull(context.sampled)
assertNull(context.profileSampled)
assertFalse(context.parentSampled!!)
assertEquals(0.3, context.parentSamplingDecision!!.sampleRate)
}

@Test
fun `when context is created from trace header and baggage header, parent sampling decision of false is set from trace header if no sample rate is available`() {
val traceHeader = SentryTraceHeader(SentryId(), SpanId(), false)
val baggageHeader = Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction")
val context = TransactionContext.fromSentryTrace("name", TransactionNameSource.CUSTOM, "op", traceHeader, baggageHeader)
assertNull(context.sampled)
assertNull(context.profileSampled)
assertFalse(context.parentSampled!!)
assertNull(context.parentSamplingDecision!!.sampleRate)
}

@Test
fun `when context is created from trace header and baggage header, parent sampling decision of true is set from trace header`() {
val traceHeader = SentryTraceHeader(SentryId(), SpanId(), true)
val baggageHeader = Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction,sentry-sample_rate=0.3")
val context = TransactionContext.fromSentryTrace("name", TransactionNameSource.CUSTOM, "op", traceHeader, baggageHeader)
assertNull(context.sampled)
assertNull(context.profileSampled)
assertTrue(context.parentSampled!!)
assertEquals(0.3, context.parentSamplingDecision!!.sampleRate)
}

@Test
fun `when context is created from trace header and baggage header, parent sampling decision of true is set from trace header if no sample rate is available`() {
val traceHeader = SentryTraceHeader(SentryId(), SpanId(), true)
val baggageHeader = Baggage.fromHeader("sentry-trace_id=a,sentry-transaction=sentryTransaction")
val context = TransactionContext.fromSentryTrace("name", TransactionNameSource.CUSTOM, "op", traceHeader, baggageHeader)
assertNull(context.sampled)
assertNull(context.profileSampled)
assertTrue(context.parentSampled!!)
assertNull(context.parentSamplingDecision!!.sampleRate)
}
}

0 comments on commit 418a49e

Please sign in to comment.