---
title: "Drift Database Instrumentation"
description: "Learn more about the Sentry Drift Database Instrumentation for the Flutter SDK."
url: https://docs.sentry.io/platforms/dart/guides/flutter/integrations/drift-instrumentation/
---

# Drift Database Instrumentation | Sentry for Flutter

References to "transactions" on this page apply to the default transaction mode. In stream mode, this integration creates service spans instead, and Sentry sends them as they finish. See [Streamed Spans](https://docs.sentry.io/platforms/dart/guides/flutter/tracing/streamed-spans.md) for more information.

Drift is a library for easily managing SQLite databases within Flutter applications. The [sentry\_drift](https://pub.dev/packages/sentry_drift) package provides `Drift` support for database performance instrumentation and allows you to track the performance of your queries.

## [Instrumentation Behaviour](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/drift-instrumentation.md#instrumentation-behaviour)

The created spans attach to the active span. In transaction mode, that's the transaction bound to the scope; in stream mode, it's the span started with `Sentry.startSpan()`. If no span is active, the Drift span won't be sent to Sentry.

## [Prerequisites](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/drift-instrumentation.md#prerequisites)

Before starting, ensure:

1. The Sentry Flutter SDK is initialized. Learn more [here](https://docs.sentry.io/platforms/dart/guides/flutter.md#configure).
2. Tracing is set up. Learn more [here](https://docs.sentry.io/platforms/dart/guides/flutter/tracing.md).

## [Install](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/drift-instrumentation.md#install)

Add the `sentry_drift` dependency to install the Drift database instrumentation.

```yml
dependencies:
sentry_flutter: ^9.25.0
sentry_drift: ^9.25.0
```

## [Setup](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/drift-instrumentation.md#setup)

Add the `SentryQueryInterceptor` to a `QueryExecutor` or `DatabaseConnection`. Read the [Drift documentation](https://drift.simonbinder.eu/examples/tracing/) for more information on how to use interceptors in Drift.

**'QueryExecutor'**

```dart
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);
```

**'DatabaseConnection'**

```dart
final interceptor = SentryQueryInterceptor(databaseName: 'my_database_name');
// AppDatabase is an example of the auto-generated database class by Drift.
final database = AppDatabase(inMemoryExecutor());
await database.runWithInterceptor(interceptor: interceptor, () async {
  // Only database operations in this block will reach the interceptor.
});
```

## [Verify](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/drift-instrumentation.md#verify)

### [1. Execute the Code](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/drift-instrumentation.md#1-execute-the-code)

**Transaction Mode (Default)**

```dart
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());
}
```

**Stream Mode**

```dart
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 {
  await Sentry.startSpan('driftTest', (span) async {
    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();
  }, parentSpan: null);
}
```

### [2. View the Transaction on Sentry.io](https://docs.sentry.io/platforms/dart/guides/flutter/integrations/drift-instrumentation.md#2-view-the-transaction-on-sentryio)

To view the recorded transaction, log into [sentry.io](https://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`.
