Drift Database Instrumentation
This feature is currently in Beta. Beta features are still in progress and may have bugs. We recognize the irony.
(New in version 7.13.1)
Drift is a library for easily managing SQLite databases within Flutter applications.
The sentry_drift package provides Drift
support for database performance instrumentation and allows you to track the performance of your queries.
Instrumentation Behaviour
The created spans will be attached to the transaction on the scope - if no transaction is on the scope the Drift span will not be sent to Sentry.
Prerequisites
Before starting, ensure:
- The Sentry Flutter SDK is initialized. Learn more here.
- Performance Monitoring is set up. Learn more here.
Install
Add the sentry_drift
dependency to install the Drift database instrumentation.
pubspec.yaml
Copied
dependencies:
sentry_flutter: ^7.14.0
sentry_drift: ^7.14.0
Configure
Inject SentryQueryExecutor
into a Drift
database instance:
Copied
import 'package:drift/native.dart';
import 'package:sentry_drift/sentry_drift.dart';
final executor = SentryQueryExecutor(
() => NativeDatabase.memory(), // You can also provide your own database opener
databaseName: 'my_db_name',
);
final database = AppDatabase(executor); // AppDatabase is an example of the auto-generated database class by Drift.
Verify
1. Execute the Code
Copied
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry_drift/sentry_drift.dart';
import 'your_database.dart';
Future<void> driftTest() async {
final tr = Sentry.startTransaction(
'driftTest',
'op',
bindToScope: true
);
final executor = SentryQueryExecutor(
() => NativeDatabase.memory(),
databaseName: 'your_db_name',
);
final db = AppDatabase(executor);
await db.into(db.todoItems).insert(
TodoItemsCompanion.insert(
title: 'This is a test title',
content: 'test',
),
);
final items = await db.select(db.todoItems).get();
await db.close();
await tr.finish(status: const SpanStatus.ok());
}
2. View the Transaction on Sentry.io
To view the recorded transaction, log into sentry.io and open your
projectRepresents your service in Sentry and allows you to scope events to a distinct application.
.
Clicking Performance will open a page with transactions, where you can select the just recorded transaction with the name driftTest
.Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
- Package:
- pub:sentry_flutter
- Version:
- 7.14.0
- Repository:
- https://github.com/getsentry/sentry-dart
- API Documentation:
- https://pub.dev/documentation/sentry_flutter/latest/