|
| 1 | +package io.sentry.opentelemetry |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.opentelemetry.api.OpenTelemetry |
| 5 | +import io.opentelemetry.api.common.Attributes |
| 6 | +import io.opentelemetry.api.trace.Span |
| 7 | +import io.opentelemetry.api.trace.SpanKind |
| 8 | +import io.opentelemetry.api.trace.TraceFlags |
| 9 | +import io.opentelemetry.api.trace.TraceState |
| 10 | +import io.opentelemetry.context.Context |
| 11 | +import io.opentelemetry.sdk.trace.SdkTracerProvider |
| 12 | +import io.opentelemetry.sdk.trace.samplers.Sampler |
| 13 | +import io.opentelemetry.sdk.trace.samplers.SamplingDecision |
| 14 | +import io.sentry.DataCategory |
| 15 | +import io.sentry.IScopes |
| 16 | +import io.sentry.SamplingContext |
| 17 | +import io.sentry.SentryOptions |
| 18 | +import io.sentry.SentryTraceHeader |
| 19 | +import io.sentry.SpanId |
| 20 | +import io.sentry.TransactionContext |
| 21 | +import io.sentry.TransactionOptions |
| 22 | +import io.sentry.clientreport.DiscardReason |
| 23 | +import io.sentry.protocol.SentryId |
| 24 | +import kotlin.test.AfterTest |
| 25 | +import kotlin.test.Test |
| 26 | +import org.mockito.AdditionalAnswers.delegatesTo |
| 27 | +import org.mockito.kotlin.any |
| 28 | +import org.mockito.kotlin.argumentCaptor |
| 29 | +import org.mockito.kotlin.mock |
| 30 | +import org.mockito.kotlin.times |
| 31 | +import org.mockito.kotlin.verify |
| 32 | +import org.mockito.kotlin.verifyNoMoreInteractions |
| 33 | +import org.mockito.kotlin.whenever |
| 34 | + |
| 35 | +class SentrySamplerTest { |
| 36 | + private val onDiscard = mock<SentryOptions.OnDiscardCallback>() |
| 37 | + private val options = |
| 38 | + SentryOptions().apply { |
| 39 | + tracesSampleRate = 1.0 |
| 40 | + profilesSampleRate = 1.0 |
| 41 | + tracesSampler = SentryOptions.TracesSamplerCallback { throw IllegalStateException("sampler") } |
| 42 | + this.onDiscard = this@SentrySamplerTest.onDiscard |
| 43 | + } |
| 44 | + private val scopes = mock<IScopes>().also { whenever(it.options).thenReturn(options) } |
| 45 | + private val sampler = SentrySampler(scopes) |
| 46 | + |
| 47 | + @AfterTest |
| 48 | + fun tearDown() { |
| 49 | + SentryWeakSpanStorage.getInstance().clear() |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `throwing tracesSampler drops root and reports callback errors alongside sample rate losses`() { |
| 54 | + for (parentSampled in listOf(null, false, true)) { |
| 55 | + val traceId = SentryId() |
| 56 | + val context = |
| 57 | + if (parentSampled == null) Context.root() |
| 58 | + else |
| 59 | + Context.root() |
| 60 | + .with( |
| 61 | + SentryOtelKeys.SENTRY_TRACE_KEY, |
| 62 | + SentryTraceHeader(traceId, SpanId(), parentSampled), |
| 63 | + ) |
| 64 | + val result = |
| 65 | + sampler.shouldSample( |
| 66 | + context, |
| 67 | + traceId.toString(), |
| 68 | + "root", |
| 69 | + SpanKind.INTERNAL, |
| 70 | + Attributes.empty(), |
| 71 | + emptyList(), |
| 72 | + ) as SentrySamplingResult |
| 73 | + |
| 74 | + assertThat(result.decision).isEqualTo(SamplingDecision.RECORD_ONLY) |
| 75 | + assertThat(result.sentryDecision.sampled).isFalse() |
| 76 | + assertThat(result.sentryDecision.profileSampled).isFalse() |
| 77 | + } |
| 78 | + |
| 79 | + verify(onDiscard, times(3)).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1) |
| 80 | + verify(onDiscard, times(3)).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1) |
| 81 | + verify(onDiscard, times(3)).execute(DiscardReason.SAMPLE_RATE, DataCategory.Transaction, 1) |
| 82 | + verify(onDiscard, times(3)).execute(DiscardReason.SAMPLE_RATE, DataCategory.Span, 1) |
| 83 | + verifyNoMoreInteractions(onDiscard) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `children of failed sampling decisions retain sample rate accounting`() { |
| 88 | + val rootResult = |
| 89 | + sampler.shouldSample( |
| 90 | + Context.root(), |
| 91 | + SentryId().toString(), |
| 92 | + "root", |
| 93 | + SpanKind.INTERNAL, |
| 94 | + Attributes.empty(), |
| 95 | + emptyList(), |
| 96 | + ) as SentrySamplingResult |
| 97 | + val restored = OtelSamplingUtil.extractSamplingDecision(rootResult.attributes)!! |
| 98 | + assertThat(restored.sampled).isFalse() |
| 99 | + assertThat(restored.sampleRand).isEqualTo(rootResult.sentryDecision.sampleRand) |
| 100 | + |
| 101 | + val parentContext = |
| 102 | + io.opentelemetry.api.trace.SpanContext.create( |
| 103 | + SentryId().toString(), |
| 104 | + SpanId().toString(), |
| 105 | + TraceFlags.getDefault(), |
| 106 | + TraceState.getDefault(), |
| 107 | + ) |
| 108 | + val parent = |
| 109 | + mock<IOtelSpanWrapper>().also { |
| 110 | + whenever(it.samplingDecision).thenReturn(restored) |
| 111 | + } |
| 112 | + SentryWeakSpanStorage.getInstance().storeSentrySpan(parentContext, parent) |
| 113 | + val childResult = |
| 114 | + sampler.shouldSample( |
| 115 | + Span.wrap(parentContext).storeInContext(Context.root()), |
| 116 | + parentContext.traceId, |
| 117 | + "child", |
| 118 | + SpanKind.INTERNAL, |
| 119 | + Attributes.empty(), |
| 120 | + emptyList(), |
| 121 | + ) as SentrySamplingResult |
| 122 | + |
| 123 | + assertThat(childResult.decision).isEqualTo(SamplingDecision.RECORD_ONLY) |
| 124 | + assertThat(childResult.sentryDecision.sampled).isFalse() |
| 125 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1) |
| 126 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1) |
| 127 | + verify(onDiscard).execute(DiscardReason.SAMPLE_RATE, DataCategory.Transaction, 1) |
| 128 | + verify(onDiscard, times(2)).execute(DiscardReason.SAMPLE_RATE, DataCategory.Span, 1) |
| 129 | + verifyNoMoreInteractions(onDiscard) |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun `Sentry API sampling failure reports before forwarding through span factory`() { |
| 134 | + val context = TransactionContext("root", "op") |
| 135 | + context.samplingDecision = |
| 136 | + options.internalTracesSampler.sample(SamplingContext(context, null, 0.0, null)) |
| 137 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1) |
| 138 | + verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1) |
| 139 | + verifyNoMoreInteractions(onDiscard) |
| 140 | + val recordingSampler = mock<Sampler>(defaultAnswer = delegatesTo(sampler)) |
| 141 | + SdkTracerProvider.builder().setSampler(recordingSampler).build().use { provider -> |
| 142 | + val openTelemetry = |
| 143 | + mock<OpenTelemetry>().also { |
| 144 | + whenever(it.tracerProvider).thenReturn(provider) |
| 145 | + } |
| 146 | + OtelSpanFactory(openTelemetry).createTransaction(context, scopes, TransactionOptions(), null) |
| 147 | + val attributes = argumentCaptor<Attributes>() |
| 148 | + verify(recordingSampler).shouldSample(any(), any(), any(), any(), attributes.capture(), any()) |
| 149 | + val restored = OtelSamplingUtil.extractSamplingDecision(attributes.firstValue)!! |
| 150 | + assertThat(restored.sampled).isFalse() |
| 151 | + assertThat(restored.profileSampled).isFalse() |
| 152 | + } |
| 153 | + |
| 154 | + verifyNoMoreInteractions(onDiscard) |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + fun `null tracesSampler result uses normal sample rate accounting`() { |
| 159 | + options.tracesSampler = SentryOptions.TracesSamplerCallback { null } |
| 160 | + options.tracesSampleRate = 0.0 |
| 161 | + val result = |
| 162 | + sampler.shouldSample( |
| 163 | + Context.root(), |
| 164 | + SentryId().toString(), |
| 165 | + "root", |
| 166 | + SpanKind.INTERNAL, |
| 167 | + Attributes.empty(), |
| 168 | + emptyList(), |
| 169 | + ) as SentrySamplingResult |
| 170 | + |
| 171 | + assertThat(result.sentryDecision.sampled).isFalse() |
| 172 | + verify(onDiscard).execute(DiscardReason.SAMPLE_RATE, DataCategory.Transaction, 1) |
| 173 | + verify(onDiscard).execute(DiscardReason.SAMPLE_RATE, DataCategory.Span, 1) |
| 174 | + verifyNoMoreInteractions(onDiscard) |
| 175 | + } |
| 176 | +} |
0 commit comments