Skip to content

Commit

Permalink
feat(cbl_sentry): allow disabling operation breadcrumbs (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Feb 10, 2022
1 parent 70e8259 commit 2284002
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
16 changes: 13 additions & 3 deletions packages/cbl_sentry/lib/src/couchbase_lite_integration.dart
Expand Up @@ -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
///
Expand All @@ -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,
});
Expand All @@ -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;
Expand All @@ -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 =
Expand Down
8 changes: 6 additions & 2 deletions packages/cbl_sentry/lib/src/sentry_tracing_delegate.dart
Expand Up @@ -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(),
Expand All @@ -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;
Expand All @@ -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 = <ISentrySpan>[];

bool get _isInsideOperation {
Expand Down Expand Up @@ -164,7 +168,7 @@ class SentryTracingDelegate extends TracingDelegate {
// === Breadcrumbs ===========================================================

bool _shouldAddBreadcrumbForOperation(TracedOperation operation) {
if (_isInsideOperation) {
if (!operationBreadcrumbs || _isInsideOperation) {
return false;
}

Expand Down
14 changes: 14 additions & 0 deletions packages/cbl_sentry/test/couchbase_lite_integration_test.dart
Expand Up @@ -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', () {
Expand Down
28 changes: 28 additions & 0 deletions packages/cbl_sentry/test/sentry_tracing_delegate_test.dart
Expand Up @@ -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(
Expand Down

0 comments on commit 2284002

Please sign in to comment.