Skip to content

Commit

Permalink
Add enableTracing option (#1395)
Browse files Browse the repository at this point in the history
Co-authored-by: Manoel Aranda Neto <marandaneto@gmail.com>
  • Loading branch information
denrase and marandaneto authored Apr 24, 2023
1 parent 0613b95 commit 82250c4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

### Features

- Add `enableTracing` option ([#1395](https://github.com/getsentry/sentry-dart/pull/1395))
- This change is backwards compatible. The default is `null` meaning existing behaviour remains unchanged (setting either `tracesSampleRate` or `tracesSampler` enables performance).
- If set to `true`, performance is enabled, even if no `tracesSampleRate` or `tracesSampler` have been configured.
- If set to `true`, sampler will use default sample rate of 1.0, if no `tracesSampleRate` is set.
- If set to `false` performance is disabled, regardless of `tracesSampleRate` and `tracesSampler` options.
- Sync `connectionTimeout` and `readTimeout` to Android ([#1397](https://github.com/getsentry/sentry-dart/pull/1397))
- Set User `name` and `geo` in native plugins ([#1393](https://github.com/getsentry/sentry-dart/pull/1393))
- Add processor count to device info ([#1402](https://github.com/getsentry/sentry-dart/pull/1402))
Expand Down
9 changes: 9 additions & 0 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ class SentryOptions {
_stackTraceExtractorsByType[extractor.exceptionType] = extractor;
}

/// Enables generation of transactions and propagation of trace data. If set
/// to null, tracing might be enabled if [tracesSampleRate] or [tracesSampler]
/// are set.
bool? enableTracing;

/// Changed SDK behaviour when set to true:
/// - Rethrow exceptions that occur in user provided closures
@internal
Expand Down Expand Up @@ -405,6 +410,10 @@ class SentryOptions {
/// Returns if tracing should be enabled. If tracing is disabled, starting transactions returns
/// [NoOpSentrySpan].
bool isTracingEnabled() {
final enable = enableTracing;
if (enable != null) {
return enable;
}
return tracesSampleRate != null || tracesSampler != null;
}

Expand Down
14 changes: 10 additions & 4 deletions dart/lib/src/sentry_traces_sampler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import '../sentry.dart';

@internal
class SentryTracesSampler {
static const _defaultSampleRate = 1.0;

final SentryOptions _options;
final Random _random;

Expand Down Expand Up @@ -50,11 +52,15 @@ class SentryTracesSampler {
return parentSamplingDecision;
}

final tracesSampleRate = _options.tracesSampleRate;
if (tracesSampleRate != null) {
double? optionsRate = _options.tracesSampleRate;
double? defaultRate =
_options.enableTracing == true ? _defaultSampleRate : null;
double? optionsOrDefaultRate = optionsRate ?? defaultRate;

if (optionsOrDefaultRate != null) {
return SentryTracesSamplingDecision(
_sample(tracesSampleRate),
sampleRate: tracesSampleRate,
_sample(optionsOrDefaultRate),
sampleRate: optionsOrDefaultRate,
);
}

Expand Down
18 changes: 18 additions & 0 deletions dart/test/sentry_options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,22 @@ void main() {

expect(options.idleTimeout?.inSeconds, Duration(seconds: 3).inSeconds);
});

test('when enableTracing is set to true tracing is considered enabled', () {
final options = SentryOptions.empty();
options.enableTracing = true;

expect(options.isTracingEnabled(), true);
});

test('when enableTracing is set to false tracing is considered disabled', () {
final options = SentryOptions.empty();
options.enableTracing = false;
options.tracesSampleRate = 1.0;
options.tracesSampler = (_) {
return 1.0;
};

expect(options.isTracingEnabled(), false);
});
}
56 changes: 55 additions & 1 deletion dart/test/sentry_traces_sampler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import 'package:test/test.dart';
import 'mocks.dart';

void main() {
final fixture = Fixture();
late Fixture fixture;

setUp(() {
fixture = Fixture();
});

test('transactionContext has sampled', () {
final sut = fixture.getSut();
Expand Down Expand Up @@ -96,6 +100,54 @@ void main() {
expect(fixture.loggedException, exception);
expect(fixture.loggedLevel, SentryLevel.error);
});

test('when no tracesSampleRate is set, do not sample with enableTracing null',
() {
final sampler = fixture.getSut(tracesSampleRate: null, enableTracing: null);
final trContext = SentryTransactionContext(
'name',
'op',
parentSamplingDecision: null,
);
final context = SentrySamplingContext(trContext, {});
final samplingDecision = sampler.sample(context);

expect(samplingDecision.sampleRate, isNull);
expect(samplingDecision.sampled, false);
});

test(
'when no tracesSampleRate is set, do not sample with enableTracing false',
() {
final sampler =
fixture.getSut(tracesSampleRate: null, enableTracing: false);
final trContext = SentryTransactionContext(
'name',
'op',
parentSamplingDecision: null,
);
final context = SentrySamplingContext(trContext, {});
final samplingDecision = sampler.sample(context);

expect(samplingDecision.sampleRate, isNull);
expect(samplingDecision.sampled, false);
});

test(
'when no tracesSampleRate is set, uses default rate with enableTracing true',
() {
final sampler = fixture.getSut(tracesSampleRate: null, enableTracing: true);
final trContext = SentryTransactionContext(
'name',
'op',
parentSamplingDecision: null,
);
final context = SentrySamplingContext(trContext, {});
final samplingDecision = sampler.sample(context);

expect(samplingDecision.sampled, true);
expect(1.0, samplingDecision.sampleRate);
});
}

class Fixture {
Expand All @@ -108,11 +160,13 @@ class Fixture {
double? tracesSampleRate = 1.0,
TracesSamplerCallback? tracesSampler,
bool debug = false,
bool? enableTracing,
}) {
options.tracesSampleRate = tracesSampleRate;
options.tracesSampler = tracesSampler;
options.debug = debug;
options.logger = mockLogger;
options.enableTracing = enableTracing;
return SentryTracesSampler(options);
}

Expand Down

0 comments on commit 82250c4

Please sign in to comment.