在 Azure Monitor 中跨 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 中受支持。
  • 对跨资源(如另一个工作区)的引用应是显式的,且不能参数化。

使用函数跨工作区、应用程序和资源进行查询

本部分介绍如何使用函数和不使用函数查询工作区、应用程序和资源。

不使用函数进行查询

你可以从任何资源实例查询多个资源。 这些资源可以是工作区和应用的组合。

跨三个工作区进行查询的示例:

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 Monitor 日志查询中的函数将查询逻辑与查询资源的范围分开。 这种方法可以简化查询结构。 以下示例演示了如何监视多个 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 资源管理器模板来编辑警报规则,以更新范围内资源。 此外,也可以在日志搜索警报查询中添加资源列表。

使用 workspace() 跨 Log Analytics 工作区进行查询

使用 workspace() 表达式从同一资源组、另一个资源组或另一个订阅的特定工作区中检索数据。 可以使用此表达式在 Log Analytics 查询中包含日志数据以及在日志查询中跨多个工作区查询数据。

语法

workspace(标识符)

参数

*Identifier*:使用下表中的某种格式标识工作区。

标识符 说明 示例
ID 工作区 GUID workspace("00000000-0000-0000-0000-000000000000")
Azure 资源 ID 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*:使用下表中的某种格式标识应用。

标识符 说明 示例
ID 应用的 GUID app("00000000-0000-0000-0000-000000000000")
Azure 资源 ID 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 Monitor 查询中使用,可以从其他资源检索数据。

语法

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 Monitor 中分析日志数据来大致了解日志查询以及 Azure Monitor 日志数据的组织结构。