firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.62k stars 3.95k forks source link

🐛 [firebase_performance] clarify situations where `await` is required for capturing metrics #12024

Closed Fernandomr88 closed 2 months ago

Fernandomr88 commented 9 months ago

So I have been using firebase_performance and there are a few firebase and google endpoint being monitored correctly at firebase console. But my own endpoints are not showing up there. I have been using Dio to make requests now and until last month using dart http package.

Also for android no requests are being logged and for iOS some are.

I have already implemented this:

    final HttpMetric metric = FirebasePerformance.instance.newHttpMetric(endpoint, _nameToEnum(verb));
    metric.start();
    await dio.get(endpoint);
    metric.stop();

but still nothing shows up.

this is "all network requests view" for iOS

image

and the custom network requests saying "colecting data", for weeks now it's being like this.

image

also for android there are no requests being logged

image

am I missing something?

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ```console Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.16.3, on macOS 14.2 23C64 darwin-arm64, locale pt-BR) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 15.1) [✓] Chrome - develop for the web [✓] Android Studio (version 2023.1) [✓] VS Code (version 1.85.0) [✓] Connected device (4 available) [✓] Network resources • No issues found! ```

Flutter dependencies

Click To Expand ```console Dart SDK 3.2.3 Flutter SDK 3.16.3 dependencies: - amplify_auth_cognito 1.6.1 [amplify_analytics_pinpoint amplify_analytics_pinpoint_dart amplify_auth_cognito_dart amplify_core amplify_flutter amplify_secure_storage async flutter meta path plugin_platform_interface] - amplify_flutter 1.6.1 [amplify_core amplify_secure_storage aws_common collection flutter meta] - android_id 0.3.6 [flutter] - async 2.11.0 [collection meta] - badges 3.1.2 [flutter] - camera 0.10.5+6 [camera_android camera_avfoundation camera_platform_interface camera_web flutter flutter_plugin_android_lifecycle] - cloud_functions 4.5.8 [cloud_functions_platform_interface cloud_functions_web firebase_core firebase_core_platform_interface flutter] - connectivity_plus 5.0.2 [flutter flutter_web_plugins connectivity_plus_platform_interface js meta nm] - cupertino_icons 1.0.6 - currency_text_input_formatter 2.1.11 [flutter intl] - datetime_picker_formfield_new 2.1.0 [flutter intl] - device_info_plus 9.1.1 [device_info_plus_platform_interface ffi file flutter flutter_web_plugins meta win32 win32_registry] - dio 5.4.0 [async http_parser meta path] - dropdown_plus 0.0.9 [flutter] - file_picker 6.1.1 [flutter flutter_web_plugins flutter_plugin_android_lifecycle plugin_platform_interface ffi path win32] - firebase_analytics 10.7.4 [firebase_analytics_platform_interface firebase_analytics_web firebase_core firebase_core_platform_interface flutter] - firebase_app_installations 0.2.4+8 [firebase_app_installations_platform_interface firebase_app_installations_web firebase_core firebase_core_platform_interface flutter] - firebase_core 2.24.2 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 3.4.8 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_messaging 14.7.8 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - firebase_performance 0.9.3+8 [firebase_core firebase_core_platform_interface firebase_performance_platform_interface firebase_performance_web flutter] - firebase_remote_config 4.3.8 [firebase_core firebase_core_platform_interface firebase_remote_config_platform_interface firebase_remote_config_web flutter] - fl_chart 0.65.0 [equatable flutter] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math web sky_engine] - flutter_animate 4.3.0 [flutter] - flutter_app_badger 1.5.0 [flutter] - flutter_credit_card 4.0.1 [flutter plugin_platform_interface flutter_web_plugins] - flutter_local_notifications 16.2.0 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math web] - flutter_mobx 2.2.0+1 [flutter mobx provider] - flutter_pdfview 1.3.2 [flutter] - flutter_secure_storage 9.0.0 [flutter flutter_secure_storage_linux flutter_secure_storage_macos flutter_secure_storage_platform_interface flutter_secure_storage_web flutter_secure_storage_windows meta] - flutter_staggered_grid_view 0.7.0 [flutter] - flutter_statusbarcolor_ns 0.5.0 [flutter] - fluttertoast 8.2.4 [flutter flutter_web_plugins] - grouped_list 5.1.2 [flutter] - http 1.1.2 [async http_parser meta web] - http_parser 4.0.2 [collection source_span string_scanner typed_data] - intl 0.18.1 [clock meta path] - launch_review 3.0.1 [flutter] - local_auth 2.1.7 [flutter local_auth_android local_auth_ios local_auth_platform_interface local_auth_windows] - mobx 2.2.3 [meta collection] - month_picker_dialog 2.4.0 [flutter flutter_localizations provider intl] - open_app_file 4.0.2 [flutter ffi] - package_info_plus 4.2.0 [ffi flutter flutter_web_plugins http meta path package_info_plus_platform_interface win32] - path 1.8.3 - path_provider 2.1.1 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows] - permission_handler 11.1.0 [flutter meta permission_handler_android permission_handler_apple permission_handler_html permission_handler_windows permission_handler_platform_interface] - qr_code_scanner 1.0.1 [js flutter flutter_web_plugins] - share_plus 7.2.1 [cross_file meta mime flutter flutter_web_plugins share_plus_platform_interface file url_launcher_web url_launcher_windows url_launcher_linux url_launcher_platform_interface ffi win32] - shared_preferences 2.2.2 [flutter shared_preferences_android shared_preferences_foundation shared_preferences_linux shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - url_launcher 6.2.2 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] dev dependencies: - flutter_launcher_icons 0.13.1 [args checked_yaml cli_util image json_annotation path yaml] - flutter_lints 3.0.1 [lints] transitive dependencies: - _flutterfire_internals 1.3.16 [collection firebase_core firebase_core_platform_interface flutter meta] - amplify_analytics_pinpoint 1.6.2 [amplify_analytics_pinpoint_dart amplify_core amplify_db_common amplify_secure_storage aws_common device_info_plus flutter meta package_info_plus path_provider] - amplify_analytics_pinpoint_dart 0.3.5 [amplify_core amplify_db_common_dart amplify_secure_storage_dart aws_common aws_signature_v4 built_collection built_value collection drift intl meta path smithy smithy_aws uuid] - amplify_auth_cognito_dart 0.10.8 [amplify_analytics_pinpoint_dart amplify_core amplify_secure_storage_dart async aws_common aws_signature_v4 built_collection built_value clock collection convert crypto ffi fixnum http intl js json_annotation meta oauth2 path smithy smithy_aws stream_transform uuid win32 win32_registry worker_bee] - amplify_core 1.6.2 [async aws_common aws_signature_v4 collection graphs intl json_annotation logging meta retry stack_trace uuid] - amplify_db_common 0.3.4 [amplify_db_common_dart drift flutter path path_provider] - amplify_db_common_dart 0.3.4 [amplify_core async aws_common drift meta path sqlite3] - amplify_secure_storage 0.4.0+6 [amplify_secure_storage_dart async file flutter meta path path_provider path_provider_windows] - amplify_secure_storage_dart 0.4.2 [async aws_common built_collection built_value ffi file js meta path win32 worker_bee] - archive 3.4.9 [crypto path pointycastle] - args 2.4.2 - aws_common 0.6.2 [async built_collection built_value collection http2 js json_annotation logging meta mime os_detect path stream_transform uuid] - aws_signature_v4 0.5.0 [async aws_common collection convert crypto json_annotation meta path] - boolean_selector 2.1.1 [source_span string_scanner] - built_collection 5.1.1 - built_value 8.7.0 [built_collection collection fixnum meta] - camera_android 0.10.8+14 [camera_platform_interface flutter flutter_plugin_android_lifecycle stream_transform] - camera_avfoundation 0.9.13+7 [camera_platform_interface flutter stream_transform] - camera_platform_interface 2.6.0 [cross_file flutter plugin_platform_interface stream_transform] - camera_web 0.3.2+3 [camera_platform_interface flutter flutter_web_plugins stream_transform] - characters 1.3.0 - checked_yaml 2.0.3 [json_annotation source_span yaml] - cli_util 0.4.0 [meta path] - clock 1.1.1 - cloud_functions_platform_interface 5.5.11 [firebase_core flutter meta plugin_platform_interface] - cloud_functions_web 4.6.11 [cloud_functions_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - collection 1.18.0 - connectivity_plus_platform_interface 1.2.4 [flutter meta plugin_platform_interface] - convert 3.1.1 [typed_data] - crclib 3.0.0 [meta tuple] - cross_file 0.3.3+8 [meta web] - crypto 3.0.3 [typed_data] - dbus 0.7.10 [args ffi meta xml] - device_info_plus_platform_interface 7.0.0 [flutter meta plugin_platform_interface] - drift 2.12.1 [async convert collection js meta stream_channel sqlite3 path] - equatable 2.0.5 [collection meta] - fake_async 1.3.1 [clock collection] - ffi 2.1.0 - file 7.0.0 [meta path] - firebase_analytics_platform_interface 3.8.4 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_analytics_web 0.5.5+11 [_flutterfire_internals firebase_analytics_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_app_installations_platform_interface 0.1.4+16 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_app_installations_web 0.1.3+16 [_flutterfire_internals firebase_app_installations_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 2.10.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.6.16 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_messaging_platform_interface 4.5.17 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 3.5.17 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - firebase_performance_platform_interface 0.1.4+16 [_flutterfire_internals firebase_core flutter plugin_platform_interface] - firebase_performance_web 0.1.4+16 [_flutterfire_internals firebase_core firebase_core_web firebase_performance_platform_interface flutter flutter_web_plugins js] - firebase_remote_config_platform_interface 1.4.16 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_remote_config_web 1.4.16 [firebase_core firebase_core_web firebase_remote_config_platform_interface flutter flutter_web_plugins js] - fixnum 1.1.0 - flutter_local_notifications_linux 4.0.0+1 [dbus ffi flutter flutter_local_notifications_platform_interface path xdg_directories] - flutter_local_notifications_platform_interface 7.0.0+1 [flutter plugin_platform_interface] - flutter_plugin_android_lifecycle 2.0.17 [flutter] - flutter_secure_storage_linux 1.2.0 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_macos 3.0.1 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_platform_interface 1.0.2 [flutter plugin_platform_interface] - flutter_secure_storage_web 1.1.2 [flutter flutter_secure_storage_platform_interface flutter_web_plugins js] - flutter_secure_storage_windows 3.0.0 [ffi flutter flutter_secure_storage_platform_interface path path_provider win32] - flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math async boolean_selector characters collection material_color_utilities meta source_span stream_channel string_scanner term_glyph web] - flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math web] - graphs 2.3.1 [collection] - http2 2.2.0 - image 4.1.3 [archive meta xml] - js 0.6.7 [meta] - json_annotation 4.8.1 [meta] - lints 3.0.0 - local_auth_android 1.0.35 [flutter flutter_plugin_android_lifecycle intl local_auth_platform_interface] - local_auth_ios 1.1.5 [flutter intl local_auth_platform_interface] - local_auth_platform_interface 1.0.8 [flutter plugin_platform_interface] - local_auth_windows 1.0.10 [flutter local_auth_platform_interface] - logging 1.2.0 - matcher 0.12.16 [async meta stack_trace term_glyph test_api] - material_color_utilities 0.5.0 [collection] - meta 1.10.0 - mime 1.0.4 - nested 1.0.0 [flutter] - nm 0.5.0 [dbus] - oauth2 2.0.2 [collection crypto http http_parser] - os_detect 2.0.1 - package_info_plus_platform_interface 2.0.1 [flutter meta plugin_platform_interface] - path_provider_android 2.2.1 [flutter path_provider_platform_interface] - path_provider_foundation 2.3.1 [flutter path_provider_platform_interface] - path_provider_linux 2.2.1 [ffi flutter path path_provider_platform_interface xdg_directories] - path_provider_platform_interface 2.1.1 [flutter platform plugin_platform_interface] - path_provider_windows 2.2.1 [ffi flutter path path_provider_platform_interface win32] - permission_handler_android 12.0.1 [flutter permission_handler_platform_interface] - permission_handler_apple 9.2.0 [flutter permission_handler_platform_interface] - permission_handler_html 0.1.0+2 [flutter flutter_web_plugins permission_handler_platform_interface] - permission_handler_platform_interface 4.0.2 [flutter meta plugin_platform_interface] - permission_handler_windows 0.2.0 [flutter permission_handler_platform_interface] - petitparser 5.4.0 [meta] - platform 3.1.3 - plugin_platform_interface 2.1.7 [meta] - pointycastle 3.7.3 [collection convert js] - provider 6.1.1 [collection flutter nested] - retry 3.1.2 - share_plus_platform_interface 3.3.1 [cross_file flutter meta mime plugin_platform_interface path_provider uuid] - shared_preferences_android 2.2.1 [flutter shared_preferences_platform_interface] - shared_preferences_foundation 2.3.4 [flutter shared_preferences_platform_interface] - shared_preferences_linux 2.3.2 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface] - shared_preferences_platform_interface 2.3.1 [flutter plugin_platform_interface] - shared_preferences_web 2.2.2 [flutter flutter_web_plugins shared_preferences_platform_interface web] - shared_preferences_windows 2.3.2 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - shelf 1.4.1 [async collection http_parser path stack_trace stream_channel] - sky_engine 0.0.99 - smithy 0.6.2 [async aws_common built_collection built_value collection convert crypto fixnum http_parser intl json_annotation meta path retry shelf typed_data xml] - smithy_aws 0.6.1 [aws_common aws_signature_v4 built_collection built_value collection convert crclib crypto intl json_annotation meta path smithy xml] - source_span 1.10.0 [collection path term_glyph] - sprintf 7.0.0 - sqlite3 2.2.0 [collection ffi js meta path] - stack_trace 1.11.1 [path] - stream_channel 2.1.2 [async] - stream_transform 2.1.0 - string_scanner 1.2.0 [source_span] - term_glyph 1.2.1 - test_api 0.6.1 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph] - timezone 0.9.2 [path] - tuple 2.0.2 - typed_data 1.3.2 [collection] - url_launcher_android 6.2.0 [flutter url_launcher_platform_interface] - url_launcher_ios 6.2.1 [flutter url_launcher_platform_interface] - url_launcher_linux 3.1.0 [flutter url_launcher_platform_interface] - url_launcher_macos 3.1.0 [flutter url_launcher_platform_interface] - url_launcher_platform_interface 2.2.0 [flutter plugin_platform_interface] - url_launcher_web 2.2.2 [flutter flutter_web_plugins url_launcher_platform_interface web] - url_launcher_windows 3.1.0 [flutter url_launcher_platform_interface] - uuid 4.2.1 [crypto sprintf meta] - vector_math 2.1.4 - web 0.3.0 - win32 5.1.1 [ffi] - win32_registry 1.1.2 [ffi win32] - worker_bee 0.2.2 [async aws_common built_collection built_value collection js meta path stack_trace stream_channel stream_transform] - xdg_directories 1.0.3 [meta path] - xml 6.3.0 [collection meta petitparser] - yaml 3.1.2 [collection source_span string_scanner] ```

