在 Azure 監視器中跨 Log Analytics 工作區、應用程式和資源查詢資料

有兩種方式可查詢多個工作區、應用程式和資源中的資料:

  • 使用 workspace()app()resource() 運算式明確指定工作區、應用程式或資源資訊,如本文所述。
  • 使用資源內容查詢隱含指定。 在特定資源、資源群組或訂用帳戶環境中查詢時,查詢會從內含這些資源資料的所有工作區擷取相關資料。 資源內容查詢不會從傳統 Application Insights 資源擷取資料。

本文說明如何使用 workspace()app()resource() 運算式,從多個 Log Analytics 工作區、應用程式和資源查詢資料。

如果您透過 Azure Lighthouse 管理其他 Microsoft Entra 租用戶中的訂用帳戶,您可以在查詢中加入在這些客戶租用戶中建立的 Log Analytics 工作區

重要

如果您使用以工作區為基礎的 Application Insights 資源,遙測會與所有其他記錄資料一起儲存在 Log Analytics 工作區中。 使用 workspace() 運算式,從多個工作區中的應用程式查詢資料。 您不需要跨工作區查詢,就可以從相同工作區中的多個應用程式查詢資料。

需要的權限

  • 例如,對於查詢的 Log Analytics 工作區,您必須擁有 Microsoft.OperationalInsights/workspaces/query/*/read 權限,如 Log Analytics 讀取器內建角色所提供。
  • 例如,若要儲存查詢,則對於您要儲存查詢的查詢套件,您必須擁有 microsoft.operationalinsights/querypacks/queries/action 權限,如 Log Analytics 參與者內建角色所提供。

限制

  • 跨資源和跨服務查詢不支援參數化函式和定義含有其他跨工作區或跨服務運算式 (包括 adx()arg()resource()workspace()app()) 的函式。
  • 單一查詢最多可以含有 100 個 Log Analytics 工作區或傳統 Application Insights 資源。
  • 跨大量資源的查詢可能會大幅降低查詢的速度。
  • 只有最新的 scheduledQueryRules API 支援記錄搜尋警示中的跨資源查詢。 如果您使用舊版 Log Analytics 警示 API,則必須切換至最新 API
  • 跨資源的參考 (例如另一個工作區) 應該明確且無法參數化。

使用函式跨工作區、應用程式和資源進行查詢

本節說明如何使用函式和不使用函式的情況下,查詢工作區、應用程式和資源。

不使用函式的查詢

您可以查詢任何資源執行個體的多個資源。 這些資源可以是工作區和應用程式合併。

跨三個工作區的查詢範例:

union 
  Update, 
  workspace("00000000-0000-0000-0000-000000000001").Update, 
  workspace("00000000-0000-0000-0000-000000000002").Update
| where TimeGenerated >= ago(1h)
| where UpdateState == "Needed"
| summarize dcount(Computer) by Classification

如需 union、where 和 summarize 運算子的詳細資訊,請參閱 union 運算子where 運算子summarize 運算子

使用函式進行查詢

使用跨資源查詢將資料從多個 Log Analytics 工作區和 Application Insights 元件相互關聯時,查詢可能會變得複雜且難以維護。 您應該利用 Azure 監視器記錄查詢中的函式,將查詢邏輯與查詢資源的範圍分開。 這個方法會簡化查詢結構。 下列範例示範如何監視多個 Application Insights 元件,並將失敗的要求計數依應用程式名稱視覺化。

建立參考 Application Insights 元件範圍的查詢,如下列範例所示。 withsource= SourceApp 命令會新增一個資料行,此資料行可指定傳送記錄的應用程式名稱。 使用別名applicationsScoping將查詢儲存為函式

// crossResource function that scopes my Application Insights components
union withsource= SourceApp
app('00000000-0000-0000-0000-000000000000').requests, 
app('00000000-0000-0000-0000-000000000001').requests,
app('00000000-0000-0000-0000-000000000002').requests,
app('00000000-0000-0000-0000-000000000003').requests,
app('00000000-0000-0000-0000-000000000004').requests

您現在可以在跨資源查詢中使用此函式,如下列範例所示。 函式別名 applicationsScoping 會從所有定義的應用程式傳送要求資料表的聯集。 接著,查詢會篩選失敗的要求,並依應用程式將趨勢視覺化。 在此範例中,parse 運算子是選用的。 它會從 SourceApp 屬性擷取應用程式名稱。

applicationsScoping 
| where timestamp > ago(12h)
| where success == 'False'
| parse SourceApp with * '(' applicationId ')' * 
| summarize count() by applicationId, bin(timestamp, 1h) 
| render timechart

注意

此方法無法與記錄搜尋警示搭配使用,因為警示規則資源的存取驗證 (包括工作區和應用程式) 會在警示建立時執行。 不支援在警示建立後將新資源新增至函式。 如果您想要在記錄搜尋警示中使用函式來設定資源範圍,您必須在入口網站中編輯警示規則,或使用 Azure Resource Manager 範本來更新有限範圍資源。 或者,您可以在記錄搜尋警示查詢中納入資源清單。

使用 workspace() 跨 Log Analytics 工作區進行查詢

使用 workspace() 運算式,從相同資源群組、另一個資源群組或另一個訂用帳戶中的特定工作區擷取資料。 您可以使用此運算式在 Application Insights 查詢中包含記錄資料,以及在記錄查詢中跨多個工作區查詢資料。

語法

workspace(Identifier)

引數

*Identifier*:使用下表中的其中一個格式識別工作區。

識別碼 描述 範例
識別碼 工作區的 GUID workspace("00000000-0000-0000-0000-000000000000")
Azure 資源識別碼 Azure 資源的識別碼 workspace("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Contoso/providers/Microsoft.OperationalInsights/workspaces/contosoretail")

範例

workspace("00000000-0000-0000-0000-000000000000").Update | count
workspace("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Contoso/providers/Microsoft.OperationalInsights/workspaces/contosoretail").Event | count
union 
( workspace("00000000-0000-0000-0000-000000000000").Heartbeat | where Computer == "myComputer"),
(app("00000000-0000-0000-0000-000000000000").requests | where cloud_RoleInstance == "myRoleInstance")
| count  
union 
(workspace("00000000-0000-0000-0000-000000000000").Heartbeat), (app("00000000-0000-0000-0000-000000000000").requests) | where TimeGenerated between(todatetime("2023-03-08 15:00:00") .. todatetime("2023-04-08 15:05:00"))

使用 app() 跨傳統 Application Insights 應用程式進行查詢

使用 app 運算式從相同資源群組、其他資源群組或其他訂用帳戶中的特定傳統 Application Insights 資源擷取資料。 如果您使用以工作區為基礎的 Application Insights 資源,遙測會與所有其他記錄資料一起儲存在 Log Analytics 工作區中。 使用 workspace() 運算式,從多個工作區中的應用程式查詢資料。 您不需要跨工作區查詢,就可以從相同工作區中的多個應用程式查詢資料。

語法

app(Identifier)

引數

*Identifier*:使用下表中的其中一個格式來識別應用程式。

識別碼 描述 範例
識別碼 應用程式的 GUID app("00000000-0000-0000-0000-000000000000")
Azure 資源識別碼 Azure 資源的識別碼 app("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Fabrikam/providers/microsoft.insights/components/fabrikamapp")

範例

app("00000000-0000-0000-0000-000000000000").requests | count
app("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Fabrikam/providers/microsoft.insights/components/fabrikamapp").requests | count
union 
(workspace("00000000-0000-0000-0000-000000000000").Heartbeat | where Computer == "myComputer"),
(app("00000000-0000-0000-0000-000000000000").requests | where cloud_RoleInstance == "myColumnInstance")
| count  
union 
(workspace("00000000-0000-0000-0000-000000000000").Heartbeat), (app("00000000-0000-0000-0000-000000000000").requests)
| where TimeGenerated between(todatetime("2023-03-08 15:00:00") .. todatetime("2023-04-08 15:05:00"))

使用 resource() 關聯資源間的資料

resource 運算式會用於範圍設定為資源的 Azure 監視器查詢,以從其他資源擷取資料。

語法

resource(Identifier)

引數

*Identifier*:識別要用來關聯資料的資源、資源群組或訂用帳戶。

識別碼 描述 範例
資源 包含資源的資料。 resource("/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcesgroups/myresourcegroup/providers/microsoft.compute/virtualmachines/myvm")
資源群組或訂用帳戶 包含資源的資料及其包含的所有資源。 resource("/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcesgroups/myresourcegroup)

範例

union (Heartbeat),(resource("/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcesgroups/myresourcegroup/providers/microsoft.compute/virtualmachines/myvm").Heartbeat) | summarize count() by _ResourceId, TenantId
union (Heartbeat),(resource("/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcesgroups/myresourcegroup).Heartbeat) | summarize count() by _ResourceId, TenantId

下一步

請參閱分析 Azure 監視器中的記錄資料,了解記錄查詢的簡短概觀,以及建置 Azure 監視器記錄資料結構的方式。