Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add File IO instrumentation docs
  • Loading branch information
marandaneto committed Dec 5, 2022
commit 8c45da94168a425f54ef78d7963e2a7a650cfd32
91 changes: 91 additions & 0 deletions src/platforms/dart/configuration/integrations/file.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
---
title: File I/O
sidebar_order: 5
description: "Learn more about the Sentry file I/O integration for the Dart SDK."
---

<Note>

This feature is currently in Beta. Beta features are still in-progress and may have bugs. We recognize the irony.

</Note>

The `sentry_file` library provides [File](https://api.dart.dev/stable/2.18.5/dart-io/File-class.html) support for Sentry wrapping the `File` class with a `SentryFile` wrapper. It is able to run performance monitoring for File operations.

The source can be found [on GitHub](https://github.com/getsentry/sentry-dart/tree/main/file/).

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

<Note>

The File I/O integration is only available for the `dart:io:File` class, not for the `dart:html:File` class.

</Note>

## Install

To use the `SentryFile` wrapper, add the `sentry_file` dependency.

```yml {filename:pubspec.yaml}
dependencies:
sentry: ^{{ packages.version('sentry.dart', '6.18.0') }}
sentry_file: ^{{ packages.version('sentry.dart.file', '6.18.0') }}
```

## Verify

<Note>

Capturing transactions requires that you first <PlatformLink to="/performance/">set up performance monitoring</PlatformLink> if you haven't already.

</Note>

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

Future<void> main() async {
// or SentryFlutter.init
await Sentry.init(
(options) {
options.dsn = 'https://[email protected]/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](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 `MyFileExample`.