Drift Database Instrumentation
Learn more about the Sentry Drift Database Instrumentation for the Flutter SDK.
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.
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.
Before starting, ensure:
Add the sentry_drift
dependency to install the Drift database instrumentation.
pubspec.yaml
dependencies:
sentry_flutter: ^8.14.2
sentry_drift: ^8.14.2
Add the SentryQueryInterceptor
to a QueryExecutor
or DatabaseConnection
. Read the Drift documentation for more information on how to use interceptors in Drift.
final interceptor = SentryQueryInterceptor(databaseName: 'my_database_name');
final executor = inMemoryExecutor().interceptWith(interceptor);
// AppDatabase is an example of the auto-generated database class by Drift.
final database = AppDatabase(executor);
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 interceptor = SentryQueryInterceptor(databaseName: 'my_database_name');
final executor = inMemoryExecutor().interceptWith(interceptor);
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());
}
To view the recorded transaction, log into sentry.io and open your project. Clicking Performance will open a page with transactions, where you can select the just recorded transaction with the name driftTest
.
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").