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
Azure Monitor helps you maximize the availability and performance of your apps. It delivers a comprehensive solution for collecting, analyzing, and acting on telemetry from your cloud and on-premises environments.
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
-
All data collected by Azure Monitor fits into one of two fundamental types:
5
+
-[Logs](https://docs.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://docs.microsoft.com/azure/azure-monitor/logs/data-platform-logs#log-analytics-workspaces). The various data types can be analyzed together using the [Kusto Query Language][kusto_query_language].
6
+
-[Metrics](https://docs.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
7
7
-
-**Metrics** - Numerical values that describe some aspect of a system at a particular time. They're lightweight and can support near real-time scenarios.
8
-
-**Logs** - Disparate types of data organized into records with different sets of properties for each type. Performance data and telemetry such as events, exceptions, and traces are stored as logs.
8
+
**Resources:**
9
9
10
-
To programmatically analyze these data sources, the Azure Monitor Query client library can be used.
- To query Logs, you need an [Azure Log Analytics workspace][azure_monitor_create_using_portal].
24
+
- To query Metrics, you need an Azure resource of any kind (Storage Account, Key Vault, Cosmos DB, etc.).
20
25
21
26
### Install the package
22
27
23
28
Install the Azure Monitor Query client library for Python with [pip][pip]:
24
29
25
30
```bash
26
-
pip install azure-monitor-query --pre
31
+
pip install azure-monitor-query
27
32
```
28
33
29
34
### Create the client
30
35
31
-
To interact with the Azure Monitor service, create an instance of a token credential. Use that instance when creating a `LogsQueryClient` or `MetricsQueryClient`.
36
+
An authenticated client is required to query Logs or Metrics. The library includes both synchronous and asynchronous forms of the clients. To authenticate, create an instance of a token credential. Use that instance when creating a `LogsQueryClient` or `MetricsQueryClient`. The following examples use `DefaultAzureCredential` from the [azure-identity](https://pypi.org/project/azure-identity/) package.
32
37
33
38
#### Synchronous clients
34
39
35
-
Consider the following example, which creates synchronous clients for both logs and metrics querying:
40
+
Consider the following example, which creates synchronous clients for both Logs and Metrics querying:
Azure Monitor Logs collects and organizes log and performance data from monitored resources. Data from different sources can be consolidated into a single workspace. Examples of data sources include:
64
-
65
-
- Platform logs from Azure services.
66
-
- Log and performance data from virtual machine agents.
67
-
- Usage and performance data from apps.
64
+
### Execute the query
68
65
69
-
#### Azure Log Analytics workspaces
66
+
For examples of Logs and Metrics queries, see the [Examples](#examples) section.
70
67
71
-
Data collected by Azure Monitor Logs is stored in one or more [Log Analytics workspaces](https://docs.microsoft.com/azure/azure-monitor/logs/data-platform-logs#log-analytics-workspaces). The workspace defines the:
72
-
73
-
- Geographic location of the data.
74
-
- Access rights defining which users can access data.
75
-
- Configuration settings, such as the pricing tier and data retention.
76
-
77
-
#### Log queries
68
+
## Key concepts
78
69
79
-
Data from the disparate sources can be analyzed together using [Kusto Query Language (KQL)](https://docs.microsoft.com/azure/data-explorer/kusto/query/)—the same query language used by [Azure Data Explorer](https://docs.microsoft.com/azure/data-explorer/data-explorer-overview). Data is retrieved from a Log Analytics workspace using a KQL query—a read-only request to process data and return results. For more information, see [Log queries in Azure Monitor](https://docs.microsoft.com/azure/azure-monitor/logs/log-query-overview).
70
+
### Logs query rate limits and throttling
80
71
81
-
### Metrics
72
+
Each Azure Active Directory user is able to make up to 200 requests per 30 seconds, with no cap on the total calls per day. If requests are made at a rate higher than this, these requests will receive HTTP status code 429 (Too Many Requests) along with the `Retry-After: <delta-seconds>` header. The header indicates the number of seconds until requests to this app are likely to be accepted.
82
73
83
-
Azure Monitor Metrics collects numeric data from monitored resources into a time series database. Metrics are collected at regular intervals and describe some aspect of a system at a particular time. Metrics in Azure Monitor are lightweight and can support near real-time scenarios. They're useful for alerting and fast detection of issues. Metrics can be:
74
+
In addition to call rate limits and daily quota caps, there are limits on queries themselves. Queries cannot:
84
75
85
-
-Analyzed interactively with [Metrics Explorer](https://docs.microsoft.com/azure/azure-monitor/essentials/metrics-getting-started).
86
-
-Used to receive notifications with an alert when a value crosses a threshold.
87
-
-Visualized in a workbook or dashboard.
76
+
-Return more than 500,000 rows.
77
+
-Return more than 64,000,000 bytes (~61 MiB total data).
78
+
-Run longer than 10 minutes by default. See this for details.
88
79
89
-
####Metrics data structure
80
+
### Metrics data structure
90
81
91
82
Each set of metric values is a time series with the following characteristics:
92
83
@@ -99,19 +90,20 @@ Each set of metric values is a time series with the following characteristics:
-[Example of handling response](#example-of-handling-response)
111
103
112
-
### Single logs query
104
+
### Logs query
113
105
114
-
This example shows getting a log query. To handle the response and view it in a tabular form, the [pandas](https://pypi.org/project/pandas/) library is used. See the [samples][python-query-samples] if you choose not to use pandas.
106
+
This example shows getting a logs query. To handle the response and view it in a tabular form, the [pandas](https://pypi.org/project/pandas/) library is used. See the [samples][samples] if you choose not to use pandas.
115
107
116
108
#### Specify timespan
117
109
@@ -150,29 +142,45 @@ for table in response.tables:
150
142
print(df)
151
143
```
152
144
153
-
#### Set logs query timeout
145
+
#### Handle logs query response
154
146
155
-
The following example shows setting a server timeout in seconds. A gateway timeout is raised if the query takes more time than the mentioned timeout. The default is 180 seconds and can be set up to 10 minutes (600 seconds).
147
+
The `query` API returns the `LogsQueryResult` while the `batch_query` API returns list of `LogsQueryResult`. Here's a hierarchy of the response:
148
+
149
+
```
150
+
LogsQueryResult
151
+
|---statistics
152
+
|---visualization
153
+
|---error
154
+
|---tables (list of `LogsTable` objects)
155
+
|---name
156
+
|---rows
157
+
|---columns (list of `LogsTableColumn` objects)
158
+
|---name
159
+
|---type
160
+
```
161
+
162
+
For example, to handle a logs query response with tables and display it using pandas:
156
163
157
164
```python
158
-
import os
159
-
import pandas as pd
160
-
from azure.monitor.query import LogsQueryClient
161
-
from azure.identity import DefaultAzureCredential
165
+
table = response.tables[0]
166
+
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
167
+
```
162
168
163
-
credential = DefaultAzureCredential()
164
-
client = LogsQueryClient(credential)
169
+
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_client.py).
165
170
166
-
response = client.query(
167
-
os.environ['LOG_WORKSPACE_ID'],
168
-
"range x from 1 to 10000000000 step 1 | count",
169
-
server_timeout=1,
170
-
)
171
+
In a similar fashion, to handle a batch logs query response:
172
+
173
+
```python
174
+
for result in response:
175
+
table = result.tables[0]
176
+
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
171
177
```
172
178
179
+
A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py).
180
+
173
181
### Batch logs query
174
182
175
-
The following example demonstrates sending multiple queries at the same time using batch query API. The queries can either be represented as a list of `LogQueryRequest` objects or a dictionary. This example uses the former approach.
183
+
The following example demonstrates sending multiple queries at the same time using batch query API. The queries can either be represented as a list of `LogsBatchQuery` objects or a dictionary. This example uses the former approach.
The `query` API returns the `LogsQueryResult` while the `batch_query` API returns list of `LogsQueryResult`.
226
+
#### Set logs query timeout
219
227
220
-
Here is a heirarchy of the response:
228
+
The following example shows setting a server timeout in seconds. A gateway timeout is raised if the query takes more time than the mentioned timeout. The default is 180 seconds and can be set up to 10 minutes (600 seconds).
221
229
222
-
```
223
-
LogsQueryResult
224
-
|---statistics
225
-
|---visualization
226
-
|---error
227
-
|---tables (list of `LogsTable` objects)
228
-
|---name
229
-
|---rows
230
-
|---columns (list of `LogsTableColumn` objects)
231
-
|---name
232
-
|---type
233
-
```
230
+
```python
231
+
import os
232
+
import pandas as pd
233
+
from azure.monitor.query import LogsQueryClient
234
+
from azure.identity import DefaultAzureCredential
234
235
235
-
So, to handle a response with tables and display it using pandas,
236
+
credential = DefaultAzureCredential()
237
+
client = LogsQueryClient(credential)
236
238
237
-
```python
238
-
table = response.tables[0]
239
-
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
239
+
response = client.query(
240
+
os.environ['LOG_WORKSPACE_ID'],
241
+
"range x from 1 to 10000000000 step 1 | count",
242
+
server_timeout=1,
243
+
)
240
244
```
241
-
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_client.py).
242
245
243
-
In a very similar fashion, to handle a batch response,
246
+
#### Query multiple workspaces
247
+
248
+
The same logs query can be executed across multiple Log Analytics workspaces. In addition to the Kusto query, the following parameters are required:
249
+
250
+
-`workspace_id` - The first (primary) workspace ID.
251
+
-`additional_workspaces` - A list of workspaces, excluding the workspace provided in the `workspace_id` parameter. The parameter's list items may consist of the following identifier formats:
252
+
- Qualified workspace names
253
+
- Workspace IDs
254
+
- Azure resource IDs
255
+
256
+
For example, the following query executes in three workspaces:
244
257
245
258
```python
246
-
for result in response:
247
-
table = result.tables[0]
248
-
df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns])
A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py).
251
265
252
-
### Query metrics
266
+
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).
267
+
268
+
### Metrics query
253
269
254
270
The following example gets metrics for an Event Grid subscription. The resource URI is that of an event grid topic.
255
271
@@ -285,7 +301,7 @@ for metric in response.metrics:
285
301
print(metric_value.time_stamp)
286
302
```
287
303
288
-
### Handle metrics response
304
+
####Handle metrics query response
289
305
290
306
The metrics query API returns a `MetricsResult` object. The `MetricsResult` object contains properties such as a list of `Metric`-typed objects, `granularity`, `namespace`, and `timespan`. The `Metric` objects list can be accessed using the `metrics` param. Each `Metric` object in this list contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` contains `data` and `metadata_values` properties. In visual form, the object hierarchy of the response resembles the following structure:
291
307
@@ -337,30 +353,6 @@ for metric in response.metrics:
337
353
)
338
354
```
339
355
340
-
### Advanced scenarios
341
-
342
-
#### Query multiple workspaces
343
-
344
-
The same log query can be executed across multiple Log Analytics workspaces. In addition to the KQL query, the following parameters are required:
345
-
346
-
-`workspace_id` - The first (primary) workspace ID.
347
-
-`additional_workspaces` - A list of workspaces, excluding the workspace provided in the `workspace_id` parameter. The parameter's list items may consist of the following identifier formats:
348
-
- Qualified workspace names
349
-
- Workspace IDs
350
-
- Azure resource IDs
351
-
352
-
For example, the following query executes in three workspaces:
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).
363
-
364
356
## Troubleshooting
365
357
366
358
Enable the `azure.monitor.query` logger to collect traces from the library.
@@ -379,9 +371,7 @@ Optional keyword arguments can be passed in at the client and per-operation leve
379
371
380
372
## Next steps
381
373
382
-
### Additional documentation
383
-
384
-
For more extensive documentation, see the [Azure Monitor Query documentation][python-query-product-docs].
374
+
To learn more about Azure Monitor, see the [Azure Monitor service documentation][azure_monitor_overview].
385
375
386
376
## Contributing
387
377
@@ -393,19 +383,19 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
0 commit comments