Monitoring Flutter apps with Sentry

Carlos Morales Pereira
Barkibu Development
3 min readMar 30, 2020

--

We’ll warn you if errorz appearz!

We’ve talked about the importance of monitoring our products before: both errors (to fix them even before our users notice them) and general events (to analyze our users behaviour).

For the first part, we have several options to choose from: Rollbar, New Relic, Crashlytics… Among all of them, we’ve decided to work with Sentry, because it integrates quite well with our stack: it’s easy to configure in Flutter and works well with VictorOps.

Using Sentry in Flutter takes only a few minutes:

First, we need to add the sentry package to our `pubspec.yaml` file.

dependencies:  
sentry: 3.0.1

Then, we create a `SentryClient` and add our DSN.

import ‘package:sentry/sentry.dart’;

final SentryClient sentry = SentryClient(
dsn: “https://SOME_RANDOM_CHARS@sentry.io/0000000",
);

With this, we can start reporting errors to Sentry. For example, we can capture exceptions in a try/catch block:

void main() async {
try {
throw FormatException();
} catch (error, stackTrace) {
await sentry.captureException(
exception: error,
stackTrace: stackTrace,
);
}
}

The exception now shows up in our panel. If the same exception happens several times, Sentry will group all instances, avoiding clutter.

To capture all unhandled exceptions, we can use a Zone:

var sentry = SentryClient(dsn: "https://...");
// Run the whole app in a zone to capture all uncaught errors.
runZoned(
() => runApp(MyApp()),
onError: (Object error, StackTrace stackTrace) {
try {
sentry.captureException(
exception: error,
stackTrace: stackTrace,
);
print('Error sent to sentry.io: $error');
} catch (e) {
print('Sending report to sentry.io failed: $e');
print('Original error: $error');
}
},
);

We could stop here, but we can attach more info to our issues.We have 2 options for this:

  • Add environmentAttributes to our SentryClient constructor.
  • Use capture() instead of captureException() , and pass a custom Event .

We’ve chosen to follow the second option, as it allows us to add fine-grained information more flexibly. The device_info package is an awesome tool for this.

/// More code ///

@override
Future<void> capture({@required dynamic event, SeverityLevel severityLevel, dynamic stackTrace}) async {
final clientEvent = await _mapEventToClient(event, severityLevel, stackTrace);

await _client.capture(event: clientEvent);
}

/// More code ///

Future<client.Event> _mapEventToClient(dynamic event, SeverityLevel severityLevel, stackTrace) async {
final packageInfo = await PackageInfo.fromPlatform();

return client.Event(
exception: event,
level: _mapSeverityLevelToClient(severityLevel),
stackTrace: stackTrace,
release: await _readPubspecYaml(),
extra: await _mapExtraDeviceInfo(),
contexts: client.Contexts(
app: client.App(
name: packageInfo.appName,
version: packageInfo.version,
build: packageInfo.buildNumber,
),
operatingSystem: client.OperatingSystem(
name: this._platform.operatingSystem,
version: this._platform.operatingSystemVersion,
),
device: await _mapDeviceInfo(),
),
);
}

Please take a look at one of the params: release. This is one of my favorite Sentry features.

Releases allows us to make Sentry aware of our git releases (and commits included in each of them). This way, when an error occurs, the system suggest the commit that might have possibly introduced the bug making it easier to solve.

One important thing about Sentry releases is that are not tied to git tags (thus, releases) automatically. You need to create them using Sentry CLI and they use the latest commit hash by default. If you want to create releases tied to the latest tag, you have to do it manually… Or you can use this great Github Workflow to do it for you!

Once we are gathering all our issues, we can do multiple things with them. Sentry has an event system that allows us to trigger actions based on our prefered rules: send an email, a Slack message, an alert to an incident response tool such as VictorOps… It’s up to us to decide the best way to manage them.

Henlo! I is your support agent! How can I help hooman?

We are hiring!

Monitoring your apps is a must have to provide support to your users. Sentry is a great tool for Flutter. Are you interested in app monitorization? Come work with us!

--

--