Skip to content
Back to archipelago.banuacoder.com

Monitoring Implementations

How to add Sentry to a Flutter app with monitoring_sdk ​

Free Reference for sentry_monitoring_impl: every variable, option and package it generates. Command: archipelago generate sentry_monitoring_impl

Generate the Sentry vendor, set your DSN by overriding SentryRegisterModule, enable tracing and sessions, and check that consent gates the data.

What you'll learn ​

  • Generating the Sentry monitoring implementation brick
  • Overriding SentryRegisterModule to supply your DSN and config
  • Enabling performance tracing and session tracking
  • Checking that consent gates what Sentry receives

Prerequisites ​

  • monitoring_sdk generated first (see Monitoring SDK)
  • A Sentry account with a Flutter project created

Step 1: Generate the Brick ​

bash
archipelago generate sentry_monitoring_impl

Skip this step if you chose sentry in monitoringVendors at project creation.

The prompts are:

  • appName: your app name, for example MyApp
  • isForMonorepo: leave this as true

The brick generates in infrastructure/sentry_monitoring_impl/.

Step 2: Retrieve Your DSN ​

In the Sentry dashboard, navigate to Project → Settings → Client Keys (DSN) and copy the DSN value. Store it as an environment variable such as SENTRY_DSN and read it through your Env class.

Step 3: Override SentryRegisterModule ​

The brick does not ship a real DSN. Create a module in your app DI that extends SentryRegisterModule and provides SentryConfig:

dart
// apps/<appName>/lib/di/app_sentry_module.dart
@module
abstract class AppSentryModule extends SentryRegisterModule {
  @override
  @lazySingleton
  SentryConfig get sentryConfig => SentryConfig(
    dsn: Env.sentryDsn,
    environment: Flavor.status.name,
    release: '${Env.appName}@${Env.version}',
    tracesSampleRate: 1.0,
    enableAutoPerformanceTracing: true,
    enableAutoSessionTracking: true,
    attachStacktrace: true,
  );
}

Available SentryConfig fields and their defaults:

FieldDefault
dsnrequired
environment'production'
releasenull
tracesSampleRate1.0
enableAutoPerformanceTracingtrue
enableAutoSessionTrackingtrue
attachStacktracetrue

Step 4: Check the Registration ​

You register nothing by hand. The post-generation hook adds the package to the host app pubspec.yaml. It registers SentryMonitoringImplPackageModule in lib/di/injector.dart.

The monitoring_impl fanout builds its vendor list when AppMonitoring is first resolved. Every vendor module must run before that. Install order does not decide the injector order. The hook rebuilds the entries from the package manifests in dependency order. Generate monitoring_sdk first when you install standalone, because the vendor imports monitoring_api.

Run the composition-root test to confirm the wiring:

bash
cd apps/<appName>
flutter test test/di/injector_test.dart

The vendor registers as an AppMonitoringVendor named MonitoringInjectorKey.sentry.

Step 5: Verify in Sentry Dashboard ​

Grant diagnostics consent. AppMonitoring sends nothing until ConsentPurpose.diagnostics is granted. If the user withdraws it, the vendor calls Sentry.close(), so nothing new is captured or sent. Sentry has no API to discard envelopes already cached on disk. A later grant initialises Sentry again. Then run the app and trigger a caught error:

dart
await getIt<AppMonitoring>().recordError(
  Exception('test'),
  StackTrace.current,
);

Check Sentry → Issues for the event. Performance transactions started via recordTransaction appear under Sentry → Performance.

Next Steps ​