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
Copy file name to clipboardExpand all lines: sdk/monitor/azure-monitor-query/README.md
+82-2Lines changed: 82 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ Install the Azure Monitor Query client library for Python with [pip][pip]:
20
20
pip install azure-monitor-query --pre
21
21
```
22
22
23
-
### Authenticate the client
23
+
### Create and Authenticate the client
24
24
A **token credential** is necessary to instantiate both the LogsQueryClient and the MetricsQueryClient object.
25
25
26
26
```Python
@@ -102,6 +102,11 @@ time-stamped data. Each set of metric values is a time series with the following
102
102
103
103
This sample 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. Please look at the samples if you don't want to use the pandas library.
104
104
105
+
#### Specifying a duration
106
+
107
+
The `duration` parameter can be used to specify the time duration for which to query the data. This can also be accompanied with either `start_time` or `end_time`. If either `start_time` or `end_time` are not provided, the current time is taken as the end time.
108
+
Alternatively, the `start_time` and `end_time` keyword arguments can be provided together instead of `duration` parameter like shown in the example below.
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""
122
126
123
127
# returns LogsQueryResults
@@ -136,6 +140,19 @@ for table in response.tables:
136
140
print(df)
137
141
```
138
142
143
+
#### Single query on multiple workspaces.
144
+
145
+
`additional_workspaces` param can be used to pass a list of workspaces that are included in the query when querying a single query over multiple workspaces. These can be qualified workspace names, workspace IDs or Azure resource IDs.
146
+
Note that a primary `workspace_id` must still be provided when querying additional workspaces like in the snippet below.
This sample shows sending multiple queries at the same time using batch query API. For each query, a `LogQueryRequest` object can be used. Alternatively, a dictionary can be used as well.
@@ -208,6 +225,8 @@ response = client.query(
208
225
209
226
This example shows getting the metrics for an EventGrid subscription. The resource URI is that of an eventgrid topic.
210
227
228
+
**Note** The resource URI must be that of the resource for which metrics are being queried. It's normally of the format, `/subscriptions/<id>/resourceGroups/<rg-name>/providers/<source>/topics/<resource-name>`.
229
+
211
230
```Python
212
231
import os
213
232
from datetime import timedelta
@@ -234,6 +253,67 @@ for metric in response.metrics:
234
253
print(metric_value.time_stamp)
235
254
```
236
255
256
+
### Handling the response for metrics
257
+
258
+
The metrics query API returns a `MetricsResult` object. This `MetricsResult` object has multiple properties, including: a list of `Metric` type objects, `interval`, `namespace`, and `timespan`.
259
+
260
+
This can be fetched using the `metrics` param.
261
+
262
+
Each of the items in this list is a `Metric` object that contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` contains `data` and `metadata_values` properties.
263
+
264
+
265
+
#### Object heirarchy of the response
266
+
267
+
```
268
+
MetricsResult
269
+
|---interval
270
+
|---timespan
271
+
|---cost
272
+
|---namespace
273
+
|---resourceregion
274
+
|---metrics (list of `Metric` objects)
275
+
|---id
276
+
|---type
277
+
|---name
278
+
|---unit
279
+
|---timeseries (list of `TimeSeriesElement` objects)
280
+
|---metadata_values
281
+
|---data (list of data points represented by `MetricValue` objects)
282
+
```
283
+
284
+
#### Sample usage of the API to handle response.
285
+
286
+
```Python
287
+
import os
288
+
from datetime import datetime, timedelta
289
+
from azure.monitor.query import MetricsQueryClient, AggregationType
290
+
from azure.identity import DefaultAzureCredential
291
+
292
+
credential = DefaultAzureCredential()
293
+
client = MetricsQueryClient(credential)
294
+
295
+
metrics_uri = os.environ['METRICS_RESOURCE_URI']
296
+
response = client.query(
297
+
metrics_uri,
298
+
metric_names=["MatchedEventCount"],
299
+
start_time=datetime(2021, 6, 21),
300
+
duration=timedelta(days=1),
301
+
aggregations=[AggregationType.COUNT]
302
+
)
303
+
304
+
for metric in response.metrics:
305
+
print(metric.name)
306
+
for time_series_element in metric.timeseries:
307
+
for metric_value in time_series_element.data:
308
+
if metric_value.count !=0:
309
+
print(
310
+
"There are {} matched events at {}".format(
311
+
metric_value.count,
312
+
metric_value.time_stamp
313
+
)
314
+
)
315
+
```
316
+
237
317
## Troubleshooting
238
318
239
319
- Enable `azure.monitor.query` logger to collect traces from the library.
0 commit comments