/
sample_metrics_query_multiple_async.py
64 lines (54 loc) · 2.69 KB
/
sample_metrics_query_multiple_async.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
FILE: sample_metrics_query_multiple_async.py
DESCRIPTION:
This sample demonstrates authenticating the MetricsClient and retrieving the "Ingress"
metric along with the "Average" aggregation type for multiple resources.
The query will execute over a timespan of 2 hours with a granularity of 5 minutes.
USAGE:
python sample_metrics_query_multiple_async.py
1) AZURE_METRICS_ENDPOINT - The regional metrics endpoint to use (i.e. https://westus3.metrics.monitor.azure.com)
This example uses DefaultAzureCredential, which requests a token from Azure Active Directory.
For more information on DefaultAzureCredential, see https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential.
In this example, storage account resources are queried for metrics.
"""
import asyncio
# [START send_metrics_batch_query_async]
from datetime import timedelta
import os
from azure.core.exceptions import HttpResponseError
from azure.identity.aio import DefaultAzureCredential
from azure.monitor.query import MetricAggregationType
from azure.monitor.query.aio import MetricsClient
async def query_metrics_batch():
endpoint = os.environ["AZURE_METRICS_ENDPOINT"]
credential = DefaultAzureCredential()
client = MetricsClient(endpoint, credential)
resource_ids = [
'/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<account-1>',
'/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<account-2>'
]
async with client:
try:
response = await client.query_resources(
resource_ids=resource_ids,
metric_namespace="Microsoft.Storage/storageAccounts",
metric_names=["Ingress"],
timespan=timedelta(hours=2),
granularity=timedelta(minutes=5),
aggregations=[MetricAggregationType.AVERAGE],
)
for metrics_query_result in response:
for metric in metrics_query_result.metrics:
print(metric.name + " -- " + metric.display_description)
for time_series_element in metric.timeseries:
for metric_value in time_series_element.data:
print("The ingress at {} is {}".format(metric_value.timestamp, metric_value.average))
except HttpResponseError as err:
print("something fatal happened")
print(err)
await credential.close()
# [END send_metrics_batch_query_async]
if __name__ == "__main__":
asyncio.run(query_metrics_batch())