sentry
| Package | Kind | Provides the role | Needs | Choose with |
|---|---|---|---|---|
smf_sentry | infrastructure | Crash reporting | values in .env, such as dart_define | -m sentry |
sentry reports to Sentry, with the sentry_flutter package, the errors that the app does not handle, those that its code reports through its CrashReporter, and the native crashes of Android and iOS.
An app can have several modules of crash reporting, and its reporter forwards each report to all of them, so sentry works on its own or next to firebase_crashlytics. The code of the app reports through CrashReporter and never through Sentry; see Crash reporting and analytics.
The DSN of the Sentry project goes into .env, so the module brings dart_define while that module is the only provider of the values in .env.
What it adds to the app
| In the app | What it is |
|---|---|
sentry_flutter: ^10.4.0 in the dependencies | The SDK of Sentry. In lib/, only the file below imports it. |
lib/core/crash_reporting/sentry_crash_reporter.dart | initSentryCrashReporter(), which starts Sentry, SentryCrashReporter, the reporter of the module, and configureSentryForTests, for the tests of the app |
The key SENTRY_DSN in .env | The DSN of your Sentry project, through the role of the values in .env |
| The section "Sentry" of the README | What reaches Sentry, the DSN, the debug builds, readable stack traces and what to tell the privacy policy |
Whichever module provides it, the crash reporting role adds lib/core/crash_reporting/crash_reporter.dart with CrashReporter, createCrashReporter() and installCrashReporting(); see firebase_crashlytics. Sentry starts asynchronously, so the file also has initCrashReporting(), which bootstrap() awaits before it installs the handlers. This is bootstrap() of an app with this module and dart_define, which comes with it:
Future<void> bootstrap() async {
checkEnvValues(debugPrint);
await initCrashReporting();
installCrashReporting();
}
The module needs nothing in the native projects.
In the guide for coding agents of the app, the crash reporting role describes the reporter under "Crash reporting". The module adds where Sentry starts, to report through CrashReporter and not through Sentry.captureException, never to call SentryFlutter.init elsewhere, that the environment of an event is the flavor of the build, and how a test starts Sentry.
The handlers of the errors
The handlers of the errors that nothing catches belong to the crash reporting role, which reports each error to every crash reporter of the app. Sentry would install handlers of its own, and each error of Flutter would then reach Sentry twice. So initSentryCrashReporter():
- removes
OnErrorIntegration, with which Sentry would takePlatformDispatcher.instance.onError. It finds the integration by its type, which a build with--obfuscatekeeps, unlike its name; - puts back the
FlutterError.onErrorthat it found, which the integration of Sentry for the errors of Flutter replaces.
So each error reaches Sentry once, also next to another crash reporter. The native crashes stay with the SDKs of Sentry for Android and iOS. Next to another crash reporter, both record the native crashes that each of them catches: an uncaught exception of Java or Kotlin reaches both, and a signal of native code on Android may reach Sentry alone. On iOS, another crash reporter may warn in the log that another handler of exceptions is installed, which does no harm.
The reporter prints nothing. Flutter still presents the errors that it catches, and in debug mode the engine prints the others.
What reaches Sentry
Each start of the app sends Sentry:
- the errors that the crash reporting of the app reports: those that nothing catches, as fatal, and those that the code reports with
recordError(), each with the messages oflog()before it as breadcrumbs and with the id of the user thatsetUserId()set. An error that comes before Sentry has started inbootstrap()is not reported; - the native crashes of Android and iOS, which the SDKs of Sentry for the platforms record and send at the next start, and on Android an app that does not respond (ANR), on iOS an app that hangs;
- a session, from which Sentry counts the users and the starts without a crash.
The start sets sendDefaultPii to false, attaches no screenshot and no view hierarchy, and makes no breadcrumb of what the app prints, since a print may hold what nobody meant to send. An event carries what the app logged with log().
The environment of each event is the flavor of the build, such as dev or prod, so Sentry tells the events of a test build from those of the release. A debug build sends its events too. To keep debug builds out of Sentry, start initSentryCrashReporter() with this line:
if (kDebugMode) throw StateError('Sentry reports nothing from a debug build.');
The role then leaves Sentry out of a debug build, as it does without a DSN.
The DSN
initSentryCrashReporter() starts Sentry with the DSN of SENTRY_DSN in .env. A DSN only lets an app send events to its project, and it goes into the app, where anyone can read it, so it is no secret. While the key has no value in a build, or has a value that is no DSN of Sentry, the start throws a StateError that names the key and never its value. The role then leaves Sentry out: the app starts, reports to its other crash reporters if it has any, and a debug build prints why.
Keep a second project for development: its DSN goes into your local .env, and the DSN of the project of the release into the values that a build for a store gets, such as those of the environment prod of your CI. In the settings of each project, give the key of the DSN a rate limit, so that one app that fails in a loop does not use up the quota of the organization. The free plan of Sentry has a monthly quota of errors, which the pricing of Sentry tells.
A release build whose command did not give the app .env at all does not build; see A release build needs .env.
Readable stack traces
Without --obfuscate, the Dart frames of an event have their functions, files and lines, also in a release build. The other cases need files that a release build writes, uploaded with an auth token of Sentry that may upload debug files, in SENTRY_AUTH_TOKEN. The README of the app gives the three, and they stay outside the app until you add them:
-
The native frames of iOS need the dSYMs of the archive. After
flutter build ipa, upload them with sentry-cli:sentry-cli debug-files upload --org <org> --project <project> build/ios/archive/Runner.xcarchive/dSYMs -
The Java and Kotlin frames of the plugins in an Android release, which R8 shrinks, need its mapping. The Gradle plugin of Sentry uploads it. Add
id("io.sentry.android.gradle") version "6.27.0"to thepluginsofandroid/app/build.gradle.kts, and this block below them, without which the plugin fails every release build without a token, sends telemetry of its own and changes the bytecode of every plugin:sentry {
autoUploadProguardMapping.set(providers.environmentVariable("SENTRY_AUTH_TOKEN").isPresent)
uploadNativeSymbols.set(false)
telemetry.set(false)
autoInstallation { enabled.set(false) }
tracingInstrumentation { enabled.set(false) }
} -
The Dart frames of a build with
--obfuscateneed its symbols. Build with--obfuscate --split-debug-info=build/debug-info --extra-gen-snapshot-options=--save-obfuscation-map=build/app/obfuscation.map.json, add sentry_dart_plugin^3.5.0to thedev_dependencies, give thepubspec.yamlof the app this block, and rundart run sentry_dart_plugin:sentry:
org: <org>
project: <project>
dart_symbol_map_path: build/app/obfuscation.map.json
Privacy
The App Store and Google Play ask for a privacy policy in the listing of the app and inside the app. Sentry gets crash and diagnostic data, the id of the user once the code sets one, and the IP address of the device, which Sentry may store unless "Prevent Storing of IP Addresses" is on in the settings of the project. The SDK of Sentry for iOS declares what it collects in its privacy manifest.
Finish the setup
Right after smf create, SENTRY_DSN is empty, so the app reports nothing to Sentry. The README of the app lists the step by hand in its section "Finish the setup", and the run ends with the link to its guide:
| Step | What you do |
|---|---|
sentry.dsn | Create a Flutter project in Sentry and copy its DSN into .env. |
In tests
sentry_flutter starts asynchronously, and its start never completes in the fake time of a widget test. So a test starts Sentry, or runs bootstrap(), in a plain test() in real time. configureSentryForTests changes the options of Sentry before it starts: a test keeps the events in beforeSend and gives Sentry a transport that sends nothing. The guide for coding agents of the app says the same.
The module keeps a test for what only it does, on the real sentry_flutter: that the start leaves FlutterError.onError as it found it and runs no OnErrorIntegration, that no event asks for the IP address of the user, that each call of the reporter reaches Sentry with its level, its reason and the environment, that an error of Flutter and an error that nothing catches each reach Sentry once, and that without a DSN, or with a value that is no DSN, the start throws an error that names the key and the app starts without Sentry. On a device, SMF's CI checks that Sentry runs exactly when the build has a DSN. See Contributing.
Choosing it
smf create asks which modules provide the crash reporting of the app, and the answer may be any number of them, none included:
Crash reporting: which module provides it?
firebase_crashlytics — Firebase Crashlytics with firebase_crashlytics
sentry — Sentry with sentry_flutter
To choose it without the question:
smf create my_app -m sentry
The module requires the values of .env, and smf create adds the only module that provides them:
Adding flutter_core: the only provider of the app entry role, which every app needs.
Adding dart_define: the only provider of the env role, which sentry requires.