From 8b6f866698bcc02f82bcaefca8b464a5219a651d Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 19 Oct 2022 12:54:02 +0200 Subject: [PATCH 1/2] Use sampled from incoming sentry-trace header --- .../java/io/sentry/TransactionContext.java | 5 +- .../java/io/sentry/TransactionContextTest.kt | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/sentry/src/main/java/io/sentry/TransactionContext.java b/sentry/src/main/java/io/sentry/TransactionContext.java index 61bc7db4b3..e941621112 100644 --- a/sentry/src/main/java/io/sentry/TransactionContext.java +++ b/sentry/src/main/java/io/sentry/TransactionContext.java @@ -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); } } diff --git a/sentry/src/test/java/io/sentry/TransactionContextTest.kt b/sentry/src/test/java/io/sentry/TransactionContextTest.kt index 77fb5fe80e..7eef9ce881 100644 --- a/sentry/src/test/java/io/sentry/TransactionContextTest.kt +++ b/sentry/src/test/java/io/sentry/TransactionContextTest.kt @@ -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 @@ -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) + } } From 59c0418f131f7bc9fe35620cbc6ee334e785b553 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Wed, 19 Oct 2022 12:58:09 +0200 Subject: [PATCH 2/2] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bfbc666e41..95fdf4220f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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