You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Optimizing log alert queries | Microsoft Docs
3
-
description: Recommendations for writing efficient alert queries
2
+
title: Optimize log alert queries | Microsoft Docs
3
+
description: This article gives recommendations for writing efficient alert queries.
4
4
author: yanivlavi
5
5
ms.author: yalavi
6
6
ms.topic: conceptual
7
7
ms.date: 2/23/2022
8
8
---
9
-
# Optimizing log alert queries
10
-
This article describes how to write and convert [Log Alert](./alerts-unified-log.md) queries to achieve optimal performance. Optimized queries reduce latency and load of alerts, which run frequently.
9
+
# Optimize log alert queries
11
10
12
-
## How to start writing an alert log query
11
+
This article describes how to write and convert [log alert](./alerts-unified-log.md) queries to achieve optimal performance. Optimized queries reduce latency and load of alerts, which run frequently.
13
12
14
-
Alert queries start from [querying the log data in Log Analytics](alerts-log.md#create-a-new-log-alert-rule-in-the-azure-portal) that indicates the issue. You can use the [alert query examples topic](../logs/queries.md) to understand what you can discover. You may also [get started on writing your own query](../logs/log-analytics-tutorial.md).
13
+
## Start writing an alert log query
14
+
15
+
Alert queries start from [querying the log data in Log Analytics](alerts-log.md#create-a-new-log-alert-rule-in-the-azure-portal) that indicates the issue. To understand what you can discover, see [Using queries in Azure Monitor Log Analytics](../logs/queries.md). You can also [get started on writing your own query](../logs/log-analytics-tutorial.md).
15
16
16
17
### Queries that indicate the issue and not the alert
17
18
18
-
The alert flow was built to transform the results that indicate the issue to an alert. For example, in a case of a query like:
19
+
The alert flow was built to transform the results that indicate the issue to an alert. For example, in the case of a query like:
19
20
20
21
```Kusto
21
22
SecurityEvent
22
23
| where EventID == 4624
23
24
```
24
25
25
-
If the intent of the user is to alert, when this event type happens, the alerting logic appends `count` to the query. The query that will run will be:
26
+
If the intent of the user is to alert, when this event type happens, the alerting logic appends `count` to the query. The query that runs will be:
26
27
27
28
```Kusto
28
29
SecurityEvent
29
30
| where EventID == 4624
30
31
| count
31
32
```
32
33
33
-
There's no need to add alerting logic to the query and doing that may even cause issues. In the above example, if you include `count` in your query, it will always result in the value 1, since the alert service will do `count` of `count`.
34
+
There's no need to add alerting logic to the query, and doing that might even cause issues. In the preceding example, if you include `count` in your query, it will always result in the value 1, because the alert service will do `count` of `count`.
34
35
35
-
### Avoid `limit` and `take` operators
36
+
### Avoid limit and take operators
36
37
37
-
Using `limit` and `take` in queries can increase latency and load of alerts as the results aren't consistent over time. It's preferred you use it only if needed.
38
+
Using `limit` and `take` in queries can increase latency and load of alerts because the results aren't consistent over time. Use them only if needed.
38
39
39
40
## Log query constraints
41
+
40
42
[Log queries in Azure Monitor](../logs/log-query-overview.md) start with either a table, [`search`](/azure/kusto/query/searchoperator), or [`union`](/azure/kusto/query/unionoperator) operator.
41
43
42
-
Queries for log alert rules should always start with a table to define a clear scope, which improves both query performance and the relevance of the results. Queries in alert rules run frequently, so using `search` and `union` can result in excessive overhead adding latency to the alert, as it requires scanning across multiple tables. These operators also reduce the ability of the alerting service to optimize the query.
44
+
Queries for log alert rules should always start with a table to define a clear scope, which improves query performance and the relevance of the results. Queries in alert rules run frequently. Using `search` and `union` can result in excessive overhead that adds latency to the alert because it requires scanning across multiple tables. These operators also reduce the ability of the alerting service to optimize the query.
43
45
44
46
We don't support creating or modifying log alert rules that use `search` or `union` operators, except for cross-resource queries.
45
47
46
-
For example, the following alerting query is scoped to the _SecurityEvent_ table and searches for specific event ID. It's the only table that the query must process.
48
+
For example, the following alerting query is scoped to the _SecurityEvent_ table and searches for a specific event ID. It's the only table that the query must process.
47
49
48
50
```Kusto
49
51
SecurityEvent
50
52
| where EventID == 4624
51
53
```
52
54
53
-
Log alert rules using [cross-resource queries](../logs/cross-workspace-query.md)are not affected by this change since cross-resource queries use a type of `union`, which limits the query scope to specific resources. The following example would be valid log alert query:
55
+
Log alert rules using [cross-resource queries](../logs/cross-workspace-query.md)aren't affected by this change because cross-resource queries use a type of `union`, which limits the query scope to specific resources. The following example would be a valid log alert query:
> [Cross-resource queries](../logs/cross-workspace-query.md) are supported in the new [scheduledQueryRules API](/rest/api/monitor/scheduledqueryrule-2021-08-01/scheduled-query-rules). If you still use the [legacy Log Analytics Alert API](./api-alerts.md) for creating log alerts, you can learn about switching [here](../alerts/alerts-log-api-switch.md).
65
+
> [Cross-resource queries](../logs/cross-workspace-query.md) are supported in the new [scheduledQueryRules API](/rest/api/monitor/scheduledqueryrule-2021-08-01/scheduled-query-rules). If you still use the [legacy Log Analytics Alert API](./api-alerts.md) for creating log alerts, see [Upgrade legacy rules management to the current Azure Monitor Log Alerts API](../alerts/alerts-log-api-switch.md) to learn about switching.
64
66
65
67
## Examples
66
-
The following examples include log queries that use `search` and `union` and provide steps you can use to modify these queries for use in alert rules.
68
+
69
+
The following examples include log queries that use `search` and `union`. They provide steps you can use to modify these queries for use in alert rules.
67
70
68
71
### Example 1
69
-
You want to create a log alert rule using the following query that retrieves performance information using `search`:
72
+
73
+
You want to create a log alert rule by using the following query that retrieves performance information using `search`:
70
74
71
75
```Kusto
72
76
search *
73
77
| where Type == 'Perf' and CounterName == '% Free Space'
74
78
| where CounterValue < 30
75
79
```
76
80
77
-
To modify this query, start by using the following query to identify the table that the properties belong to:
78
-
79
-
```Kusto
80
-
search *
81
-
| where CounterName == '% Free Space'
82
-
| summarize by $table
83
-
```
81
+
1.To modify this query, start by using the following query to identify the table that the properties belong to:
82
+
83
+
```Kusto
84
+
search *
85
+
| where CounterName == '% Free Space'
86
+
| summarize by $table
87
+
```
84
88
85
-
The result of this query would show that the _CounterName_ property came from the _Perf_ table.
89
+
The result of this query would show that the _CounterName_ property came from the _Perf_ table.
86
90
87
-
You can use this result to create the following query that you would use for the alert rule:
91
+
1. Use this result to create the following query that you would use for the alert rule:
88
92
89
-
```Kusto
90
-
Perf
91
-
| where CounterName == '% Free Space'
92
-
| where CounterValue < 30
93
-
```
93
+
``` Kusto
94
+
Perf
95
+
| where CounterName == '% Free Space'
96
+
| where CounterValue < 30
97
+
```
94
98
95
99
### Example 2
96
-
You want to create a log alert rule using the following query that retrieves performance information using `search`:
100
+
101
+
You want to create a log alert rule by using the following query that retrieves performance information using `search`:
97
102
98
103
``` Kusto
99
104
search ObjectName =="Memory" and CounterName=="% Committed Bytes In Use"
100
105
| summarize Avg_Memory_Usage =avg(CounterValue) by Computer
101
106
| where Avg_Memory_Usage between(90 .. 95)
102
107
```
103
108
104
-
To modify this query, start by using the following query to identify the table that the properties belong to:
109
+
1. To modify this query, start by using the following query to identify the table that the properties belong to:
110
+
111
+
```Kusto
112
+
search ObjectName=="Memory" and CounterName=="% Committed Bytes In Use"
113
+
| summarize by $table
114
+
```
115
+
116
+
The result of this query would show that the _ObjectName_ and _CounterName_ properties came from the _Perf_ table.
105
117
106
-
```Kusto
107
-
search ObjectName=="Memory" and CounterName=="% Committed Bytes In Use"
108
-
| summarize by $table
109
-
```
110
-
111
-
The result of this query would show that the _ObjectName_ and _CounterName_ property came from the _Perf_ table.
118
+
1. Use this result to create the following query that you would use for the alert rule:
112
119
113
-
You can use this result to create the following query that you would use for the alert rule:
114
-
115
-
```Kusto
116
-
Perf
117
-
| where ObjectName =="Memory" and CounterName=="% Committed Bytes In Use"
118
-
| summarize Avg_Memory_Usage=avg(CounterValue) by Computer
119
-
| where Avg_Memory_Usage between(90 .. 95)
120
-
```
120
+
``` Kusto
121
+
Perf
122
+
| where ObjectName =="Memory" and CounterName=="% Committed Bytes In Use"
123
+
| summarize Avg_Memory_Usage=avg(CounterValue) by Computer
124
+
| where Avg_Memory_Usage between(90 .. 95)
125
+
```
121
126
122
127
### Example 3
123
128
124
-
You want to create a log alert rule using the following query that uses both `search` and `union` to retrieve performance information:
129
+
You want to create a log alert rule by using the following query that uses both `search` and `union` to retrieve performance information:
125
130
126
131
``` Kusto
127
132
search (ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceName == "_Total")
@@ -132,39 +137,40 @@ search (ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceN
132
137
| summarize Avg_Idle_Time = avg(CounterValue) by Computer
133
138
```
134
139
135
-
To modify this query, start by using the following query to identify the table that the properties in the first part of the query belong to:
140
+
1.To modify this query, start by using the following query to identify the table that the properties in the first part of the query belong to:
136
141
137
-
```Kusto
138
-
search (ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceName == "_Total")
139
-
| summarize by $table
140
-
```
142
+
```Kusto
143
+
search (ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceName == "_Total")
144
+
| summarize by $table
145
+
```
141
146
142
-
The result of this query would show that all these properties came from the _Perf_ table.
147
+
The result of this query would show that all these properties came from the _Perf_ table.
143
148
144
-
Now use`union` with `withsource` command to identify which source table has contributed each row.
149
+
1. Use `union` with the `withsource` command to identify which source table has contributed each row:
145
150
146
-
```Kusto
147
-
union withsource=table *
148
-
| where CounterName == "% Processor Utility"
149
-
| summarize by table
150
-
```
151
+
``` Kusto
152
+
union withsource=table *
153
+
| where CounterName == "% Processor Utility"
154
+
| summarize by table
155
+
```
151
156
152
-
The result of this query would show that these properties also came from the _Perf_ table.
157
+
The result of this query would show that these properties also came from the _Perf_ table.
153
158
154
-
You can use these results to create the following query that you would use for the alert rule:
159
+
1. Use these results to create the following query that you would use for the alert rule:
155
160
156
-
```Kusto
157
-
Perf
158
-
| where ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceName == "_Total"
159
-
| where Computer !in (
160
-
(Perf
161
-
| where CounterName == "% Processor Utility"
162
-
| summarize by Computer))
163
-
| summarize Avg_Idle_Time = avg(CounterValue) by Computer
164
-
```
161
+
``` Kusto
162
+
Perf
163
+
| where ObjectName == "Processor" and CounterName == "% Idle Time" and InstanceName == "_Total"
164
+
| where Computer !in (
165
+
(Perf
166
+
| where CounterName == "% Processor Utility"
167
+
| summarize by Computer))
168
+
| summarize Avg_Idle_Time = avg(CounterValue) by Computer
169
+
```
165
170
166
171
### Example 4
167
-
You want to create a log alert rule using the following query that joins the results of two `search` queries:
172
+
173
+
You want to create a log alert rule by using the following query that joins the results of two `search` queries:
168
174
169
175
```Kusto
170
176
search Type == 'SecurityEvent' and EventID == '4625'
@@ -176,38 +182,39 @@ search Type == 'SecurityEvent' and EventID == '4625'
176
182
) on Hour
177
183
```
178
184
179
-
To modify the query, start by using the following query to identify the table that contains the properties in the left side of the join:
180
-
181
-
```Kusto
182
-
search Type == 'SecurityEvent' and EventID == '4625'
183
-
| summarize by $table
184
-
```
185
+
1.To modify the query, start by using the following query to identify the table that contains the properties in the left side of the join:
186
+
187
+
```Kusto
188
+
search Type == 'SecurityEvent' and EventID == '4625'
189
+
| summarize by $table
190
+
```
185
191
186
-
The result indicates that the properties in the left side of the join belong to _SecurityEvent_ table.
192
+
The result indicates that the properties in the left side of the join belong to the _SecurityEvent_ table.
187
193
188
-
Now use the following query to identify the table that contains the properties in the right side of the join:
194
+
1. Use the following query to identify the table that contains the properties in the right side of the join:
189
195
190
-
```Kusto
191
-
search in (Heartbeat) OSType == 'Windows'
192
-
| summarize by $table
193
-
```
196
+
``` Kusto
197
+
search in (Heartbeat) OSType == 'Windows'
198
+
| summarize by $table
199
+
```
194
200
195
-
The result indicates that the properties in the right side of the join belong to _Heartbeat_ table.
196
-
197
-
You can use these results to create the following query that you would use for the alert rule:
198
-
199
-
```Kusto
200
-
SecurityEvent
201
-
| where EventID == '4625'
202
-
| summarize by Computer, Hour = bin(TimeGenerated, 1h)
0 commit comments