Skip to content

Commit 0063fb2

Browse files
authored
[Monitor][Query] Add docs about stats and vis (Azure#27263)
Two sections in the README were added to demonstrate how users can retreive statistics and visualization data when performing log queries. Closes: Azure#23311 Signed-off-by: Paul Van Eck <[email protected]>
1 parent 0642a94 commit 0063fb2

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

sdk/monitor/azure-monitor-query/README.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The Azure Monitor Query client library is used to execute read-only queries against [Azure Monitor][azure_monitor_overview]'s two data platforms:
44

55
- [Logs](https://learn.microsoft.com/azure/azure-monitor/logs/data-platform-logs) - Collects and organizes log and performance data from monitored resources. Data from different sources such as platform logs from Azure services, log and performance data from virtual machines agents, and usage and performance data from apps can be consolidated into a single [Azure Log Analytics workspace](https://learn.microsoft.com/azure/azure-monitor/logs/data-platform-logs#log-analytics-and-workspaces). The various data types can be analyzed together using the [Kusto Query Language][kusto_query_language].
6-
- [Metrics](https://learn.microsoft.com/azure/azure-monitor/essentials/data-platform-metrics) - Collects numeric data from monitored resources into a time series database. Metrics are numerical values that are collected at regular intervals and describe some aspect of a system at a particular time. Metrics are lightweight and capable of supporting near real-time scenarios, making them particularly useful for alerting and fast detection of issues.
6+
- [Metrics](https://learn.microsoft.com/azure/azure-monitor/essentials/data-platform-metrics) - Collects numeric data from monitored resources into a time series database. Metrics are numerical values that are collected at regular intervals and describe some aspect of a system at a particular time. Metrics are lightweight and capable of supporting near real-time scenarios, making them useful for alerting and fast detection of issues.
77

88
**Resources:**
99

@@ -94,6 +94,8 @@ Each set of metric values is a time series with the following characteristics:
9494
- [Advanced logs query scenarios](#advanced-logs-query-scenarios)
9595
- [Set logs query timeout](#set-logs-query-timeout)
9696
- [Query multiple workspaces](#query-multiple-workspaces)
97+
- [Include statistics](#include-statistics)
98+
- [Include visualization](#include-visualization)
9799
- [Metrics query](#metrics-query)
98100
- [Handle metrics query response](#handle-metrics-query-response)
99101
- [Example of handling response](#example-of-handling-response)
@@ -289,12 +291,98 @@ For example, the following query executes in three workspaces:
289291
client.query_workspace(
290292
<workspace_id>,
291293
query,
294+
timespan=timedelta(days=1),
292295
additional_workspaces=['<workspace 2>', '<workspace 3>']
293296
)
294297
```
295298

296299
A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py).
297300

301+
#### Include statistics
302+
303+
To get logs query execution statistics, such as CPU and memory consumption:
304+
305+
1. Set the `include_statistics` parameter to `True`.
306+
2. Access the `statistics` field inside the `LogsQueryResult` object.
307+
308+
The following example prints the query execution time:
309+
310+
```python
311+
query = "AzureActivity | top 10 by TimeGenerated"
312+
result = client.query_workspace(
313+
<workspace_id>,
314+
query,
315+
timespan=timedelta(days=1),
316+
include_statistics=True
317+
)
318+
319+
execution_time = result.statistics.get("query", {}).get("executionTime")
320+
print(f"Query execution time: {execution_time}")
321+
```
322+
323+
The `statistics` field is a `dict` that corresponds to the raw JSON response, and its structure can vary by query. The statistics are found within the `query` property. For example:
324+
325+
```python
326+
{
327+
"query": {
328+
"executionTime": 0.0156478,
329+
"resourceUsage": {...},
330+
"inputDatasetStatistics": {...},
331+
"datasetStatistics": [{...}]
332+
}
333+
}
334+
```
335+
#### Include visualization
336+
337+
To get visualization data for logs queries using the [render operator](https://docs.microsoft.com/azure/data-explorer/kusto/query/renderoperator?pivots=azuremonitor):
338+
339+
1. Set the `include_visualization` property to `True`.
340+
1. Access the `visualization` field inside the `LogsQueryResult` object.
341+
342+
For example:
343+
344+
```python
345+
query = (
346+
"StormEvents"
347+
"| summarize event_count = count() by State"
348+
"| where event_count > 10"
349+
"| project State, event_count"
350+
"| render columnchart"
351+
)
352+
result = client.query_workspace(
353+
<workspace_id>,
354+
query,
355+
timespan=timedelta(days=1),
356+
include_visualization=True
357+
)
358+
359+
print(f"Visualization result: {result.visualization}")
360+
```
361+
362+
The `visualization` field is a `dict` that corresponds to the raw JSON response, and its structure can vary by query. For example:
363+
364+
```python
365+
{
366+
"visualization": "columnchart",
367+
"title": "the chart title",
368+
"accumulate": False,
369+
"isQuerySorted": False,
370+
"kind": None,
371+
"legend": None,
372+
"series": None,
373+
"yMin": "NaN",
374+
"yMax": "NaN",
375+
"xAxis": None,
376+
"xColumn": None,
377+
"xTitle": "x axis title",
378+
"yAxis": None,
379+
"yColumns": None,
380+
"ySplit": None,
381+
"yTitle": None,
382+
"anomalyColumns": None
383+
}
384+
```
385+
298386
### Metrics query
299387

300388
The following example gets metrics for an Event Grid subscription. The resource URI is that of an Event Grid topic.

0 commit comments

Comments
 (0)