danagbemava-nc commented 9 months ago

Hi @Fernandomr88, from what I understand, performance does not automatically capture http requests from dart (dio & http). Can you check the plugin example to see if it offers any hints on how you could do this in your project? Also, per https://github.com/firebase/flutterfire/issues/4397#issuecomment-825016396 since, you're using dio, could you try using an interceptor?

Fernandomr88 commented 9 months ago

Hi @Fernandomr88, from what I understand, performance does not automatically capture http requests from dart (dio & http). Can you check the plugin example to see if it offers any hints on how you could do this in your project? Also, per #4397 (comment) since, you're using dio, could you try using an interceptor?

I see, but that's not what it says on the documentation

https://firebase.google.com/docs/perf-mon/custom-network-traces?platform=flutter

Fernandomr88 commented 9 months ago

Also, at the example it uses a very basic code such as the documentation and mine.

Example:

  @override
  Future<StreamedResponse> send(BaseRequest request) async {
    // Custom network monitoring is not supported for web.
    // https://firebase.google.com/docs/perf-mon/custom-network-traces?platform=android
    final HttpMetric metric = FirebasePerformance.instance
        .newHttpMetric(request.url.toString(), HttpMethod.Get);

    metric.requestPayloadSize = request.contentLength;
    await metric.start();

    StreamedResponse response;
    try {
      response = await _inner.send(request);
      print(
        'Called ${request.url} with custom monitoring, response code: ${response.statusCode}',
      );

      metric.responseContentType = 'text/html';
      metric.httpResponseCode = response.statusCode;
      metric.responsePayloadSize = response.contentLength;

      metric.putAttribute('score', '15');
      metric.putAttribute('to_be_removed', 'should_not_be_logged');
    } finally {
      metric.removeAttribute('to_be_removed');
      await metric.stop();
    }
danagbemava-nc commented 9 months ago

Hi @Fernandomr88, using the plugin example, I do see the trace for BBC in my console. Can you try the plugin example app to see if that is the case for you as well?

Fernandomr88 commented 9 months ago

Hi @Fernandomr88, using the plugin example, I do see the trace for BBC in my console. Can you try the plugin example app to see if that is the case for you as well?

when I added the await before metric.start() and metric.stop(), it worked. Is that supposed to be so?

The problem is that at both methods documentation it says await is not necessary:

Captura de Tela 2023-12-14 às 09 31 59
/// Starts this [HttpMetric].
  ///
  /// Can only be called once.
  ///
  /// Using `await` with this method is only necessary when accurate timing
  /// is relevant.
  Future<void> start() {
    return _delegate.start();
  }

  /// Stops this [HttpMetric].
  ///
  /// Can only be called once and only after start(), Data collected is
  /// automatically sent to the associate Firebase console after stop() is
  /// called. You can confirm that Performance Monitoring results appear in the
  /// Firebase console. Results should appear within 12 hours.
  ///
  /// Not necessary to use `await` with this method.
  Future<void> stop() {
    return _delegate.stop();
  }
darshankawar commented 9 months ago

when I added the await before metric.start() and metric.stop(), it worked. Is that supposed to be so?

These methods are part of the parent send method which is Sends an HTTP request and asynchronously returns the response and that's the reason why you are seeing await which seems to be WAI.

Fernandomr88 commented 9 months ago

when I added the await before metric.start() and metric.stop(), it worked. Is that supposed to be so?

These methods are part of the parent send method which is Sends an HTTP request and asynchronously returns the response and that's the reason why you are seeing await which seems to be WAI.

So, correct me if I'm wrong, If await is needed in some cases that "optional" text on the documentation should not be there, right?

danagbemava-nc commented 9 months ago

Hi @Fernandomr88, I think it should be there but maybe the case where it is needed should be clarified. Would that help?

Fernandomr88 commented 9 months ago

Hi @Fernandomr88, I think it should there but maybe the case where it is needed should be clarified. Would that help?

It would definitely help.

Lyokone commented 2 months ago

Hello, I'm checking this issue and I'm sure what there is to clarify. The documentation does mention the await

final metric = FirebasePerformance.instance
    .newHttpMetric("https://www.google.com", HttpMethod.Get);

await metric.start();
final response = await http.get(Uri.parse("https://www.google.com/"));
await metric.stop();

And is properly typed as a Future. Maybe the answer would be to use https://dart.dev/tools/linter-rules/unawaited_futures

Fernandomr88 commented 2 months ago

Hello, I'm checking this issue and I'm sure what there is to clarify. The documentation does mention the await

final metric = FirebasePerformance.instance
    .newHttpMetric("https://www.google.com", HttpMethod.Get);

await metric.start();
final response = await http.get(Uri.parse("https://www.google.com/"));
await metric.stop();

And is properly typed as a Future. Maybe the answer would be to use https://dart.dev/tools/linter-rules/unawaited_futures

the issue is not with the documentation on the firebase website, but at code level as I mentioned above and it is still mentioning that: https://github.com/firebase/flutterfire/issues/12024#issuecomment-1855773955

Fernandomr88 commented 2 months ago

Since the documentation is still unclear, i don't see this as completed. @Lyokone

https://github.com/firebase/flutterfire/blob/master/packages/firebase_performance/firebase_performance/lib/src/http_metric.dart#L87