有兩種方式可查詢多個工作區、應用程式和資源中的資料:
- 明確 - 使用 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
如需詳細資訊,請參閱 聯集運算子、 where 運算子和 摘要運算子。
使用函數查詢
使用跨資源查詢將資料從多個 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:下表中的明確識別碼格式是查詢工作區的最佳方式,因為它們是最有效率的。
| 識別碼 | 描述 | 範例 |
|---|---|---|
| ID | 工作區的 GUID | workspace("00000000-0000-0000-0000-000000000000") |
| Azure 資源識別碼 | Azure 資源的識別碼 | workspace("/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/Contoso/providers/Microsoft.OperationalInsights/workspaces/contosoretail") |
不建議使用僅使用工作區名稱或工作區資源名稱的其他工作區識別碼,因為它們會導致效能降低,並在跨工作區查詢時可能發生錯誤。
範例
workspace("00000000-0000-0000-0000-000000000000").Update | count
workspace("/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/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:下表中的明確識別碼格式是查詢應用程式的最佳方式,因為它們是最有效率的。
| 識別碼 | 描述 | 範例 |
|---|---|---|
| ID | 應用程式的 GUID | app(“00000000-0000-0000-0000-000000000000”) |
| Azure 資源識別碼 | Azure 資源的識別碼 | app("/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourcegroups/Fabrikam/providers/microsoft.insights/components/fabrikamapp") |
範例
app("00000000-0000-0000-0000-000000000000").requests | count
app("/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/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:識別要用來關聯資料的資源、資源群組或訂用帳戶。
| 識別碼 | 描述 | 範例 |
|---|---|---|
| 資源 | 包含資源的資料。 | 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 監視器中的記錄數據 。