getsentry / sentry-dart

Sentry SDK for Dart and Flutter
https://sentry.io/for/flutter/
MIT License
743 stars 227 forks source link

Metrics not enabled in native SDKs when enabled in Flutter #2190

Closed albertmoravec closed 1 week ago

albertmoravec commented 1 month ago

Platform

Flutter Mobile

Obfuscation

Disabled

Debug Info

Disabled

Doctor

[✓] Flutter (Channel stable, 3.22.2, on Microsoft Windows [Version 10.0.19045.4651], locale cs-CZ) • Flutter version 3.22.2 on channel stable at C:\tools\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 761747bfc5 (7 weeks ago), 2024-06-05 22:15:13 +0200 • Engine revision edd8546116 • Dart version 3.4.3 • DevTools version 2.34.3

[✓] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at C:\Users\Albert\AppData\Local\Android\sdk ✗ cmdline-tools component is missing Run path/to/sdkmanager --install "cmdline-tools;latest" See https://developer.android.com/studio/command-line for more details. ✗ Android license status unknown. Run flutter doctor --android-licenses to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[✓] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[!] Visual Studio - develop Windows apps (Visual Studio Community 2019 16.11.32) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 version 16.11.34301.259 • Windows 10 SDK version 10.0.19041.0 ✗ Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components: MSVC v142 - VS 2019 C++ x64/x86 build tools

[✓] Android Studio (version 2024.1) • Android Studio at C:\Users\Albert\AppData\Local\Programs\Android Studio • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314)

[✓] VS Code, 64-bit edition (version 1.91.1) • VS Code at C:\Program Files\Microsoft VS Code • Flutter extension version 3.92.0

[✓] Connected device (4 available) • Mi 9T Pro (mobile) • 30f23296 • android-arm64 • Android 11 (API 30) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.4651] • Chrome (web) • chrome • web-javascript • Google Chrome 126.0.6478.182 • Edge (web) • edge • web-javascript • Microsoft Edge 126.0.2592.102

[✓] Network resources • All expected network resources are available.

! Doctor found issues in 2 categories.

Version

8.4.0

Steps to Reproduce

  1. Initialize Sentry from Flutter with the following code:
    void main() async {
    await SentryFlutter.init(
    (options) {
      options.dsn = '<MY DSN>';
      options.tracesSampleRate = 1.0;
      options.enableMetrics = true;
    },
    appRunner: () => runApp(const MyApp()),
    );
    }
  2. Try emitting metric from native code, e.g.:
    Sentry.metrics().increment("counter", 1.0, null, null)

Expected Result

Metrics should be enabled for native Android and iOS SDKs and the "counter" metric should be emitted.

Actual Result

Metrics are not emitted, as enableMetrics = true is (AFAIK) not passed to native code.

Are you willing to submit a PR?

Yes

buenaflor commented 1 month ago

The reason is because our metrics implementation is built in our dart layer so it doesn't communicate with our native sdks at all.

@stefanosiano I'm not sure what our stance is on this

marianhlavac commented 1 month ago

We'd like to use just the native layer, but keep the configuration & control at the Dart layer, so for us, effectively, allowing to set the option would be enough, but I understand that for others this may become very confusing.

buenaflor commented 1 month ago

Got it, so it's a matter of convenience for your usecase?

marianhlavac commented 1 month ago

Not only a matter of convenience, but also code de-duplication and keeping the configuration of Sentry at once place.

When we switch options.autoInitializeNativeSdk to false, suddenly we need to keep the native Sentry SDK versions in sync with the Dart's library Sentry SDK used (otherwise it tries to install both of them and results in various errors), and the configuration is now at two places instead of one (iOS+Android), which we need to either keep in sync or write some custom Dart initialization function, that will trigger the native initialization (means we'll be duplicating the Sentry options, etc.), and similar issues. Not the end of the world, but also noticeably worse experience, just because one single parameter is not being passed to native.

I was also hoping that metrics can be enabled later on, which would solve this problem – Sentry gets initialized by Dart, and metrics can be enabled separately by some native function, but that doesn't seem to be possible. Correct me if I'm wrong?

Lastly, what happens when we want to use metrics both from Dart layer & native is unclear at this moment. I understand that there's always an option to keep two Sentry SDK instances running (Dart + native), but that probably requires two Sentry projects (otherwise all events would be duplicated) and most importantly feels fundamentally wrong.

Thanks for your attention to this.

buenaflor commented 1 month ago

I was also hoping that metrics can be enabled later on, which would solve this problem – Sentry gets initialized by Dart, and metrics can be enabled separately by some native function, but that doesn't seem to be possible. Correct me if I'm wrong?

I am not aware of being able to enable/disable features after the init in our SDKs.

Lastly, what happens when we want to use metrics both from Dart layer & native is unclear at this moment. I understand that there's always an option to keep two Sentry SDK instances running (Dart + native), but that probably requires two Sentry projects

It will send the events to the same project regardless because both native and dart would use the same DSN (dart uses the underlying native sdk methods to send events which will send it to whatever the DSN is configured to be in the native sdk)

@stefanosiano any thoughts on this?

buenaflor commented 1 week ago

hey Sentry is going to deprecate the metrics API, read this: https://sentry.zendesk.com/hc/en-us/articles/26369339769883-Upcoming-API-Changes-to-Metrics

We will have to revisit this when the new API is released