|
1 | 1 | ---
|
2 |
| -title: Query across resources with Azure Monitor | Microsoft Docs |
3 |
| -description: This article describes how you can query against resources from multiple workspaces and an Application Insights app in your subscription. |
4 |
| -ms.topic: conceptual |
| 2 | +title: Query across resources with Azure Monitor |
| 3 | +description: Query and correlated data from multiple Log Analytics workspaces, applications, or resources using the `workspace()`, `app()`, and `resource()` Kusto Query Language (KQL) expressions. |
| 4 | +ms.topic: how-to |
5 | 5 | author: guywi-ms
|
6 | 6 | ms.author: guywild
|
7 | 7 | ms.date: 05/30/2023
|
| 8 | +# Customer intent: As a data analyst, I want to write KQL queries that correlate data from multiple Log Analytics workspaces, applications, or resources, to enable my analysis. |
8 | 9 |
|
9 | 10 | ---
|
10 | 11 |
|
11 |
| -# Create a log query across multiple workspaces and apps in Azure Monitor |
| 12 | +# Query data across Log Analytics workspaces, applications, and resources in Azure Monitor |
12 | 13 |
|
13 |
| -Azure Monitor Logs support querying across multiple Log Analytics workspaces and Application Insights apps in the same resource group, another resource group, or another subscription. This capability provides you with a systemwide view of your data. |
| 14 | +There are two ways to query data from multiple workspaces, applications, and resources: |
14 | 15 |
|
15 |
| -If you manage subscriptions in other Microsoft Entra tenants through [Azure Lighthouse](../../lighthouse/overview.md), you can include [Log Analytics workspaces created in those customer tenants](../../lighthouse/how-to/monitor-at-scale.md) in your queries. |
| 16 | +* Explicitly by specifying the workspace, app, or resource information using the [workspace()](#query-across-log-analytics-workspaces-using-workspace), [app()](#query-across-classic-application-insights-applications-using-app), or [resource()](#correlate-data-between-resources-using-resource) expressions, as described in this article. |
| 17 | +* Implicitly by using [resource-context queries](manage-access.md#access-mode). When you query in the context of a specific resource, resource group, or a subscription, the query retrieves relevant data from all workspaces that contain data for these resources. Resource-context queries don't retrieve data from classic Application Insights resources. |
16 | 18 |
|
17 |
| -There are two methods to query data that's stored in multiple workspaces and apps: |
| 19 | +This article explains how to use the `workspace()`, `app()`, and `resource()` expressions to query data from multiple Log Analytics workspaces, applications, and resources. |
18 | 20 |
|
19 |
| -* Explicitly by specifying the workspace and app information. This technique is used in this article. |
20 |
| -* Implicitly by using [resource-context queries](manage-access.md#access-mode). When you query in the context of a specific resource, resource group, or a subscription, the relevant data will be fetched from all workspaces that contain data for these resources. Application Insights data that's stored in apps won't be fetched. |
| 21 | +If you manage subscriptions in other Microsoft Entra tenants through [Azure Lighthouse](../../lighthouse/overview.md), you can include [Log Analytics workspaces created in those customer tenants](../../lighthouse/how-to/monitor-at-scale.md) in your queries. |
21 | 22 |
|
22 | 23 | > [!IMPORTANT]
|
23 |
| -> If you're using a [workspace-based Application Insights resource](../app/create-workspace-resource.md), telemetry is stored in a Log Analytics workspace with all other log data. Use the `workspace()` expression to write a query that includes applications in multiple workspaces. For multiple applications in the same workspace, you don't need a cross-workspace query. |
| 24 | +> If you're using a [workspace-based Application Insights resource](../app/create-workspace-resource.md), telemetry is stored in a Log Analytics workspace with all other log data. Use the `workspace()` expression to query data from applications in multiple workspaces. You don't need a cross-workspace query to query data from multiple applications in the same workspace. |
24 | 25 |
|
25 | 26 | ## Permissions required
|
26 | 27 |
|
27 | 28 | - You must have `Microsoft.OperationalInsights/workspaces/query/*/read` permissions to the Log Analytics workspaces you query, as provided by the [Log Analytics Reader built-in role](./manage-access.md#log-analytics-reader), for example.
|
28 | 29 | - To save a query, you must have `microsoft.operationalinsights/querypacks/queries/action` permisisons to the query pack where you want to save the query, as provided by the [Log Analytics Contributor built-in role](./manage-access.md#log-analytics-contributor), for example.
|
29 | 30 |
|
30 |
| -## Cross-resource query limits |
| 31 | +## Limitations |
31 | 32 |
|
32 |
| -* The number of Application Insights components and Log Analytics workspaces that you can include in a single query is limited to 100. |
| 33 | +* Cross-resource and cross-service queries don’t support parameterized functions and functions whose definition includes other cross-workspace or cross-service expressions, including `adx()`, `arg()`, `resource()`, `workspace()`, and `app()`. |
| 34 | +* You can include up to 100 Log Analytics workspaces or classic Application Insights resources in a single query. |
33 | 35 | * Querying across a large number of resources can substantially slow down the query.
|
34 | 36 | * Cross-resource queries in log alerts are only supported in the current [scheduledQueryRules API](/rest/api/monitor/scheduledqueryrule-2018-04-16/scheduled-query-rules). If you're using the legacy Log Analytics Alerts API, you'll need to [switch to the current API](../alerts/alerts-log-api-switch.md).
|
35 |
| -* References to a cross resource, such as another workspace, should be explicit and can't be parameterized. See [Gather identifiers for Log Analytics workspaces](?tabs=workspace-identifier#gather-identifiers-for-log-analytics-workspaces-and-application-insights-resources) for examples. |
36 |
| - |
37 |
| -## Gather identifiers for Log Analytics workspaces and Application Insights resources |
38 |
| - |
39 |
| -To reference another workspace in your query, use the [workspace](../logs/workspace-expression.md) identifier. For an app from Application Insights, use the [app](./app-expression.md) identifier. |
40 |
| - |
41 |
| -### [Workspace identifier](#tab/workspace-identifier) |
42 |
| - |
43 |
| -You can identify a workspace using one of these IDs: |
44 |
| - |
45 |
| -* **Workspace ID**: A workspace ID is the unique, immutable, identifier assigned to each workspace represented as a globally unique identifier (GUID). |
46 |
| - |
47 |
| - `workspace("00000000-0000-0000-0000-000000000000").Update | count` |
48 |
| - |
49 |
| -* **Azure Resource ID**: This ID is the Azure-defined unique identity of the workspace. For workspaces, the format is */subscriptions/subscriptionId/resourcegroups/resourceGroup/providers/microsoft.OperationalInsights/workspaces/workspaceName*. |
50 |
| - |
51 |
| - For example: |
52 |
| - |
53 |
| - ``` |
54 |
| - workspace("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/ContosoAzureHQ/providers/Microsoft.OperationalInsights/workspaces/contosoretail-it").Update | count |
55 |
| - ``` |
56 |
| -
|
57 |
| -### [App identifier](#tab/app-identifier) |
58 |
| -The following examples return a summarized count of requests made against an app named *fabrikamapp* in Application Insights. |
59 |
| -
|
60 |
| -You can identify an app using one of these IDs: |
61 |
| -
|
62 |
| -* **ID**: This ID is the app GUID of the application. |
63 |
| -
|
64 |
| - `app("00000000-0000-0000-0000-000000000000").requests | count` |
65 |
| -
|
66 |
| -* **Azure Resource ID**: This ID is the Azure-defined unique identity of the app. The format is */subscriptions/subscriptionId/resourcegroups/resourceGroup/providers/microsoft.OperationalInsights/components/componentName*. |
67 |
| -
|
68 |
| - For example: |
69 |
| -
|
70 |
| - ``` |
71 |
| - app("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Fabrikam/providers/microsoft.insights/components/fabrikamapp").requests | count |
72 |
| - ``` |
73 |
| -
|
74 |
| ---- |
| 37 | +* References to a cross resource, such as another workspace, should be explicit and can't be parameterized. |
75 | 38 |
|
76 |
| -## Query across Log Analytics workspaces and from Application Insights |
| 39 | +## Query across workspaces, applications, and resources using functions |
77 | 40 |
|
78 |
| -Follow the instructions in this section to query without using a function or by using a function. |
| 41 | +This section explains how to query workspaces, applications, and resources using functions with and without using a function. |
79 | 42 |
|
80 | 43 | ### Query without using a function
|
81 | 44 | You can query multiple resources from any of your resource instances. These resources can be workspaces and apps combined.
|
82 | 45 |
|
83 | 46 | Example for a query across three workspaces:
|
84 | 47 |
|
85 |
| -``` |
| 48 | +```kusto |
86 | 49 | union
|
87 | 50 | Update,
|
88 | 51 | workspace("00000000-0000-0000-0000-000000000001").Update,
|
@@ -123,6 +86,108 @@ applicationsScoping
|
123 | 86 | >[!NOTE]
|
124 | 87 | > This method can't be used with log alerts because the access validation of the alert rule resources, including workspaces and applications, is performed at alert creation time. Adding new resources to the function after the alert creation isn't supported. If you prefer to use a function for resource scoping in log alerts, you must edit the alert rule in the portal or with an Azure Resource Manager template to update the scoped resources. Alternatively, you can include the list of resources in the log alert query.
|
125 | 88 |
|
| 89 | +## Query across Log Analytics workspaces using workspace() |
| 90 | + |
| 91 | +Use the `workspace()` expression to retrieve data from a specific workspace in the same resource group, another resource group, or another subscription. You can use this expression to include log data in an Application Insights query and to query data across multiple workspaces in a log query. |
| 92 | + |
| 93 | +### Syntax |
| 94 | + |
| 95 | +`workspace(`*Identifier*`)` |
| 96 | + |
| 97 | +### Arguments |
| 98 | + |
| 99 | +`*Identifier*`: Identifies the workspace by using one of the formats in the following table. |
| 100 | + |
| 101 | +| Identifier | Description | Example |
| 102 | +|:---|:---|:---| |
| 103 | +| ID | GUID of the workspace | workspace("00000000-0000-0000-0000-000000000000") | |
| 104 | +| Azure Resource ID | Identifier for the Azure resource | workspace("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Contoso/providers/Microsoft.OperationalInsights/workspaces/contosoretail") | |
| 105 | + |
| 106 | +### Examples |
| 107 | + |
| 108 | +```Kusto |
| 109 | +workspace("00000000-0000-0000-0000-000000000000").Update | count |
| 110 | +``` |
| 111 | +```Kusto |
| 112 | +workspace("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Contoso/providers/Microsoft.OperationalInsights/workspaces/contosoretail").Event | count |
| 113 | +``` |
| 114 | +```Kusto |
| 115 | +union |
| 116 | +( workspace("00000000-0000-0000-0000-000000000000").Heartbeat | where Computer == "myComputer"), |
| 117 | +(app("00000000-0000-0000-0000-000000000000").requests | where cloud_RoleInstance == "myRoleInstance") |
| 118 | +| count |
| 119 | +``` |
| 120 | +```Kusto |
| 121 | +union |
| 122 | +(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")) |
| 123 | +``` |
| 124 | + |
| 125 | +## Query across classic Application Insights applications using app() |
| 126 | + |
| 127 | +Use the `app` expression to retrieve data from a specific classic Application Insights resource in the same resource group, another resource group, or another subscription. If you're using a [workspace-based Application Insights resource](../app/create-workspace-resource.md), telemetry is stored in a Log Analytics workspace with all other log data. Use the `workspace()` expression to query data from applications in multiple workspaces. You don't need a cross-workspace query to query data from multiple applications in the same workspace. |
| 128 | + |
| 129 | +### Syntax |
| 130 | + |
| 131 | +`app(`*Identifier*`)` |
| 132 | + |
| 133 | + |
| 134 | +### Arguments |
| 135 | + |
| 136 | +`*Identifier*`: Identifies the app using one of the formats in the table below. |
| 137 | + |
| 138 | +| Identifier | Description | Example |
| 139 | +|:---|:---|:---| |
| 140 | +| ID | GUID of the app | app("00000000-0000-0000-0000-000000000000") | |
| 141 | +| Azure Resource ID | Identifier for the Azure resource |app("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Fabrikam/providers/microsoft.insights/components/fabrikamapp") | |
| 142 | + |
| 143 | +### Examples |
| 144 | + |
| 145 | +```Kusto |
| 146 | +app("00000000-0000-0000-0000-000000000000").requests | count |
| 147 | +``` |
| 148 | +```Kusto |
| 149 | +app("/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/Fabrikam/providers/microsoft.insights/components/fabrikamapp").requests | count |
| 150 | +``` |
| 151 | +```Kusto |
| 152 | +union |
| 153 | +(workspace("00000000-0000-0000-0000-000000000000").Heartbeat | where Computer == "myComputer"), |
| 154 | +(app("00000000-0000-0000-0000-000000000000").requests | where cloud_RoleInstance == "myColumnInstance") |
| 155 | +| count |
| 156 | +``` |
| 157 | +```Kusto |
| 158 | +union |
| 159 | +(workspace("00000000-0000-0000-0000-000000000000").Heartbeat), (app("00000000-0000-0000-0000-000000000000").requests) |
| 160 | +| where TimeGenerated between(todatetime("2023-03-08 15:00:00") .. todatetime("2023-04-08 15:05:00")) |
| 161 | +``` |
| 162 | + |
| 163 | +## Correlate data between resources using resource() |
| 164 | + |
| 165 | +The `resource` expression is used in a Azure Monitor query [scoped to a resource](scope.md#query-scope) to retrieve data from other resources. |
| 166 | + |
| 167 | + |
| 168 | +### Syntax |
| 169 | + |
| 170 | +`resource(`*Identifier*`)` |
| 171 | + |
| 172 | +### Arguments |
| 173 | + |
| 174 | +`*Identifier*`: Identifies the resource, resource group, or subscription from which to correlate data. |
| 175 | + |
| 176 | +| Identifier | Description | Example |
| 177 | +|:---|:---|:---| |
| 178 | +| Resource | Includes data for the resource. | resource("/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcesgroups/myresourcegroup/providers/microsoft.compute/virtualmachines/myvm") | |
| 179 | +| Resource Group or Subscription | Includes data for the resource and all resources that it contains. | resource("/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcesgroups/myresourcegroup) | |
| 180 | + |
| 181 | + |
| 182 | +### Examples |
| 183 | + |
| 184 | +```Kusto |
| 185 | +union (Heartbeat),(resource("/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcesgroups/myresourcegroup/providers/microsoft.compute/virtualmachines/myvm").Heartbeat) | summarize count() by _ResourceId, TenantId |
| 186 | +``` |
| 187 | +```Kusto |
| 188 | +union (Heartbeat),(resource("/subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcesgroups/myresourcegroup).Heartbeat) | summarize count() by _ResourceId, TenantId |
| 189 | +``` |
| 190 | + |
126 | 191 | ## Next steps
|
127 | 192 |
|
128 | 193 | See [Analyze log data in Azure Monitor](./log-query-overview.md) for an overview of log queries and how Azure Monitor log data is structured.
|
0 commit comments