Skip to content

Commit b40e31f

Browse files
authored
Fix: Handle traces sampler exception (#1040)
1 parent 11b09e9 commit b40e31f

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- Handle traces sampler exception ([#1040](https://github.com/getsentry/sentry-dart/pull/1040))
8+
59
### Features
610

711
- Added [Flutter renderer](https://docs.flutter.dev/development/platform-integration/web/renderers) information to events ([#1035](https://github.com/getsentry/sentry-dart/pull/1035))

dart/lib/src/sentry_traces_sampler.dart

+14-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ class SentryTracesSampler {
2323

2424
final tracesSampler = _options.tracesSampler;
2525
if (tracesSampler != null) {
26-
final result = tracesSampler(samplingContext);
27-
if (result != null) {
28-
return SentryTracesSamplingDecision(
29-
_sample(result),
30-
sampleRate: result,
26+
try {
27+
final result = tracesSampler(samplingContext);
28+
if (result != null) {
29+
return SentryTracesSamplingDecision(
30+
_sample(result),
31+
sampleRate: result,
32+
);
33+
}
34+
} catch (exception, stackTrace) {
35+
_options.logger(
36+
SentryLevel.error,
37+
'The tracesSampler callback threw an exception',
38+
exception: exception,
39+
stackTrace: stackTrace,
3140
);
3241
}
3342
}

dart/test/sentry_traces_sampler_test.dart

+38
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,55 @@ void main() {
7575

7676
expect(sut.sample(context).sampled, false);
7777
});
78+
79+
test('tracesSampler exception is handled', () {
80+
final sut = fixture.getSut(debug: true);
81+
82+
final exception = Exception("tracesSampler exception");
83+
double? sampler(SentrySamplingContext samplingContext) {
84+
throw exception;
85+
}
86+
87+
fixture.options.tracesSampler = sampler;
88+
89+
final trContext = SentryTransactionContext(
90+
'name',
91+
'op',
92+
);
93+
final context = SentrySamplingContext(trContext, {});
94+
sut.sample(context);
95+
96+
expect(fixture.loggedException, exception);
97+
expect(fixture.loggedLevel, SentryLevel.error);
98+
});
7899
}
79100

80101
class Fixture {
81102
final options = SentryOptions(dsn: fakeDsn);
82103

104+
SentryLevel? loggedLevel;
105+
Object? loggedException;
106+
83107
SentryTracesSampler getSut({
84108
double? tracesSampleRate = 1.0,
85109
TracesSamplerCallback? tracesSampler,
110+
bool debug = false,
86111
}) {
87112
options.tracesSampleRate = tracesSampleRate;
88113
options.tracesSampler = tracesSampler;
114+
options.debug = debug;
115+
options.logger = mockLogger;
89116
return SentryTracesSampler(options);
90117
}
118+
119+
void mockLogger(
120+
SentryLevel level,
121+
String message, {
122+
String? logger,
123+
Object? exception,
124+
StackTrace? stackTrace,
125+
}) {
126+
loggedLevel = level;
127+
loggedException = exception;
128+
}
91129
}

0 commit comments

Comments
 (0)