Skip to content

Commit

Permalink
Breadcrumbs for database operations (#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
denrase authored Oct 31, 2023
1 parent 2261c15 commit 955541a
Show file tree
Hide file tree
Showing 14 changed files with 1,163 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/flutter_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ jobs:
run: |
case "${{ matrix.target }}" in
ios)
device=$(xcrun simctl create sentryPhone com.apple.CoreSimulator.SimDeviceType.iPhone-14 com.apple.CoreSimulator.SimRuntime.iOS-16-4)
device=$(xcrun simctl create sentryPhone com.apple.CoreSimulator.SimDeviceType.iPhone-14 com.apple.CoreSimulator.SimRuntime.iOS-17-0)
xcrun simctl boot ${device}
echo "platform=iOS Simulator,id=${device}" >> "$GITHUB_OUTPUT"
;;
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Features

- Breadcrumbs for database operations ([#1656](https://github.com/getsentry/sentry-dart/pull/1656))

## 7.12.0

### Enhancements
Expand Down
3 changes: 3 additions & 0 deletions flutter/example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ end
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
end
end
end
31 changes: 31 additions & 0 deletions sqflite/lib/src/sentry_batch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class SentryBatch implements Batch {
span?.origin = SentryTraceOrigins.autoDbSqfliteBatch;
setDatabaseAttributeData(span, _dbName);

var breadcrumb = Breadcrumb(
message: _buffer.toString().trim(),
data: {},
type: 'query',
);
setDatabaseAttributeOnBreadcrumb(breadcrumb, _dbName);

try {
final result = await _batch.apply(
noResult: noResult,
Expand All @@ -64,14 +71,23 @@ class SentryBatch implements Batch {

span?.status = SpanStatus.ok();

breadcrumb.data?['status'] = 'ok';

return result;
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();

breadcrumb.data?['status'] = 'internal_error';
breadcrumb = breadcrumb.copyWith(
level: SentryLevel.warning,
);

rethrow;
} finally {
await span?.finish();
// ignore: invalid_use_of_internal_member
await _hub.scope.addBreadcrumb(breadcrumb);
}
});
}
Expand All @@ -94,6 +110,13 @@ class SentryBatch implements Batch {
span?.origin = SentryTraceOrigins.autoDbSqfliteBatch;
setDatabaseAttributeData(span, _dbName);

var breadcrumb = Breadcrumb(
message: _buffer.toString().trim(),
data: {},
type: 'query',
);
setDatabaseAttributeOnBreadcrumb(breadcrumb, _dbName);

try {
final result = await _batch.commit(
exclusive: exclusive,
Expand All @@ -102,15 +125,23 @@ class SentryBatch implements Batch {
);

span?.status = SpanStatus.ok();
breadcrumb.data?['status'] = 'ok';

return result;
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();

breadcrumb.data?['status'] = 'internal_error';
breadcrumb = breadcrumb.copyWith(
level: SentryLevel.warning,
);

rethrow;
} finally {
await span?.finish();
// ignore: invalid_use_of_internal_member
await _hub.scope.addBreadcrumb(breadcrumb);
}
});
}
Expand Down
36 changes: 34 additions & 2 deletions sqflite/lib/src/sentry_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,39 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
Future<void> close() {
return Future<void>(() async {
final currentSpan = _hub.getSpan();
final description = 'Close DB: ${_database.path}';
final span = currentSpan?.startChild(
dbOp,
description: 'Close DB: ${_database.path}',
description: description,
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabase;

var breadcrumb = Breadcrumb(
message: description,
category: dbOp,
data: {},
type: 'query',
);

try {
await _database.close();

span?.status = SpanStatus.ok();
breadcrumb.data?['status'] = 'ok';
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();
breadcrumb.data?['status'] = 'internal_error';
breadcrumb = breadcrumb.copyWith(
level: SentryLevel.warning,
);

rethrow;
} finally {
await span?.finish();
// ignore: invalid_use_of_internal_member
await _hub.scope.addBreadcrumb(breadcrumb);
}
});
}
Expand Down Expand Up @@ -126,14 +141,23 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
}) {
return Future<T>(() async {
final currentSpan = _hub.getSpan();
final description = 'Transaction DB: ${_database.path}';
final span = currentSpan?.startChild(
_dbSqlOp,
description: 'Transaction DB: ${_database.path}',
description: description,
);
// ignore: invalid_use_of_internal_member
span?.origin = SentryTraceOrigins.autoDbSqfliteDatabase;
setDatabaseAttributeData(span, dbName);

var breadcrumb = Breadcrumb(
message: description,
category: _dbSqlOp,
data: {},
type: 'query',
);
setDatabaseAttributeOnBreadcrumb(breadcrumb, dbName);

Future<T> newAction(Transaction txn) async {
final executor = SentryDatabaseExecutor(
txn,
Expand All @@ -152,15 +176,23 @@ class SentryDatabase extends SentryDatabaseExecutor implements Database {
await _database.transaction(newAction, exclusive: exclusive);

span?.status = SpanStatus.ok();
breadcrumb.data?['status'] = 'ok';

return result;
} catch (exception) {
span?.throwable = exception;
span?.status = SpanStatus.internalError();
breadcrumb.data?['status'] = 'internal_error';
breadcrumb = breadcrumb.copyWith(
level: SentryLevel.warning,
);

rethrow;
} finally {
await span?.finish();

// ignore: invalid_use_of_internal_member
await _hub.scope.addBreadcrumb(breadcrumb);
}
});
}
Expand Down
Loading

0 comments on commit 955541a

Please sign in to comment.