Skip to content

Commit 5b0d221

Browse files
authored
Merge pull request #6164 from getsentry/fix/callback-error-handling-profiles-sampler
fix(core): [Callback Errors 6] Handle profilesSampler failures
2 parents b692e97 + 97cd7e2 commit 5b0d221

3 files changed

Lines changed: 94 additions & 10 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
- Drop telemetry and record `callback_error` when a customer event processor throws instead of continuing with a potentially partially processed item. SDK-owned processor failures are logged and processing continues without a `callback_error` client report.
6666
- Drop breadcrumbs when `beforeBreadcrumb` throws instead of storing exception details on the breadcrumb.
6767
- When `tracesSampler` throws, drop the transaction and record `callback_error` instead of inheriting the parent sampling decision or falling back to `tracesSampleRate` ([#6163](https://github.com/getsentry/sentry-java/pull/6163))
68+
- When `profilesSampler` throws, disable profiling instead of falling back to `profilesSampleRate` or inheriting the parent's profiling decision. Trace sampling is unchanged ([#6164](https://github.com/getsentry/sentry-java/pull/6164))
6869
- Disable URL caching when reading `META-INF/MANIFEST.MF` files during version detection so that the SDK no longer keeps jar file handles open for the life of the process ([#6124](https://github.com/getsentry/sentry-java/pull/6124)
6970
- Keep the `EventListener` wrapped by `SentryOkHttpEventListener` per `Call` ([#6003](https://github.com/getsentry/sentry-java/pull/6003))
7071

‎sentry/src/main/java/io/sentry/TracesSampler.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ public TracesSamplingDecision sample(final @NotNull SamplingContext samplingCont
2626
}
2727

2828
Double profilesSampleRate = null;
29+
boolean profilesSamplerFailed = false;
2930
if (options.getProfilesSampler() != null) {
3031
try {
3132
profilesSampleRate = options.getProfilesSampler().sample(samplingContext);
3233
} catch (Throwable t) {
34+
profilesSamplerFailed = true;
3335
options
3436
.getLogger()
3537
.log(SentryLevel.ERROR, "Error in the 'ProfilesSamplerCallback' callback.", t);
3638
}
3739
}
38-
if (profilesSampleRate == null) {
40+
if (profilesSampleRate == null && !profilesSamplerFailed) {
3941
profilesSampleRate = options.getProfilesSampleRate();
4042
}
4143
Boolean profilesSampled = profilesSampleRate != null && sample(profilesSampleRate, sampleRand);
@@ -69,6 +71,13 @@ public TracesSamplingDecision sample(final @NotNull SamplingContext samplingCont
6971
final TracesSamplingDecision parentSamplingDecision =
7072
samplingContext.getTransactionContext().getParentSamplingDecision();
7173
if (parentSamplingDecision != null) {
74+
if (profilesSamplerFailed) {
75+
return SampleRateUtils.backfilledSampleRand(
76+
new TracesSamplingDecision(
77+
parentSamplingDecision.getSampled(),
78+
parentSamplingDecision.getSampleRate(),
79+
parentSamplingDecision.getSampleRand()));
80+
}
7281
return SampleRateUtils.backfilledSampleRand(parentSamplingDecision);
7382
}
7483

‎sentry/src/test/java/io/sentry/TracesSamplerTest.kt‎

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class TracesSamplerTest {
194194

195195
@Test
196196
fun `when profilesSampler returns null and parentSampled is set sampler uses it as a sampling decision`() {
197-
val sampler = fixture.getSut(tracesSampleRate = 1.0, profilesSamplerCallback = null)
197+
val sampler = fixture.getSut(tracesSampleRate = 1.0, profilesSamplerCallback = { null })
198198
val transactionContextParentSampled = TransactionContext("name", "op")
199199
transactionContextParentSampled.setParentSampled(true, true)
200200
val samplingDecision =
@@ -225,7 +225,7 @@ class TracesSamplerTest {
225225
fixture.getSut(
226226
tracesSampleRate = 1.0,
227227
profilesSampleRate = 0.2,
228-
profilesSamplerCallback = null,
228+
profilesSamplerCallback = { null },
229229
)
230230
val samplingDecision =
231231
sampler.sample(
@@ -358,18 +358,92 @@ class TracesSamplerTest {
358358
}
359359

360360
@Test
361-
fun `when a profilingRate and a ProfilesSamplerCallback is set but the callback throws an exception then profiling should still be enabled`() {
362-
val exception = Exception("faulty ProfilesSamplerCallback")
361+
fun `when profilesSampler throws then static profile rates are ignored`() {
362+
for (profilesSampleRate in listOf(null, 0.0, 1.0)) {
363+
val sampler =
364+
fixture.getSut(
365+
tracesSampleRate = 1.0,
366+
profilesSampleRate = profilesSampleRate,
367+
profilesSamplerCallback = {
368+
throw IllegalStateException("faulty ProfilesSamplerCallback")
369+
},
370+
)
371+
val decision =
372+
sampler.sample(SamplingContext(TransactionContext("name", "op"), null, 0.0, null))
373+
374+
assertThat(decision.sampled).isTrue()
375+
assertThat(decision.sampleRate).isEqualTo(1.0)
376+
assertThat(decision.sampleRand).isEqualTo(0.0)
377+
assertThat(decision.profileSampled).isFalse()
378+
assertThat(decision.profileSampleRate).isNull()
379+
}
380+
}
381+
382+
@Test
383+
fun `when profilesSampler throws then tracesSampler still determines trace sampling`() {
363384
val sampler =
364385
fixture.getSut(
365-
tracesSampleRate = 1.0,
386+
tracesSampleRate = 0.0,
366387
profilesSampleRate = 1.0,
367-
profilesSamplerCallback = { throw exception },
388+
tracesSamplerCallback = { 0.5 },
389+
profilesSamplerCallback = { throw IllegalStateException("faulty ProfilesSamplerCallback") },
368390
)
369391
val decision =
370-
sampler.sample(SamplingContext(TransactionContext("name", "op"), null, 0.0, null))
371-
assertTrue(decision.profileSampled)
372-
assertEquals(0.0, decision.sampleRand)
392+
sampler.sample(SamplingContext(TransactionContext("name", "op"), null, 0.1, null))
393+
394+
assertThat(decision.sampled).isTrue()
395+
assertThat(decision.sampleRate).isEqualTo(0.5)
396+
assertThat(decision.sampleRand).isEqualTo(0.1)
397+
assertThat(decision.profileSampled).isFalse()
398+
assertThat(decision.profileSampleRate).isNull()
399+
}
400+
401+
@Test
402+
fun `when profilesSampler throws then parent trace sampling is preserved without profiling`() {
403+
val sampler =
404+
fixture.getSut(
405+
tracesSampleRate = 1.0,
406+
profilesSampleRate = 1.0,
407+
profilesSamplerCallback = { throw IllegalStateException("faulty ProfilesSamplerCallback") },
408+
)
409+
for (sampled in listOf(true, false)) {
410+
val sampleRand = if (sampled) 0.1 else 0.9
411+
val parentDecision = TracesSamplingDecision(sampled, 0.5, sampleRand, true, 1.0)
412+
val transactionContext =
413+
TransactionContext(SentryId(), SpanId(), SpanId(), parentDecision, null)
414+
415+
val decision = sampler.sample(SamplingContext(transactionContext, null, sampleRand, null))
416+
417+
assertThat(decision.sampled).isEqualTo(sampled)
418+
assertThat(decision.sampleRate).isEqualTo(0.5)
419+
assertThat(decision.sampleRand).isEqualTo(sampleRand)
420+
assertThat(decision.profileSampled).isFalse()
421+
assertThat(decision.profileSampleRate).isNull()
422+
assertThat(parentDecision.profileSampled).isEqualTo(sampled)
423+
assertThat(parentDecision.profileSampleRate).isEqualTo(1.0)
424+
}
425+
}
426+
427+
@Test
428+
fun `when both samplers throw then tracing and profiling are disabled despite a sampled parent`() {
429+
val sampler =
430+
fixture.getSut(
431+
tracesSampleRate = 0.0,
432+
profilesSampleRate = 1.0,
433+
tracesSamplerCallback = { throw IllegalStateException("faulty TracesSamplerCallback") },
434+
profilesSamplerCallback = { throw IllegalStateException("faulty ProfilesSamplerCallback") },
435+
)
436+
val parentDecision = TracesSamplingDecision(true, 0.5, true, 1.0)
437+
val transactionContext =
438+
TransactionContext(SentryId(), SpanId(), SpanId(), parentDecision, null)
439+
440+
val decision = sampler.sample(SamplingContext(transactionContext, null, 0.9, null))
441+
442+
assertThat(decision.sampled).isFalse()
443+
assertThat(decision.sampleRate).isNull()
444+
assertThat(decision.sampleRand).isEqualTo(0.9)
445+
assertThat(decision.profileSampled).isFalse()
446+
assertThat(decision.profileSampleRate).isNull()
373447
}
374448

375449
@Test

0 commit comments

Comments
 (0)