|
3 | 3 | The Azure Monitor Query client library is used to execute read-only queries against [Azure Monitor][azure_monitor_overview]'s two data platforms:
|
4 | 4 |
|
5 | 5 | - [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. |
7 | 7 |
|
8 | 8 | **Resources:**
|
9 | 9 |
|
@@ -94,6 +94,8 @@ Each set of metric values is a time series with the following characteristics:
|
94 | 94 | - [Advanced logs query scenarios](#advanced-logs-query-scenarios)
|
95 | 95 | - [Set logs query timeout](#set-logs-query-timeout)
|
96 | 96 | - [Query multiple workspaces](#query-multiple-workspaces)
|
| 97 | + - [Include statistics](#include-statistics) |
| 98 | + - [Include visualization](#include-visualization) |
97 | 99 | - [Metrics query](#metrics-query)
|
98 | 100 | - [Handle metrics query response](#handle-metrics-query-response)
|
99 | 101 | - [Example of handling response](#example-of-handling-response)
|
@@ -289,12 +291,98 @@ For example, the following query executes in three workspaces:
|
289 | 291 | client.query_workspace(
|
290 | 292 | <workspace_id>,
|
291 | 293 | query,
|
| 294 | + timespan=timedelta(days=1), |
292 | 295 | additional_workspaces=['<workspace 2>', '<workspace 3>']
|
293 | 296 | )
|
294 | 297 | ```
|
295 | 298 |
|
296 | 299 | 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).
|
297 | 300 |
|
| 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 | + |
298 | 386 | ### Metrics query
|
299 | 387 |
|
300 | 388 | The following example gets metrics for an Event Grid subscription. The resource URI is that of an Event Grid topic.
|
|
0 commit comments