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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect incoming parent sampled decision when continuing a trace #2311

Merged
merged 3 commits into from
Oct 20, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Choose a reason for hiding this comment

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

Why parentSampled is null should be sampled ?

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