Hive Database Instrumentation for Flutter

Learn more about the Sentry Hive Database Instrumentation for the Flutter SDK.

(New in version 7.13.1)

Hive is a lightweight and fast key-value database for Flutter and Dart applications. The sentry_hive package provides Hive support for database instrumentation and allows you to track the performance of your queries.

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 Hive span won't be sent to Sentry.

Before starting, ensure:

  1. The Sentry Flutter SDK is initialized. Learn more here.
  2. Tracing is set up. Learn more here.

Add the sentry_hive dependency to install the Hive database instrumentation.

pubspec.yaml
Copied
dependencies:
sentry_flutter: ^10.0.0
sentry_hive: ^10.0.0
path_provider: ^2.0.0

Use SentryHive to initialize the instance:

Copied
import 'package:path_provider/path_provider.dart';
import 'package:sentry_hive/sentry_hive.dart';

final appDir = await getApplicationDocumentsDirectory();
SentryHive.init(appDir.path);

To use the Transaction Mode tab, set options.traceLifecycle = SentryTraceLifecycle.static during SDK initialization.

Copied
import 'package:sentry_hive/sentry_hive.dart';

Future<void> hiveTest() async {
  await Sentry.startSpan('hiveTest', (span) async {
    final appDir = await getApplicationDocumentsDirectory();
    SentryHive.init(appDir.path);

    final catsBox = await SentryHive.openBox<Map>('cats');
    await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4});
    await catsBox.put('loki', {'name': 'Loki', 'age': 2});
    await catsBox.clear();
    await catsBox.close();

    SentryHive.close();
  }, parentSpan: null);
}

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 hiveTest.

Database spans identify the database system with db.system.name (flutter_hive) and the database name with db.namespace, when available. Use these keys in queries and span-filtering callbacks.

Database breadcrumbs also use db.system.name and db.namespace.

Was this helpful?
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").