Skip to content

Sentry Support

Hannes Winkler edited this page May 4, 2024 · 4 revisions

NOTE: This is WIP.

Flutter-pi has native integration for the official flutter sentry plugin. This enables reporting of flutter-pi crashes (e.g. Segmentation faults), in addition to normal flutter exceptions. Using it is pretty straightforward:

Setup

  1. Make sure you've cloned flutter-pi with submodules. The third_party/sentry-native directory should be not empty for you.

    • To fetch the submodules after clone: git submodule update --init --recursive
    • To clone flutter-pi with submodules: git clone --recursive https://github.com/ardera/flutter-pi.git
  2. Install sentry specific deps:

    sudo apt install libcurl4-openssl-dev
    
  3. Build flutter-pi with sentry support:

    cmake ... -DBUILD_SENTRY_PLUGIN=On
  4. Install the normal sentry_flutter package:

    flutter pub add sentry_flutter

    The flutter code looks similar to the normal sentry_flutter code, except you need to use a custom platform checker to enable the native integration:

import 'dart:ffi' as ffi;

class FlutterpiCompatiblePlatformChecker extends PlatformChecker {
  FlutterpiCompatiblePlatformChecker({
    super.platform,
    ffi.Abi? platformAbi,
    super.isWeb,
  }) : this.platformAbi = platformAbi ?? ffi.Abi.current();

  late final ffi.Abi platformAbi;

  bool get isFlutterPi {
    // potentially use a more fine-grained check here
    return platformAbi == ffi.Abi.linuxArm64 || platformAbi == ffi.Abi.linuxArm;
  }

  @override
  bool get hasNativeIntegration {
    if (isFlutterPi) {
      return true;
    }

    return super.hasNativeIntegration;
  }
}

void main() async {
  await SentryFlutter.init(
    (options) {
      options.dsn = sentryDsn;
      // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
      // We recommend adjusting this value in production.
      options.tracesSampleRate = 1.0;
      // The sampling rate for profiling is relative to tracesSampleRate
      // Setting to 1.0 will profile 100% of sampled transactions:
      options.profilesSampleRate = 1.0;

      options.debug = kDebugMode;
    },
    appRunner: () => runApp(MyApp()),
    
    // Needed because sentry doesn't use the platform checker configured in the options
    // to check for native integration.
    // ignore: invalid_use_of_internal_member
    platformChecker: FlutterpiCompatiblePlatformChecker(),
  );
}

Done.

C/C++ Stack Traces

To get useful C/C++ stack traces in sentry, you will need to upload all relevant debug info files with sentry-cli. (sentry-cli debug-files upload). That includes:

  • flutter-pi, built with debug symbols, so either use -DCMAKE_BUILD_TYPE=RelWithDebInfo or -DCMAKE_BUILD_TYPE=Debug
  • flutter app, also built with debug symbols. If you use flutterpi_tool, you can just use flutterpi_tool build --debug-symbols and upload the whole app bundle.

Dart Stack Traces

Dart stack traces work if they are captured directly inside flutter. E.g. all exceptions you report with Sentry.captureException will have their proper stack traces in sentry.

However, if the application crashes completely, the dart stack won't be shown correctly on sentry, only the C/C++ stack. Somehow crashpad doesn't detect the app ELF file as a valid module, so sentry will show all the dart frames as coming from some unknown module. Though most complete crashes should come from native code anyway, so this might not be that big of a problem.

Clone this wiki locally