From 70e82597f9d08d5a82f4417ddbd16bcd441e1991 Mon Sep 17 00:00:00 2001 From: Gabriel Terwesten Date: Thu, 10 Feb 2022 21:33:46 +0100 Subject: [PATCH] feat(cbl_sentry): skip tracing when not enabled in Sentry options (#294) --- .../lib/src/couchbase_lite_integration.dart | 11 ++++++ .../lib/src/sentry_tracing_delegate.dart | 10 +++++- .../test/couchbase_lite_integration_test.dart | 22 ++++++++++++ .../test/sentry_tracing_delegate_test.dart | 36 +++++++++++++++++++ 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/packages/cbl_sentry/lib/src/couchbase_lite_integration.dart b/packages/cbl_sentry/lib/src/couchbase_lite_integration.dart index 33dd745c..590db815 100644 --- a/packages/cbl_sentry/lib/src/couchbase_lite_integration.dart +++ b/packages/cbl_sentry/lib/src/couchbase_lite_integration.dart @@ -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. @@ -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) { diff --git a/packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart b/packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart index 9b62f329..3ccb356b 100644 --- a/packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart +++ b/packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart @@ -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, @@ -17,6 +18,7 @@ class SentryTracingDelegate extends TracingDelegate { SentryTracingDelegate._workerDelegate(SentryTracingDelegate userDelegate) : sentryDsn = userDelegate.sentryDsn, + tracingEnabled = userDelegate.tracingEnabled, traceInternalOperations = userDelegate.traceInternalOperations, onInitialize = null, _hub = userDelegate._hub, @@ -24,6 +26,8 @@ class SentryTracingDelegate extends TracingDelegate { final String? sentryDsn; + final bool tracingEnabled; + final bool traceInternalOperations; final bool _isWorkerDelegate; @@ -83,7 +87,7 @@ class SentryTracingDelegate extends TracingDelegate { @override Object? captureTracingContext() { - if (!traceInternalOperations || !_isInsideOperation) { + if (!tracingEnabled || !traceInternalOperations || !_isInsideOperation) { return null; } @@ -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; diff --git a/packages/cbl_sentry/test/couchbase_lite_integration_test.dart b/packages/cbl_sentry/test/couchbase_lite_integration_test.dart index 9fe78d6a..752b3f72 100644 --- a/packages/cbl_sentry/test/couchbase_lite_integration_test.dart +++ b/packages/cbl_sentry/test/couchbase_lite_integration_test.dart @@ -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()); diff --git a/packages/cbl_sentry/test/sentry_tracing_delegate_test.dart b/packages/cbl_sentry/test/sentry_tracing_delegate_test.dart index cb3742aa..aa0afa0d 100644 --- a/packages/cbl_sentry/test/sentry_tracing_delegate_test.dart +++ b/packages/cbl_sentry/test/sentry_tracing_delegate_test.dart @@ -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');