Skip to content

Commit b4c90d0

Browse files
authored
Merge pull request #58088 from bwren/cross-resource
Azure Monitor cross resource queries
2 parents 40cfb2b + 4352e4b commit b4c90d0

File tree

5 files changed

+113
-25
lines changed

5 files changed

+113
-25
lines changed

articles/azure-monitor/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@
691691
href: ../log-analytics/query-language/advanced-query-writing.md?toc=/azure/azure-monitor/toc.json
692692
- name: Charts and diagrams
693693
href: ../log-analytics/query-language/charts.md?toc=/azure/azure-monitor/toc.json
694+
- name: Functions
695+
href: ../log-analytics/query-language/functions.md?toc=/azure/azure-monitor/toc.json
694696
- name: Cheatsheets
695697
items:
696698
- name: SQL

articles/log-analytics/log-analytics-cross-workspace-search.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.workload: na
1212
ms.tgt_pltfrm: na
1313
ms.devlang: na
1414
ms.topic: conceptual
15-
ms.date: 04/17/2018
15+
ms.date: 11/15/2018
1616
ms.author: magoedte
1717
ms.component:
1818
---
@@ -86,7 +86,7 @@ Identifying an application in Application Insights can be accomplished with the
8686
```
8787
8888
### Performing a query across multiple resources
89-
You can query multiple resorces from any of your resource instances, these can be workspaces and apps combined.
89+
You can query multiple resources from any of your resource instances, these can be workspaces and apps combined.
9090
9191
Example for query across two workspaces:
9292
@@ -97,6 +97,36 @@ union Update, workspace("contosoretail-it").Update, workspace("b459b4u5-912x-46d
9797
| summarize dcount(Computer) by Classification
9898
```
9999
100+
## Using cross-resource query for multiple resources
101+
When using cross-resource queries to correlate data from multiple Log Analytics and Application Insights resources, the query can become complex and difficult to maintain. You should leverage [functions in Log Analytics](query-language/functions.md) to separate the query logic from the scoping of the query resources, which simplifies the query structure. The following example demonstrates how you can monitor multiple Application Insights resources and visualize the count of failed requests by application name.
102+
103+
Create a query like the following that references the scope of Application Insights resources. The `withsource= SourceApp` command adds a column that designates the application name that sent the log. [Save the query as function](query-language/functions.md#create-a-function) with the alias _applicationsScoping_.
104+
105+
```Kusto
106+
// crossResource function that scopes my Application Insights resources
107+
union withsource= SourceApp
108+
app('Contoso-app1').requests,
109+
app('Contoso-app2').requests,
110+
app('Contoso-app3').requests,
111+
app('Contoso-app4').requests,
112+
app('Contoso-app5').requests
113+
```
114+
115+
116+
117+
You can now [use this function](query-language/functions.md#use-a-function) in a cross-resource query like the following. The function alias _applicationsScoping_ returns the union of the requests table from all the defined applications. The query then filters for failed requests and visualizes the trends by application. The _parse_ operator is optional in this example. It extracts the application name from _SourceApp_ property.
118+
119+
```Kusto
120+
applicationsScoping
121+
| where timestamp > ago(12h)
122+
| where success == 'False'
123+
| parse SourceApp with * '(' applicationName ')' *
124+
| summarize count() by applicationName, bin(timestamp, 1h)
125+
| sort by count_ desc
126+
| render timechart
127+
```
128+
![Timechart](media/log-analytics-cross-workspace-search/chart.png)
129+
100130
## Next steps
101131

102132
Review the [Log Analytics log search reference](https://docs.microsoft.com/azure/log-analytics/query-language/kusto) to view all of the query syntax options available in Log Analytics.
40.7 KB
Loading

articles/log-analytics/query-language/advanced-query-writing.md

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ms.workload: na
1212
ms.tgt_pltfrm: na
1313
ms.devlang: na
1414
ms.topic: conceptual
15-
ms.date: 08/16/2018
15+
ms.date: 11/15/2018
1616
ms.author: bwren
1717
ms.component: na
1818
---
@@ -73,28 +73,6 @@ Event
7373
| project TimeGenerated, USTimeGenerated, Source, Computer, EventLevel, EventData
7474
```
7575

76-
## Functions
77-
You can save a query with a function alias so it can be referenced by other queries. For example, the following standard query returns all missing security updates reported in the last day:
78-
79-
```Kusto
80-
Update
81-
| where TimeGenerated > ago(1d)
82-
| where Classification == "Security Updates"
83-
| where UpdateState == "Needed"
84-
```
85-
86-
You can save this query as a function and give it an alias such as _security_updates_last_day_. Then you can use it in another query to search for SQL-related needed security updates:
87-
88-
```Kusto
89-
security_updates_last_day | where Title contains "SQL"
90-
```
91-
92-
To save a query as a function, select the **Save** button in the portal and change **Save as** to _Function_. The function alias can contain letters, digits, or underscores but must start with a letter or an underscore.
93-
94-
> [!NOTE]
95-
> Saving a function is possible in Log Analytics queries, but currently not for Application Insights queries.
96-
97-
9876
## Print
9977
`print` will return a table with a single column and a single row, showing the result of a calculation. This is often used in cases where you need a simple calcuation. For example, to find the current time in PST and add a column with EST:
10078

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: Functions Azure Log Analytics | Microsoft Docs
3+
description: This article describes how to use functions to call a query from another query in Log Analytics.
4+
services: log-analytics
5+
documentationcenter: ''
6+
author: bwren
7+
manager: carmonm
8+
editor: ''
9+
ms.assetid:
10+
ms.service: log-analytics
11+
ms.workload: na
12+
ms.tgt_pltfrm: na
13+
ms.devlang: na
14+
ms.topic: conceptual
15+
ms.date: 11/15/2018
16+
ms.author: bwren
17+
ms.component: na
18+
---
19+
20+
21+
# Using functions in Azure Monitor Log Analytics
22+
23+
> [!NOTE]
24+
> You should complete [Get started with the Analytics portal](get-started-analytics-portal.md) and [Getting started with queries](get-started-queries.md) before completing this lesson.
25+
26+
[!INCLUDE [log-analytics-demo-environment](../../../includes/log-analytics-demo-environment.md)]
27+
28+
29+
To use a Log Analytics query with another query you can save it as a function. This allows you to simplify complex queries by breaking them into parts and allows you to reuse common code with multiple queries.
30+
31+
## Create a function
32+
33+
Create a function in the Azure portal by clicking **Save** and then providing the information in the following table.
34+
35+
| Setting | Description |
36+
|:---|:---|
37+
| Name | Display name for the query in **Query explorer**. |
38+
| Save as | Function |
39+
| Function Alias | Short name to use the function in other queries. May not contain spaces and must be unique. |
40+
| Category | A category to organize saved queries and functions in **Query explorer**. |
41+
42+
> [!NOTE]
43+
> A function in Log Analytics cannot contain another function.
44+
45+
> [!NOTE]
46+
> Saving a function is possible in Log Analytics queries, but currently not for Application Insights queries.
47+
48+
49+
50+
## Use a function
51+
Use a function by including its alias in another query. It can be used like any other table.
52+
53+
## Example
54+
The following sample query returns all missing security updates reported in the last day. Save this query as a function with the alias _security_updates_last_day_.
55+
56+
```Kusto
57+
Update
58+
| where TimeGenerated > ago(1d)
59+
| where Classification == "Security Updates"
60+
| where UpdateState == "Needed"
61+
```
62+
63+
Create another to search for SQL-related needed security updates.
64+
65+
```Kusto
66+
security_updates_last_day | where Title contains "SQL"
67+
```
68+
69+
## Next steps
70+
See other lessons for using the Log Analytics query language:
71+
72+
- [String operations](string-operations.md)
73+
- [Date and time operations](datetime-operations.md)
74+
- [Aggregation functions](aggregations.md)
75+
- [Advanced aggregations](advanced-aggregations.md)
76+
- [JSON and data structures](json-data-structures.md)
77+
- [Joins](joins.md)
78+
- [Charts](charts.md)

0 commit comments

Comments
 (0)