File I/O
Learn more about the Sentry file I/O integration for the Dart SDK.
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 for more information.
File I/O operations are fundamental for reading from and writing to files within an application. The sentry_file integration provides File support for Sentry thus providing insight into tracing of file operations.
- 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 File I/O span won't be sent to Sentry. - The File I/O integration is only available for the
dart:io:Fileclass, not for thedart:html:Fileclass. - The SDK sets the span
operationtofile.copy,file.write,file.delete,file.open,file.readorfile.rename, anddescriptiontofilename(for example,file.txt). - The span finishes once the operation has been executed. The span
statusis then set toSpanStatus.okif successful, orSpanStatus.internalErrorif there was an error. - When the operation throws an
Exception, Sentry's SDK associates it with the running span.
To use the SentryFile wrapper, add the sentry_file dependency.
pubspec.yamldependencies:
sentry: ^9.29.0
sentry_file: ^9.29.0
dependencies:
sentry: ^9.29.0
sentry_file: ^9.29.0
Call the Sentry extension method to wrap the file:
final sentryFile = file.sentryTrace();
final sentryFile = file.sentryTrace();
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();
}
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();
}
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;
// Enables stream mode
options.traceLifecycle = SentryTraceLifecycle.stream;
},
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 root span — file I/O spans attach to the active span
await Sentry.startSpan('MyFileExample', (span) async {
// 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();
}, parentSpan: null);
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.
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").