-
-
Notifications
You must be signed in to change notification settings - Fork 478
fix(core): [Callback Errors 5] Handle tracesSampler failures #6163
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
Draft
adinauer
wants to merge
3
commits into
fix/callback-error-handling-breadcrumbs
Choose a base branch
from
fix/callback-error-handling-traces-sampler
base: fix/callback-error-handling-breadcrumbs
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+420
โ8
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SentrySamplerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| package io.sentry.opentelemetry | ||
|
|
||
| import com.google.common.truth.Truth.assertThat | ||
| import io.opentelemetry.api.OpenTelemetry | ||
| import io.opentelemetry.api.common.Attributes | ||
| import io.opentelemetry.api.trace.Span | ||
| import io.opentelemetry.api.trace.SpanKind | ||
| import io.opentelemetry.api.trace.TraceFlags | ||
| import io.opentelemetry.api.trace.TraceState | ||
| import io.opentelemetry.context.Context | ||
| import io.opentelemetry.sdk.trace.SdkTracerProvider | ||
| import io.opentelemetry.sdk.trace.samplers.Sampler | ||
| import io.opentelemetry.sdk.trace.samplers.SamplingDecision | ||
| import io.sentry.DataCategory | ||
| import io.sentry.IScopes | ||
| import io.sentry.SamplingContext | ||
| import io.sentry.SentryOptions | ||
| import io.sentry.SentryTraceHeader | ||
| import io.sentry.SpanId | ||
| import io.sentry.TransactionContext | ||
| import io.sentry.TransactionOptions | ||
| import io.sentry.clientreport.DiscardReason | ||
| import io.sentry.protocol.SentryId | ||
| import kotlin.test.AfterTest | ||
| import kotlin.test.Test | ||
| import org.mockito.AdditionalAnswers.delegatesTo | ||
| import org.mockito.kotlin.any | ||
| import org.mockito.kotlin.argumentCaptor | ||
| import org.mockito.kotlin.mock | ||
| import org.mockito.kotlin.times | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.verifyNoMoreInteractions | ||
| import org.mockito.kotlin.whenever | ||
|
|
||
| class SentrySamplerTest { | ||
| private val onDiscard = mock<SentryOptions.OnDiscardCallback>() | ||
| private val options = | ||
| SentryOptions().apply { | ||
| tracesSampleRate = 1.0 | ||
| profilesSampleRate = 1.0 | ||
| tracesSampler = SentryOptions.TracesSamplerCallback { throw IllegalStateException("sampler") } | ||
| this.onDiscard = this@SentrySamplerTest.onDiscard | ||
| } | ||
| private val scopes = mock<IScopes>().also { whenever(it.options).thenReturn(options) } | ||
| private val sampler = SentrySampler(scopes) | ||
|
|
||
| @AfterTest | ||
| fun tearDown() { | ||
| SentryWeakSpanStorage.getInstance().clear() | ||
| } | ||
|
|
||
| @Test | ||
| fun `throwing tracesSampler drops root and reports callback errors alongside sample rate losses`() { | ||
| for (parentSampled in listOf(null, false, true)) { | ||
| val traceId = SentryId() | ||
| val context = | ||
| if (parentSampled == null) Context.root() | ||
| else | ||
| Context.root() | ||
| .with( | ||
| SentryOtelKeys.SENTRY_TRACE_KEY, | ||
| SentryTraceHeader(traceId, SpanId(), parentSampled), | ||
| ) | ||
| val result = | ||
| sampler.shouldSample( | ||
| context, | ||
| traceId.toString(), | ||
| "root", | ||
| SpanKind.INTERNAL, | ||
| Attributes.empty(), | ||
| emptyList(), | ||
| ) as SentrySamplingResult | ||
|
|
||
| assertThat(result.decision).isEqualTo(SamplingDecision.RECORD_ONLY) | ||
| assertThat(result.sentryDecision.sampled).isFalse() | ||
| assertThat(result.sentryDecision.profileSampled).isFalse() | ||
| } | ||
|
|
||
| verify(onDiscard, times(3)).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1) | ||
| verify(onDiscard, times(3)).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1) | ||
| verify(onDiscard, times(3)).execute(DiscardReason.SAMPLE_RATE, DataCategory.Transaction, 1) | ||
| verify(onDiscard, times(3)).execute(DiscardReason.SAMPLE_RATE, DataCategory.Span, 1) | ||
| verifyNoMoreInteractions(onDiscard) | ||
| } | ||
|
|
||
| @Test | ||
| fun `children of failed sampling decisions retain sample rate accounting`() { | ||
| val rootResult = | ||
| sampler.shouldSample( | ||
| Context.root(), | ||
| SentryId().toString(), | ||
| "root", | ||
| SpanKind.INTERNAL, | ||
| Attributes.empty(), | ||
| emptyList(), | ||
| ) as SentrySamplingResult | ||
| val restored = OtelSamplingUtil.extractSamplingDecision(rootResult.attributes)!! | ||
| assertThat(restored.sampled).isFalse() | ||
| assertThat(restored.sampleRand).isEqualTo(rootResult.sentryDecision.sampleRand) | ||
|
|
||
| val parentContext = | ||
| io.opentelemetry.api.trace.SpanContext.create( | ||
| SentryId().toString(), | ||
| SpanId().toString(), | ||
| TraceFlags.getDefault(), | ||
| TraceState.getDefault(), | ||
| ) | ||
| val parent = | ||
| mock<IOtelSpanWrapper>().also { | ||
| whenever(it.samplingDecision).thenReturn(restored) | ||
| } | ||
| SentryWeakSpanStorage.getInstance().storeSentrySpan(parentContext, parent) | ||
| val childResult = | ||
| sampler.shouldSample( | ||
| Span.wrap(parentContext).storeInContext(Context.root()), | ||
| parentContext.traceId, | ||
| "child", | ||
| SpanKind.INTERNAL, | ||
| Attributes.empty(), | ||
| emptyList(), | ||
| ) as SentrySamplingResult | ||
|
|
||
| assertThat(childResult.decision).isEqualTo(SamplingDecision.RECORD_ONLY) | ||
| assertThat(childResult.sentryDecision.sampled).isFalse() | ||
| verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1) | ||
| verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1) | ||
| verify(onDiscard).execute(DiscardReason.SAMPLE_RATE, DataCategory.Transaction, 1) | ||
| verify(onDiscard, times(2)).execute(DiscardReason.SAMPLE_RATE, DataCategory.Span, 1) | ||
| verifyNoMoreInteractions(onDiscard) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Sentry API sampling failure reports before forwarding through span factory`() { | ||
| val context = TransactionContext("root", "op") | ||
| context.samplingDecision = | ||
| options.internalTracesSampler.sample(SamplingContext(context, null, 0.0, null)) | ||
| verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Transaction, 1) | ||
| verify(onDiscard).execute(DiscardReason.CALLBACK_ERROR, DataCategory.Span, 1) | ||
| verifyNoMoreInteractions(onDiscard) | ||
| val recordingSampler = mock<Sampler>(defaultAnswer = delegatesTo(sampler)) | ||
| SdkTracerProvider.builder().setSampler(recordingSampler).build().use { provider -> | ||
| val openTelemetry = | ||
| mock<OpenTelemetry>().also { | ||
| whenever(it.tracerProvider).thenReturn(provider) | ||
| } | ||
| OtelSpanFactory(openTelemetry).createTransaction(context, scopes, TransactionOptions(), null) | ||
| val attributes = argumentCaptor<Attributes>() | ||
| verify(recordingSampler).shouldSample(any(), any(), any(), any(), attributes.capture(), any()) | ||
| val restored = OtelSamplingUtil.extractSamplingDecision(attributes.firstValue)!! | ||
| assertThat(restored.sampled).isFalse() | ||
| assertThat(restored.profileSampled).isFalse() | ||
| } | ||
|
|
||
| verifyNoMoreInteractions(onDiscard) | ||
| } | ||
|
|
||
| @Test | ||
| fun `null tracesSampler result uses normal sample rate accounting`() { | ||
| options.tracesSampler = SentryOptions.TracesSamplerCallback { null } | ||
| options.tracesSampleRate = 0.0 | ||
| val result = | ||
| sampler.shouldSample( | ||
| Context.root(), | ||
| SentryId().toString(), | ||
| "root", | ||
| SpanKind.INTERNAL, | ||
| Attributes.empty(), | ||
| emptyList(), | ||
| ) as SentrySamplingResult | ||
|
|
||
| assertThat(result.sentryDecision.sampled).isFalse() | ||
| verify(onDiscard).execute(DiscardReason.SAMPLE_RATE, DataCategory.Transaction, 1) | ||
| verify(onDiscard).execute(DiscardReason.SAMPLE_RATE, DataCategory.Span, 1) | ||
| verifyNoMoreInteractions(onDiscard) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This causes double reporting for transactions and spans with reason
callback_errorand again withsample_rateorbackpressure. We discussed internally and are accepting this for now as the alternative would be more complexity.If we reconsider, we can add a
DiscardReasontoTracesSamplingDecision. It also needs to be transported into ourio.sentry.opentelemetry.SentrySampler.