共用方式為


啟用Azure Monitor OpenTelemetry 支援 .NET、Node.js、Python 及 Java 應用程式

本文說明如何在 Azure Monitor Application Insights 中啟用並配置基於 OpenTelemetry 的資料收集。 Azure 監視器 OpenTelemetry Distro:

  • 提供 OpenTelemetry 發行版,包含對 Azure Monitor 特定功能的支援。
  • 藉由包含 OpenTelemetry 檢測程式庫來收集追蹤、計量、記錄和例外狀況,以啟用自動遙測。
  • 允許收集自訂遙測。
  • 支援 即時度量 ,以監控並收集來自現場、正在生產的網頁應用程式的遙測數據。

欲了解更多使用 Azure Monitor OpenTelemetry 發行版的優點,請參閱 為什麼應該使用 Azure Monitor OpenTelemetry 發行版

想了解更多使用 OpenTelemetry 收集資料的資訊,請參閱 Collect OpenTelemetry(OTel)以獲得應用洞察體驗OpenTelemetry 常見問題

OpenTelemetry 發行狀態

OpenTelemetry 提供.NET、Node.js、Python 及 Java 應用。 如需逐個功能的發佈狀態,請參閱常見問題

附註

關於 Azure 功能應用程式,請參見 將 OpenTelemetry 用於 Azure Functions

使用 Application Insights 啟用 OpenTelemetry

請遵循本節中的步驟,使用 OpenTelemetry 檢測您的應用程式。 選取索引標籤以取得特定語言的指示。

附註

.NET 涵蓋多種情境,包括經典 ASP.NET、主控台應用程式、Windows Forms(WinForms )等。

必要條件

秘訣

如果您正從 Application Insights Classic API 遷移,請參閱我們的移轉文件

安裝用戶端程式庫

安裝最新的 Azure.Monitor.OpenTelemetry.AspNetCoreNuGet 套件

dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore 

修改您的應用程式

匯入 Azure.Monitor.OpenTelemetry.AspNetCore 命名空間,新增 OpenTelemetry,並設定它在你的 program.cs 類別中使用 Azure Monitor:

// Import the Azure.Monitor.OpenTelemetry.AspNetCore namespace.
using Azure.Monitor.OpenTelemetry.AspNetCore;

var builder = WebApplication.CreateBuilder(args);

// Add OpenTelemetry and configure it to use Azure Monitor.
builder.Services.AddOpenTelemetry().UseAzureMonitor();

var app = builder.Build();

app.Run();

從您的 Application Insights 資源複製連線字串

connection string 是唯一的,並指定 Azure Monitor OpenTelemetry 發行版將所收集的遙測資料傳送到哪裡。

秘訣

如果您還沒有可以使用的 Application Insights 資源,請遵循本指南建立資源。 建議您建立新的資源,而不是使用現有的資源

要複製「connection string」:

  1. 移至 Application Insights 資源的 [概觀] 窗格。
  2. 尋找您的連接字串
  3. 將滑鼠移到connection string上,選擇複製到剪貼板圖示。

顯示 Application Insights 總覽及連接字串的截圖。

把 connection string 貼到你的環境裡

要貼上你的 connection string,請從以下選項中選擇:

重要

我們建議僅在本地開發與測試環境中使用程式碼設置連線字串。

生產環境時,請使用環境變數或設定檔(僅限 Java)。

  • 透過環境變數設定 - 建議

    請將以下指令中的 <Your connection string> 替換成你的 connection string。

    APPLICATIONINSIGHTS_CONNECTION_STRING=<Your connection string>
    
  • 透過設定檔設定 - 僅限 Java

    建立名為 applicationinsights.json 的組態檔,並將其置於具有與 applicationinsights-agent-3.7.5.jar 下列內容的相同目錄下:

    {
      "connectionString": "<Your connection string>"
    }
    

    將前面 JSON 中的 <Your connection string> 替換成 您的唯一的連接字串。

  • 透過代碼設定 - ASP.NET Core、Node.js,以及僅Python

    請參見 connection string configuration 範例,說明如何透過程式碼設定connection string。

附註

如果你在多個地方設定 connection string,環境變數的優先順序如下:

  1. Code
  2. 環境變數
  3. 組態檔

確認資料正在流動

執行你的應用程式,然後在 Azure 入口開啟 Application Insights。 資料可能需要幾分鐘的時間才會顯示。

此螢幕擷取畫面顯示 Application Insights 的 [概觀] 索引標籤,其中醒目提示伺服器要求和伺服器回應時間。

Application Insights 現在已為您的應用程式啟用。 下列步驟是選擇性的,並允許進一步自訂。

附註

在使用 Application Insights 檢測的過程中,我們會收集診斷資料並且傳送給 Microsoft。 此資料可協助我們執行及改善 Application Insights。 欲了解更多,請參閱應用洞察常見問題集。

重要

如果您有兩個或兩個以上服務將遙測發出至相同的 Application Insights 資源,則必須設定雲端角色名稱,才能在應用程式地圖上正確地代表服務。

故障排除、回饋與支援

秘訣

以下章節在所有 OpenTelemetry Distro 文章中皆可查閱。

Troubleshooting

OpenTelemetry 意見反應

若要提供意見反應:

支援

選取您選擇的語言索引標籤,以探索支援選項。

下一步