File I/O

The sentry_file library provides File support for Sentry wrapping the File class with a SentryFile wrapper. The SentryFile is able to run performance monitoring for File operations.

The source can be found on GitHub.

On this page, we get you up and running with Sentry's file I/O integration, so that it will automatically start a span out of the active transaction, bound to the scope for operations such as copy, create, delete, open, rename, read, and write.

Install

To use the SentryFile wrapper, add the sentry_file dependency.

pubspec.yaml
Copied
dependencies:
  sentry: ^6.19.0
  sentry_file: ^6.19.0

Verify

Copied
import 'package:sentry/sentry.dart';
import 'package:sentry_file/sentry_file.dart';
import 'dart:io';

Future<void> main() async {
  await Sentry.init(
    (options) {
      options.dsn = 'https://example@sentry.io/example';
      // To set a uniform sample rate
      options.tracesSampleRate = 1.0;
    },
    appRunner: runApp, // Init your App.
  );
}

Future<void> runApp() async {
  final file = File('my_file.txt');
  // Call the Sentry extension method to wrap up the File
  final sentryFile = file.sentryTrace();

  // Start a transaction if there's no active transaction
  final transaction = Sentry.startTransaction(
    'MyFileExample',
    'file',
    bindToScope: true,
  );

  // create the File
  await sentryFile.create();
  // Write some content
  await sentryFile.writeAsString('Hello World');
  // Read the content
  final text = await sentryFile.readAsString();

  print(text);

  // Delete the file
  await sentryFile.delete();

  // Finish the transaction
  await transaction.finish(status: SpanStatus.ok());

  await Sentry.close();
}

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

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").