搜尋套件、儲存庫與 APP 分類

azure_application_insights 封面
套件詳情v5.0.1

azure_application_insights

用於與 Azure 的 Application Insights 服務整合的 Dart 客戶端

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
3.5萬
喜歡數
49
Pub 分數
160

安裝套件

flutter pub add azure_application_insights

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Dart 客戶端,可實現與 Azure Application Insights 的整合,讓開發者能夠從跨多個平台的 Flutter 應用程式中收集遙測資料,例如記錄、例外狀況和效能指標。它支援在 Android、iOS、Web、macOS、Windows 和 Linux 上執行的應用程式的即時監控與診斷。此套件簡化了將應用洞察傳送至 Azure 的過程,有助於除錯、效能最佳化和使用者行為分析。專為在 Flutter 專案中無縫使用而設計,設定需求極少。

適用情境

  • 監控生產環境中的應用效能
  • 追蹤使用者互動和會話資料
  • 捕獲異常和錯誤以進行除錯
  • 分析崩潰報告和失敗率
  • 移動和Web應用的即時可觀測性

主要特色

  • 跨平台遙測收集
  • 對日誌、事件和異常的支援
  • 與 Azure Application Insights 電子看板整合
  • 最簡設定和設定
  • 自動追蹤應用生命週期事件

適合對象

  • 使用 Azure 進行分析的 Flutter 開發人員
  • 需要跨平台監控的團隊
  • 需要詳細錯誤追蹤的應用程式
  • 專注於效能優化的專案
  • 實施可觀測性實踐的開發人員

使用建議

  • 需要一個 Azure Application Insights 資源
  • 數據隱私和合規性考慮
  • 對應用大小和效能的潛在影響
  • 依賴網路連接進行遙測傳輸
  • 與原生 SDK 相比,自訂功能有限

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Azure Application Insights

pub package

All Contributors

What?

This is a Dart library that integrates with Azure's Application Insights service. It allows you to push telemetry items into your Application Insights instance.

Why?

Application Insights is a powerful tool for understanding how your software system is operating. In the context of Flutter applications, it allows you to deeply understand user interactions, performance, and faults in your application.

Where?

Firstly, install the azure_application_insights package, then you can import it into your Dart code as follows:

import 'package:azure_application_insights/azure_application_insights.dart';

How?

The only piece of information you need to integrate with Application Insights is your connection string, which you can find inside the Azure portal. Open your Application Insights resource and look in the Overview tab.

Once you have your connection string, you can construct a TelemetryClient as follows:

final processor = BufferedProcessor(
  next: TransmissionProcessor(
    connectionString: connectionString,
    httpClient: client,
    timeout: const Duration(seconds: 10),
  ),
);

final telemetryClient = TelemetryClient(
  processor: processor,
);

This is a typical setup where telemetry items are buffered before being transmitted. Depending on your processing needs, you may have a need for more than one TelemetryClient in your application. For example, you might have one TelemetryClient that buffers telemetry items and is used for all telemetry other than errors, and a second that does not buffer and is used only to submit errors as promptly as possible. Please review the example code and API docs for alternative configurations.

Once you have a TelemetryClient, you can simply invoke the various methods to capture telemetry items:

telemetryClient.trackTrace(
  severity: Severity.information,
  message: 'Hello from Dart!',
);

You may also want to configure additional properties to be submitted with telemetry items. These can be configured in two places:

  1. On the TelemetryContext associated with your TelemetryClient. Properties in this context object will be attached to every telemetry item submitted. Moreover, you can share a TelemetryContext between multiple TelemetryClient instances if desired.
  2. Every method on TelemetryClient allows you to specify additionalProperties that will be captured only for that telemetry item. As the name suggests, these properties are in addition to those within the context.

Here are examples of both:

final telemetryClient = ...;

// 1. Properties associated with the TelemetryClient will be attached to
// all telemetry items submitted via that client.
telemetryClient.context
    // These built-in helpers simply set the pre-defined properties
    // Application Insights provides.
    ..applicationVersion = 'my version'
    ..device.type = 'Android';
    // But you can also set whatever property name you like.
    ..properties['environmentName'] = 'dev';

// 2. Additional properties can be bundled with individual telemetry items.
telemetryClient.traceTrace(
    severity: Severity.information,
    message: 'An example',
    additionalProperties: <String, Object>{
        'answer': 42,
    },
);

Of course, you can leverage whatever data sources and third party libraries make sense in order to populate properties. Typically you would use a package like device_info_plus to obtain information on the device and fill in the appropriate properties on the context.

Flutter Integration

To submit crashes in Flutter applications as telemetry, follow the following recipe:

void main() {
    // You probably don't want to always run with crash reporting because it interferes with the normal
    // debug/development experience. Here we use kReleaseMode to only enable crash reporting for release builds, but
    // you can use whatever criteria and mechanism you like.
    if (kReleaseMode) {
        runWithCrashReporting(codeToExecute: run);
    } else {
        run();
    }
}

void run() => runApp(MyApp());

Future<void> runWithCrashReporting({
  required VoidCallback codeToExecute,
}) async {
  // Hook into Flutter error handling.
  FlutterError.onError = (details) => submitErrorAsTelemetry(
        isFatal: true,
        error: details.exception,
        trace: details.stack,
      );

  // Run the code to execute in a zone and handle all errors within.
  runZonedGuarded(
    codeToExecute,
    (error, trace) => submitErrorAsTelemetry(
      isFatal: true,
      error: error,
      trace: trace,
    ),
  );
}

Future<void> submitErrorAsTelemetry({
  required bool isFatal,
  required Object error,
  required StackTrace trace,
}) async {
  debugPrint('reporting ${isFatal ? 'fatal' : 'non-fatal'} error: $error');
  debugPrint('$trace');

  try {
    // Get your TelemetryClient instance here, perhaps by DI or some other mechanism.
    final telemetryClient = ...;

    // Get any additional properties for the crash report here, such as device information.
    final errorProperties = ...;

    // Write an error telemetry item.
    telemetryClient.trackError(
      error: error,
      stackTrace: trace,
      severity: isFatal ? Severity.critical : Severity.error,
      additionalProperties: errorProperties,
    );

    if (isFatal) {
      await telemetryClient.flush();
    }
  } on Object catch (e, t) {
    // We print synchronously here to ensure the output is written in the case we force exit.
    debugPrintSynchronously('Sending error telemetry failed: $e\r\n$t');
    debugPrintSynchronously('Original error: $error');
  } finally {
    if (isFatal && kReleaseMode) {
      debugPrintSynchronously('Forcing exit');
    }
  }
}
HTTP Middleware

Whilst you can certainly use TelemetryClient.trackDependency to track HTTP requests yourself, you can save time by using the TelemetryHttpClient HTTP client. This class will automatically time requests and forward on the details of completed requests to a given TelemetryClient as dependency telemetry items.

Typical usage looks like this:

final telemetryHttpClient = TelemetryHttpClient(
  telemetryClient: telemetryClient,
  inner: Client(),
);

// This request will be captured as a dependency telemetry item.
await telemetryHttpClient.get('https://kent-boogaart.com/');

The TelemetryHttpClient could be one piece in a long chain of HTTP middleware - it is composable.

Who?

Thanks goes to these wonderful people (emoji key):

Kent Boogaart
Kent Boogaart

💻 ⚠️ 📖 💡
Daniel Luz
Daniel Luz

💻
Robert Sandberg
Robert Sandberg

💻
Robson Silva
Robson Silva

💻
Michael Gugler
Michael Gugler

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

所屬分類

依賴

依賴清單 5 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。