Skip to content

Commit 8912ff8

Browse files
Rakshith Bhyravabhotlaswathipil
andauthored
Update and improve README (#19903)
* Update and improve README * Update sdk/monitor/azure-monitor-query/README.md * Apply suggestions from code review Co-authored-by: swathipil <[email protected]> * Update sdk/monitor/azure-monitor-query/README.md Co-authored-by: swathipil <[email protected]> * Update sdk/monitor/azure-monitor-query/README.md Co-authored-by: swathipil <[email protected]> * Update sdk/monitor/azure-monitor-query/README.md Co-authored-by: swathipil <[email protected]> Co-authored-by: swathipil <[email protected]>
1 parent ee646d7 commit 8912ff8

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

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

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Install the Azure Monitor Query client library for Python with [pip][pip]:
2020
pip install azure-monitor-query --pre
2121
```
2222

23-
### Authenticate the client
23+
### Create and Authenticate the client
2424
A **token credential** is necessary to instantiate both the LogsQueryClient and the MetricsQueryClient object.
2525

2626
```Python
@@ -102,6 +102,11 @@ time-stamped data. Each set of metric values is a time series with the following
102102

103103
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.
104104

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.
109+
105110
```Python
106111
import os
107112
import pandas as pd
@@ -117,7 +122,6 @@ client = LogsQueryClient(credential)
117122
# Response time trend
118123
# request duration over the last 12 hours.
119124
query = """AppRequests |
120-
where TimeGenerated > ago(12h) |
121125
summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId"""
122126

123127
# returns LogsQueryResults
@@ -136,6 +140,19 @@ for table in response.tables:
136140
print(df)
137141
```
138142

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.
147+
148+
```Python
149+
client.query(
150+
<primary_workspace_id>,
151+
query,
152+
additional_workspaces=['<workspace 1>', '<workspace 2>']
153+
)
154+
```
155+
139156
### Get Logs for multiple queries
140157

141158
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(
208225

209226
This example shows getting the metrics for an EventGrid subscription. The resource URI is that of an eventgrid topic.
210227

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+
211230
```Python
212231
import os
213232
from datetime import timedelta
@@ -234,6 +253,67 @@ for metric in response.metrics:
234253
print(metric_value.time_stamp)
235254
```
236255

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+
237317
## Troubleshooting
238318

239319
- Enable `azure.monitor.query` logger to collect traces from the library.

sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ class TimeSeriesElement(object):
483483
484484
:keyword metadata_values: the metadata values returned if $filter was specified in the call.
485485
:paramtype metadata_values: list[~monitor_query_client.models.MetadataValue]
486-
:keyword data: An array of data points representing the metric values. This is only returned if
486+
:keyword data: An array of data points representing the metric values. This is only returned if
487487
a result type of data is specified.
488488
:paramtype data: list[~monitor_query_client.models.MetricValue]
489489
"""

0 commit comments

Comments
 (0)