工作區型 Application Insights 資源

Azure 監視器Application Insights 工作區型資源整合 Application InsightsLog Analytics

藉由工作區型資源,Application Insights 將遙測傳送到通用 Log Analytics 工作區,以便能夠完整存取 Log Analytics 的全部功能,同時將應用程式、基礎結構和平台記錄保留在合併的單一位置。 這種整合也能夠在您的全部資源上進行一般 Azure 角色型存取控制,而不需要跨應用程式/工作區查詢。

注意

工作區型 Application Insights 資源的資料內嵌和保留會透過資料所在的 Log Analytics 工作區來計費。 若要深入了解工作區型 Application Insights 資源的計費,請參閱 Azure 監視器記錄定價詳細資料

提供的新功能

以工作區為基礎的 Application Insights 與 Azure 監視器和 Log Analytics 整合,以增強功能:

建立工作區型資源

登入 Azure 入口網站,並建立 Application Insights 資源。

顯示工作區型 Application Insights 資源的螢幕快照。

如果您沒有現有的 Log Analytics 工作區,請參閱 Log Analytics 工作區建立文件

工作區型資源目前適用於全部商業區域和 Azure Government。 在兩個不同的區域中擁有 Application Insights 和 Log Analytics 可能會影響延遲,並減少監視解決方案的整體可靠性。

在建立資源之後,您會在 [概 觀] 窗格中看到對應的工作區資訊。

顯示工作區名稱的螢幕快照。

選取藍色連結文字,移至相關聯的 Log Analytics 工作區,您可以在其中利用新的整合工作區查詢環境。

注意

我們仍會為您的 Application Insights 傳統資源查詢、活頁簿和記錄型警示提供完整的回溯相容性。 若要查詢或檢視新的工作區型資料表結構或結構描述,您必須先移至 Log Analytics 工作區。 選取 [Application Insights] 窗格中的 [記錄 (分析)],可讓您存取傳統 Application Insights 查詢體驗。

複製連接字串

連接字串可識別您想要與遙測資料建立關聯的資源。 您也可以將其用來修改您資源將用作遙測目的地的端點。 您必須複製連接字串,並將其新增至應用程式的程式碼或新增至環境變數。

設定監視

建立工作區型 Application Insights 資源之後,您可以設定監視。

程式碼型應用程式監視

針對程式碼型應用程式監視,您會安裝適當的 Application Insights SDK,並將連接字串指向新建立的資源。

如需如何設定 Application Insights SDK 以進行程式碼型監視的相關資訊,請參閱下列語言或架構的專屬文件:

無程式代碼監視

針對 Azure Functions 和 Azure App 服務 等服務的無程式代碼監視,您可以先建立以工作區為基礎的 Application Insights 資源。 然後,您會在設定監視時指向該資源。 或者,您可以在 Application Insights 啟用時建立新的 Application Insights 資源。

自動建立資源

Azure CLI

若要存取預覽 Application Insights Azure CLI 命令,您必須先執行:

 az extension add -n application-insights

如果您未執行 az extension add 命令,則會看到錯誤訊息,指出 az : ERROR: az monitor: 'app-insights' is not in the 'az monitor' command group. See 'az monitor --help'

現在您可以執行下列程式碼來建立您的 Application Insights 資源:

az monitor app-insights component create --app
                                         --location
                                         --resource-group
                                         [--application-type]
                                         [--ingestion-access {Disabled, Enabled}]
                                         [--kind]
                                         [--only-show-errors]
                                         [--query-access {Disabled, Enabled}]
                                         [--tags]
                                         [--workspace]

範例

az monitor app-insights component create --app demoApp --location eastus --kind web -g my_resource_group --workspace "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/test1234/providers/microsoft.operationalinsights/workspaces/test1234555"

如需此命令的完整 Azure CLI 文件,請參閱 Azure CLI 文件

Azure PowerShell

建立新的工作區型 Application Insights 資源。

