Sentry for Dart
Learn how to set up Sentry in your Dart app, capture your first errors and traces, and view them in Sentry.
Use curl to download, read and follow https://skills.sentry.dev/instrument to set up the Sentry Dart SDK.Using Flutter?
This guide focuses on plain Dart. If you're building a Flutter app, use the dedicated Flutter SDK instead.
You need:
Add the Sentry Dart SDK to your pubspec.yaml:
pubspec.yamldependencies:
sentry: ^10.3.0
dependencies:
sentry: ^10.3.0
Choose the features you want to configure, and this guide will show you how:
Configuration should happen as early as possible in your application's lifecycle.
Import and initialize the SDK in your app's entry point:
import 'package:sentry/sentry.dart';
Future<void> main() async {
await Sentry.init((options) {
options.dsn = '___PUBLIC_DSN___';
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/dart/data-management/data-collected/
options.sendDefaultPii = true;
// ___PRODUCT_OPTION_START___ performance
// Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
// We recommend adjusting this value in production.
options.tracesSampleRate = 1.0;
// ___PRODUCT_OPTION_END___ performance
});
// you can also configure SENTRY_DSN, SENTRY_RELEASE, SENTRY_DIST, and
// SENTRY_ENVIRONMENT via Dart environment variable (--dart-define)
}
import 'package:sentry/sentry.dart';
Future<void> main() async {
await Sentry.init((options) {
options.dsn = '___PUBLIC_DSN___';
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/dart/data-management/data-collected/
options.sendDefaultPii = true;
// ___PRODUCT_OPTION_START___ performance
// Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
// We recommend adjusting this value in production.
options.tracesSampleRate = 1.0;
// ___PRODUCT_OPTION_END___ performance
});
// you can also configure SENTRY_DSN, SENTRY_RELEASE, SENTRY_DIST, and
// SENTRY_ENVIRONMENT via Dart environment variable (--dart-define)
}
Let's test your setup and confirm that data reaches your Sentry project.
To verify that Sentry captures errors and creates issues in your Sentry project, add this intentional error to your application:
import 'package:sentry/sentry.dart';
try {
throw StateError('Sentry Test Error');
} catch (exception, stackTrace) {
await Sentry.captureException(
exception,
stackTrace: stackTrace,
);
}
import 'package:sentry/sentry.dart';
try {
throw StateError('Sentry Test Error');
} catch (exception, stackTrace) {
await Sentry.captureException(
exception,
stackTrace: stackTrace,
);
}
To test your tracing configuration, create a custom transaction and span:
final transaction = Sentry.startTransaction('test-transaction', 'task');
final span = transaction.startChild('test-span');
await span.finish();
await transaction.finish();
final transaction = Sentry.startTransaction('test-transaction', 'task');
final span = transaction.startChild('test-span');
await span.finish();
await transaction.finish();
Logs are enabled by default. Use the Sentry logger to send structured logs from anywhere in your application:
Sentry.logger.info('User clicked checkout button');
Sentry.logger.info('Order completed', attributes: {
'order_id': SentryAttribute.string('12345'),
'total': SentryAttribute.double(99.99),
});
Sentry.logger.warn('Warning message');
Sentry.logger.error('Error occurred');
Sentry.logger.info('User clicked checkout button');
Sentry.logger.info('Order completed', attributes: {
'order_id': SentryAttribute.string('12345'),
'total': SentryAttribute.double(99.99),
});
Sentry.logger.warn('Warning message');
Sentry.logger.error('Error occurred');
Metrics are enabled by default. Send test metrics from your app to verify that metrics are arriving in Sentry:
Sentry.metrics.count('checkout.failed', 1);
Sentry.metrics.gauge('queue.depth', 42);
Sentry.metrics.distribution('cart.amount_usd', 187.5);
Sentry.metrics.count('checkout.failed', 1);
Sentry.metrics.gauge('queue.depth', 42);
Sentry.metrics.distribution('cart.amount_usd', 187.5);
Now, head over to your project on Sentry.io to view the collected data (it takes a couple of moments for the data to appear).
At this point, you should have integrated Sentry into your Dart application and should already be sending data to your Sentry project.
Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are:
- Explore practical guides on what to monitor, log, track, and investigate after setup
- Continue to customize your configuration
- Learn more about manually capturing errors or messages
- Learn about the features of Sentry's Dart SDK
- Add performance instrumentation to your app
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").