Skip to content

Commit

Permalink
feat(cbl_sentry): skip tracing when not enabled in Sentry options (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Feb 10, 2022
1 parent cfd0fd8 commit 70e8259
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/cbl_sentry/lib/src/couchbase_lite_integration.dart
Expand Up @@ -37,15 +37,25 @@ import 'zone_span.dart';
/// span is available though [Sentry.getSpan] or [cblSentrySpan], when the
/// operation is executed.
///
/// Tracing of operations is by default enabled if Sentry has been configured
/// for tracing. This can be overridden by setting [tracingEnabled].
///
/// Whether or not internal operations are traced is controlled by the
/// [traceInternalOperations] option (defaults to `false`).
/// ```
class CouchbaseLiteIntegration extends Integration {
CouchbaseLiteIntegration({
this.tracingEnabled,
this.traceInternalOperations = false,
this.breadcrumbLogLevel = LogLevel.warning,
});
/// Whether tracing of Couchbase Lite operations is enabled.
///
/// If this property is not set, tracing is enabled if Sentry has been
/// configured for tracing.
final bool? tracingEnabled;
/// Whether to trace internal operations.
///
/// Activating this option can be useful to debug issues with CBL Dart itself.
Expand All @@ -71,6 +81,7 @@ class CouchbaseLiteIntegration extends Integration {
final tracingDelegate = _tracingDelegate = SentryTracingDelegate(
sentryDsn: options.dsn,
tracingEnabled: tracingEnabled ?? options.isTracingEnabled(),
traceInternalOperations: traceInternalOperations,
onInitialize: () {
if (breadcrumbLogLevel != LogLevel.none) {
Expand Down
10 changes: 9 additions & 1 deletion packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart
Expand Up @@ -9,6 +9,7 @@ import 'zone_span.dart';
class SentryTracingDelegate extends TracingDelegate {
SentryTracingDelegate({
required this.sentryDsn,
this.tracingEnabled = true,
this.traceInternalOperations = false,
this.onInitialize,
Hub? hub,
Expand All @@ -17,13 +18,16 @@ class SentryTracingDelegate extends TracingDelegate {

SentryTracingDelegate._workerDelegate(SentryTracingDelegate userDelegate)
: sentryDsn = userDelegate.sentryDsn,
tracingEnabled = userDelegate.tracingEnabled,
traceInternalOperations = userDelegate.traceInternalOperations,
onInitialize = null,
_hub = userDelegate._hub,
_isWorkerDelegate = true;

final String? sentryDsn;

final bool tracingEnabled;

final bool traceInternalOperations;

final bool _isWorkerDelegate;
Expand Down Expand Up @@ -83,7 +87,7 @@ class SentryTracingDelegate extends TracingDelegate {

@override
Object? captureTracingContext() {
if (!traceInternalOperations || !_isInsideOperation) {
if (!tracingEnabled || !traceInternalOperations || !_isInsideOperation) {
return null;
}

Expand Down Expand Up @@ -193,6 +197,10 @@ class SentryTracingDelegate extends TracingDelegate {
// === Performance tracing ===================================================

bool _shouldStartSpanForOperation(TracedOperation operation) {
if (!tracingEnabled) {
return false;
}

final isInternalOperation = _isInsideOperation;
if (isInternalOperation && !traceInternalOperations) {
return false;
Expand Down
22 changes: 22 additions & 0 deletions packages/cbl_sentry/test/couchbase_lite_integration_test.dart
Expand Up @@ -62,6 +62,28 @@ void main() {
expect(integration.tracingDelegate?.sentryDsn, 'a');
});

test('disables tracing if not enabled for Sentry', () async {
final integration = await callTestIntegration(CouchbaseLiteIntegration());

expect(integration.tracingDelegate?.tracingEnabled, false);
});

test('enables tracing if enabled for Sentry', () async {
options.tracesSampleRate = 1;
final integration = await callTestIntegration(CouchbaseLiteIntegration());

expect(integration.tracingDelegate?.tracingEnabled, true);
});

test('allows overriding enabling of tracing', () async {
options.tracesSampleRate = 1;
final integration = await callTestIntegration(CouchbaseLiteIntegration(
tracingEnabled: false,
));

expect(integration.tracingDelegate?.tracingEnabled, false);
});

test('defaults traceInternalOperations to false', () async {
final integration = await callTestIntegration(CouchbaseLiteIntegration());

Expand Down
36 changes: 36 additions & 0 deletions packages/cbl_sentry/test/sentry_tracing_delegate_test.dart
Expand Up @@ -165,6 +165,42 @@ void main() {
expect(child.finished, isTrue);
});

test('does not trace sync operations when tracing is disabled', () {
final delegate = SentryTracingDelegate(
sentryDsn: '',
tracingEnabled: false,
hub: hub,
);
final root = MockSpan('root');

runWithCblSentrySpan(root, () {
delegate.traceSyncOperation(
InitializeOp(),
() {},
);
});

expect(root.children, isEmpty);
});

test('does not trace async operations when tracing is disabled', () async {
final delegate = SentryTracingDelegate(
sentryDsn: '',
tracingEnabled: false,
hub: hub,
);
final root = MockSpan('root');

await runWithCblSentrySpan(root, () async {
await delegate.traceAsyncOperation(
InitializeOp(),
() async {},
);
});

expect(root.children, isEmpty);
});

test('does not trace sync internal operations', () {
final delegate = SentryTracingDelegate(sentryDsn: '', hub: hub);
final root = MockSpan('root');
Expand Down

0 comments on commit 70e8259

Please sign in to comment.