New-AzApplicationInsights -Name <String> -ResourceGroupName <String> -Location <String> -WorkspaceResourceId <String>
   [-SubscriptionId <String>]
   [-ApplicationType <ApplicationType>]
   [-DisableIPMasking]
   [-DisableLocalAuth]
   [-Etag <String>]
   [-FlowType <FlowType>]
   [-ForceCustomerStorageForProfiler]
   [-HockeyAppId <String>]
   [-ImmediatePurgeDataOn30Day]
   [-IngestionMode <IngestionMode>]
   [-Kind <String>]
   [-PublicNetworkAccessForIngestion <PublicNetworkAccessType>]
   [-PublicNetworkAccessForQuery <PublicNetworkAccessType>]
   [-RequestSource <RequestSource>]
   [-RetentionInDays <Int32>]
   [-SamplingPercentage <Double>]
   [-Tag <Hashtable>]
   [-DefaultProfile <PSObject>]
   [-Confirm]
   [-WhatIf]
   [<CommonParameters>]

範例

New-AzApplicationInsights -Kind java -ResourceGroupName testgroup -Name test1027 -location eastus -WorkspaceResourceId "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/test1234/providers/microsoft.operationalinsights/workspaces/test1234555"

如需此 Cmdlet 的完整 PowerShell 文件,並了解如何擷取連接字串,請參閱 Azure PowerShell 文件

Azure Resource Manager 範本

@description('Name of Application Insights resource.')
param name string

@description('Type of app you are deploying. This field is for legacy reasons and will not impact the type of App Insights resource you deploy.')
param type string

@description('Which Azure Region to deploy the resource to. This must be a valid Azure regionId.')
param regionId string

@description('See documentation on tags: https://learn.microsoft.com/azure/azure-resource-manager/management/tag-resources.')
param tagsArray object

@description('Source of Azure Resource Manager deployment')
param requestSource string

@description('Log Analytics workspace ID to associate with your Application Insights resource.')
param workspaceResourceId string

resource component 'Microsoft.Insights/components@2020-02-02' = {
  name: name
  location: regionId
  tags: tagsArray
  kind: 'other'
  properties: {
    Application_Type: type
    Flow_Type: 'Bluefield'
    Request_Source: requestSource
    WorkspaceResourceId: workspaceResourceId
  }
}

參數檔案

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "name": {
      "value": "my_workspace_based_resource"
    },
    "type": {
      "value": "web"
    },
    "regionId": {
      "value": "westus2"
    },
    "tagsArray": {
      "value": {}
    },
    "requestSource": {
      "value": "CustomDeployment"
    },
    "workspaceResourceId": {
      "value": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testxxxx/providers/microsoft.operationalinsights/workspaces/testworkspace"
    }
  }
}

修改相關聯的工作區

建立工作區型 Application Insights 資源之後,您可以修改相關聯的 Log Analytics 工作區。

在 Application Insights 資源窗格中,選取 [屬性] > [變更工作區] > [Log Analytics 工作區]

匯出遙測

工作區型資源不支援舊版連續匯出功能。 因此,請在您的 Application Insights 資源中選取 [診斷設定]> [新增診斷設定]。 您可以選取所有資料表或資料表子集,以封存至儲存體帳戶。 您也可以串流至 Azure 事件中樞。

注意

診斷設定匯出可能會增加成本。 如需詳細資訊,請參閱<從 Application Insights 匯出遙測> (英文)。 如需此功能的定價資訊,請參閱 Azure 監視器定價頁面。 在計費開始之前,將會傳送通知。 如果您在通知期間之後繼續使用遙測資料匯出,將會以適用的費率計費。

我應該部署多少 Application Insights 資源?

當您在開發下一版 Web 應用程式時,您不會想看到新版與已發行版本的 Application Insights 遙測混合在一起。

為了避免混淆,請將不同開發階段的遙測,以個別的連接字串傳送到個別的 Application Insights 資源。

