From 22840026f59a6e1f469a819367a1d49a7fa51b50 Mon Sep 17 00:00:00 2001 From: Gabriel Terwesten Date: Thu, 10 Feb 2022 22:36:58 +0100 Subject: [PATCH] feat(cbl_sentry): allow disabling operation breadcrumbs (#295) --- .../lib/src/couchbase_lite_integration.dart | 16 +++++++++-- .../lib/src/sentry_tracing_delegate.dart | 8 ++++-- .../test/couchbase_lite_integration_test.dart | 14 ++++++++++ .../test/sentry_tracing_delegate_test.dart | 28 +++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/packages/cbl_sentry/lib/src/couchbase_lite_integration.dart b/packages/cbl_sentry/lib/src/couchbase_lite_integration.dart index 590db815..5ac9c6fa 100644 --- a/packages/cbl_sentry/lib/src/couchbase_lite_integration.dart +++ b/packages/cbl_sentry/lib/src/couchbase_lite_integration.dart @@ -27,9 +27,12 @@ import 'zone_span.dart'; /// /// ## Breadcrumbs /// -/// The start of a [TracedOperation] that is the result of usage of the CBL Dart -/// API is recorded as a breadcrumb. Internal operations are not recorded as -/// breadcrumbs. +/// For [TracedOperation]s that signify a direct interaction with the CBL Dart +/// API a breadcrumb is recorded at their start. This means that internal +/// operations are not recorded as breadcrumbs. +/// +/// Recording of these types of breadcrumbs is enabled by default and can be +/// disabled by setting [operationBreadcrumbs] to `false`. /// /// ## Transaction spans /// @@ -44,9 +47,11 @@ import 'zone_span.dart'; /// [traceInternalOperations] option (defaults to `false`). /// ``` class CouchbaseLiteIntegration extends Integration { + /// Creates a Sentry [Integration] that integrates CBL Dart with Sentry. CouchbaseLiteIntegration({ this.tracingEnabled, this.traceInternalOperations = false, + this.operationBreadcrumbs = true, this.breadcrumbLogLevel = LogLevel.warning, }); @@ -61,6 +66,10 @@ class CouchbaseLiteIntegration extends Integration { /// Activating this option can be useful to debug issues with CBL Dart itself. final bool traceInternalOperations; + /// Whether to record breadcrumbs for direct interactions with the CBL Dart + /// API. + final bool operationBreadcrumbs; + /// The log level at which Couchbase Lite logs are added as Sentry /// breadcrumbs. final LogLevel breadcrumbLogLevel; @@ -83,6 +92,7 @@ class CouchbaseLiteIntegration extends Integration { sentryDsn: options.dsn, tracingEnabled: tracingEnabled ?? options.isTracingEnabled(), traceInternalOperations: traceInternalOperations, + operationBreadcrumbs: operationBreadcrumbs, onInitialize: () { if (breadcrumbLogLevel != LogLevel.none) { Database.log.custom = diff --git a/packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart b/packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart index 3ccb356b..82c7f89a 100644 --- a/packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart +++ b/packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart @@ -11,6 +11,7 @@ class SentryTracingDelegate extends TracingDelegate { required this.sentryDsn, this.tracingEnabled = true, this.traceInternalOperations = false, + this.operationBreadcrumbs = true, this.onInitialize, Hub? hub, }) : _hub = hub ?? HubAdapter(), @@ -20,6 +21,7 @@ class SentryTracingDelegate extends TracingDelegate { : sentryDsn = userDelegate.sentryDsn, tracingEnabled = userDelegate.tracingEnabled, traceInternalOperations = userDelegate.traceInternalOperations, + operationBreadcrumbs = false, onInitialize = null, _hub = userDelegate._hub, _isWorkerDelegate = true; @@ -30,12 +32,14 @@ class SentryTracingDelegate extends TracingDelegate { final bool traceInternalOperations; - final bool _isWorkerDelegate; + final bool operationBreadcrumbs; final void Function()? onInitialize; final Hub _hub; + final bool _isWorkerDelegate; + final _operationSpans = []; bool get _isInsideOperation { @@ -164,7 +168,7 @@ class SentryTracingDelegate extends TracingDelegate { // === Breadcrumbs =========================================================== bool _shouldAddBreadcrumbForOperation(TracedOperation operation) { - if (_isInsideOperation) { + if (!operationBreadcrumbs || _isInsideOperation) { 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 752b3f72..562e019a 100644 --- a/packages/cbl_sentry/test/couchbase_lite_integration_test.dart +++ b/packages/cbl_sentry/test/couchbase_lite_integration_test.dart @@ -97,6 +97,20 @@ void main() { expect(integration.tracingDelegate?.traceInternalOperations, true); }); + + test('defaults operationBreadcrumbs to false', () async { + final integration = await callTestIntegration(CouchbaseLiteIntegration()); + + expect(integration.tracingDelegate?.operationBreadcrumbs, true); + }); + + test('passes operationBreadcrumbs option to delegate', () async { + final integration = await callTestIntegration(CouchbaseLiteIntegration( + operationBreadcrumbs: false, + )); + + expect(integration.tracingDelegate?.operationBreadcrumbs, false); + }); }); group('logging', () { diff --git a/packages/cbl_sentry/test/sentry_tracing_delegate_test.dart b/packages/cbl_sentry/test/sentry_tracing_delegate_test.dart index aa0afa0d..a3301df4 100644 --- a/packages/cbl_sentry/test/sentry_tracing_delegate_test.dart +++ b/packages/cbl_sentry/test/sentry_tracing_delegate_test.dart @@ -62,6 +62,34 @@ void main() { expect(breadcrumb.data, {'withConflictHandler': true}); }); + test("doesn't add sync operation breadcrumbs if disabled", () { + final delegate = SentryTracingDelegate( + sentryDsn: '', + operationBreadcrumbs: false, + hub: hub, + ); + delegate.traceSyncOperation( + InitializeOp(), + () => expect(hub.breadcrumbs, isEmpty), + ); + + expect(hub.breadcrumbs, isEmpty); + }); + + test("doesn't add async operation breadcrumbs if disabled", () async { + final delegate = SentryTracingDelegate( + sentryDsn: '', + operationBreadcrumbs: false, + hub: hub, + ); + await delegate.traceAsyncOperation( + InitializeOp(), + () async => expect(hub.breadcrumbs, isEmpty), + ); + + expect(hub.breadcrumbs, isEmpty); + }); + test('adds query operations with breadcrumb type query', () { final delegate = SentryTracingDelegate(sentryDsn: '', hub: hub); delegate.traceSyncOperation(