如果您的系統是 Azure 雲端服務的執行個體,則會有另一種設定個別連接字串的方法

關於資源和連接字串

在為 Web 應用程式設定 Application Insights 監視時,您會在 Azure 中建立 Application Insights 資源。 您可以在 Azure 入口網站 中開啟資源,以查看和分析從您的應用程式收集的遙測。 連接字串 識別資源。 您安裝 Application Insights 套件來監視應用程式時,您必須為這設定連接字串,以便讓這知道要將遙測傳送到哪裡。

每個 Application Insights 資源都隨附現成可用的計量。 如果個別元件會回報給相同的 Application Insights 資源,則針對這些計量發出警示可能沒有意義。

使用單一 Application Insights 資源的時機

使用單一 Application Insights 資源來進行:

  • 簡化部署在一起之應用程式的DevOps/ITOps管理,通常是由同一個小組開發及管理。
  • 預設會在儀錶板中集中處理關鍵效能指標,例如響應時間和失敗率。 視需要在計量總管中依角色名稱區隔。
  • 當應用程式元件之間不需要不同的 Azure 角色型存取控制管理時。
  • 當相同的計量警示準則、連續導出和跨元件計費/配額管理都足夠時。
  • 當 API 金鑰可以接受存取所有元件的數據時,且 10 個 API 金鑰符合所有元件的需求。
  • 當相同的智慧偵測和工作專案整合設定適用於所有角色時。

注意

如果您要合併多個 Application Insights 資源,您可以將現有應用程式元件指向新的合併 Application Insights 資源。 儲存在舊資源中的遙測不會傳輸到新資源。 請只在新資源中有足夠的遙測可實現商務持續性時,才刪除舊資源。

其他考量

若要啟用入口網站體驗,請新增自定義程式碼,以將有意義的值指派給 Cloud_RoleName 屬性。 如果沒有這些值,入口網站功能就無法運作。

針對 Azure Service Fabric 應用程式和傳統雲端服務,SDK 會從 Azure 角色環境讀取來自動設定服務。 針對其他應用程式類型,您通常需要明確設定它。

即時計量無法依角色名稱分割數據。

建立更多 Application Insights 資源

若要建立 Application Insights 資源,請參閱建立 Application Insights 資源

警告

如果您的 Application Insights 資源正在監視不同區域中的 Azure 資源 (亦即遙測產生端),可能會產生額外的網路費用。 是,費用會因為遙測的來源區域和目的地區域不同而有所差異。 如需詳細資訊,請參閱 Azure 頻寬定價

取得連接字串

連接字串會識別您所建立的資源。

您需要應用程式傳送資料的所有資源 連接字串。

篩選組建編號

當您發佈新版本的應用程式時,您想要能夠將遙測與不同的組建分開。

您可以設定 [應用程式版本] 屬性,如此便能篩選搜尋計量瀏覽器的結果。

設定 [應用程式版本] 屬性有幾種不同的方法。

  • 直接設定:

    telemetryClient.Context.Component.Version = typeof(MyProject.MyClass).Assembly.GetName().Version;

  • 遙測初始設定式中將該行換行,以確保會以一致性方式設定所有 TelemetryClient 執行個體。

  • ASP.NET:在 BuildInfo.config 中設定版本。 Web 模組會從 BuildLabel 節點取得版本。 在您的專案中包含此檔案,而且記得要在方案總管中設定 [永遠複製] 屬性。

    <?xml version="1.0" encoding="utf-8"?>
    <DeploymentEvent xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/VisualStudio/DeploymentEvent/2013/06">
      <ProjectName>AppVersionExpt</ProjectName>
      <Build type="MSBuild">
        <MSBuild>
          <BuildLabel kind="label">1.0.0.2</BuildLabel>
        </MSBuild>
      </Build>
    </DeploymentEvent>
    
    
  • ASP.NET:在 Microsoft Build Engine 中自動產生 BuildInfo.config。 在 .csproj 檔案中新增幾行:

    <PropertyGroup>
      <GenerateBuildInfoConfigFile>true</GenerateBuildInfoConfigFile>    <IncludeServerNameInBuildInfo>true</IncludeServerNameInBuildInfo>
    </PropertyGroup>
    

    此步驟會產生一個名為 yourProjectName.BuildInfo.config 的檔案。 發佈程序會將這個檔案重新命名為 BuildInfo.config

    當您使用 Visual Studio 建置時,組建標籤會包含佔位元 (*AutoGen_...*) 。 但使用 Microsoft Build Engine 建置時,則會填入正確的版本號碼。

    若要允許 Microsoft Build Engine 產生版本號碼,請在 AssemblyReference.cs 中設定類似 1.0.* 的版本。

版本和版次追蹤

若要追蹤應用程式版本,請確定您的 Microsoft Build Engine 程序已產生 buildinfo.config。 在您的 .csproj 檔案中,新增:

<PropertyGroup>
  <GenerateBuildInfoConfigFile>true</GenerateBuildInfoConfigFile>
  <IncludeServerNameInBuildInfo>true</IncludeServerNameInBuildInfo>
</PropertyGroup>

當 Application Insights Web 模組有組建資訊時,就會自動新增應用程式版本,作為每個遙測項目的屬性。 因此,您可以在執行診斷搜尋或在探索計量時,依據版本來篩選。

Microsoft Build Engine 僅會產生組建版本號碼,而不是來自 Visual Studio 的開發人員組建。

版本註釋

如果您使用 Azure DevOps,您可以取得註解標記 (每當發行新版本時,此標記就會新增至您的圖表)。

常見問題集

本節提供常見問題的答案。

如何將 Application Insights 資源移至新區域?

不支援在區域之間傳輸現有的 Application Insights 資源,而且您無法將歷程記錄數據遷移至新的區域。 因應措施包括:

  • 在所需的區域中建立以工作區為基礎的 Application Insights 資源。
  • 在新的資源中重新建立原始資源的任何唯一自定義專案。
  • 使用新區域資源的 連接字串 更新應用程式。
  • 測試以確保所有專案都如預期般運作,並搭配新的 Application Insights 資源使用。
  • 決定保留或刪除原始的Application Insights資源。 刪除傳統資源表示遺失所有歷程記錄數據。 如果資源是以工作區為基礎,則數據會保留在Log Analytics中,以便在保留期間到期之前存取歷程記錄數據。

在新區域中通常需要手動重新建立或更新資源的唯一自訂,包括但不限於:

  • 重新建立自訂儀表板和活頁簿。
  • 重新建立或更新任何自訂記錄/計量警示的範圍。
  • 重新建立可用性警示。
  • 重新建立您的使用者存取新資源所需的自訂 Azure 角色型存取控制。
  • 複寫牽涉到內嵌取樣、資料保留、每日上限和自訂計量的設定。 這些設定會透過使用者和預估成本窗格控制。
  • 需要 API 金鑰的任何整合,例如版本註釋即時計量安全控制通道。 您必須產生新的 API 金鑰,並更新相關聯的整合。
  • 必須再次設定傳統資源中的連續匯出。
  • 以工作區為基礎的資源中的診斷設定必須重新設定。

注意

如果您在新區域中建立的資源即將取代傳統資源,我們建議您探索建立新的工作區型資源。 或者,將現有的資源遷移至以工作區為基礎的優點

我可以在 Azure Resource Manager 部署中使用 `providers('Microsoft.Insights', 'components').apiVersions[0]` 嗎?

我們不建議使用此方法來填入 API 版本。 最新版本可代表其中可能包含中斷性變更的預覽版本。 即使使用較新的非預覽版本,API 版本也不一定與現有範本回溯相容。 在某些情況下,API 版本可能無法供所有訂用帳戶使用。

下一步