Skip to content

Commit 24b79ab

Browse files
author
Rakshith Bhyravabhotla
authored
Aggregation should be a list (Azure#19381)
* Aggregation should be a list * Update sdk/monitor/azure-monitor-query/tests/async/test_metrics_client_async.py * Update sdk/monitor/azure-monitor-query/tests/test_metrics_client.py
1 parent 4834809 commit 24b79ab

File tree

6 files changed

+44
-21
lines changed

6 files changed

+44
-21
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
- `workspaces`, `workspace_ids`, `qualified_names` and `azure_resource_ids` are now merged into a single `additional_workspaces` list in the query API.
1010
- The `LogQueryRequest` object now takes in a `workspace_id` and `additional_workspaces` instead of `workspace`.
11+
- `aggregation` param is now a list instead of a string in the `query` method.
1112

1213
### Key Bugs Fixed
1314

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def query(self, resource_uri, metric_names, duration=None, **kwargs):
6262
:param resource_uri: The identifier of the resource.
6363
:type resource_uri: str
6464
:param metric_names: The names of the metrics to retrieve.
65-
:type metric_names: list
65+
:type metric_names: list[str]
6666
:param str duration: The duration for which to query the data. This can also be accompanied
6767
with either start_time or end_time. If start_time or end_time is not provided, the current time is
6868
taken as the end time. This should be provided in a ISO8601 string format like 'PT1H', 'P1Y2M10DT2H30M'.
@@ -72,8 +72,8 @@ def query(self, resource_uri, metric_names, duration=None, **kwargs):
7272
with either start_time or duration.
7373
:keyword interval: The interval (i.e. timegrain) of the query.
7474
:paramtype interval: ~datetime.timedelta
75-
:keyword aggregation: The list of aggregation types (comma separated) to retrieve.
76-
:paramtype aggregation: str
75+
:keyword aggregation: The list of aggregation types to retrieve.
76+
:paramtype aggregation: list[str]
7777
:keyword top: The maximum number of records to retrieve.
7878
Valid only if $filter is specified.
7979
Defaults to 10.
@@ -112,6 +112,9 @@ def query(self, resource_uri, metric_names, duration=None, **kwargs):
112112
"""
113113
start = kwargs.pop('start_time', None)
114114
end = kwargs.pop('end_time', None)
115+
aggregation = kwargs.pop("aggregation", None)
116+
if aggregation:
117+
kwargs.setdefault("aggregation", ",".join(aggregation))
115118
timespan = construct_iso8601(start, end, duration)
116119
kwargs.setdefault("metricnames", ",".join(metric_names))
117120
kwargs.setdefault("timespan", timespan)

sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ async def query(self, resource_uri: str, metric_names: List, duration: str = Non
6161
with either start_time or duration.
6262
:keyword interval: The interval (i.e. timegrain) of the query.
6363
:paramtype interval: ~datetime.timedelta
64-
:keyword aggregation: The list of aggregation types (comma separated) to retrieve.
65-
:paramtype aggregation: str
64+
:keyword aggregation: The list of aggregation types to retrieve.
65+
:paramtype aggregation: list[str]
6666
:keyword top: The maximum number of records to retrieve.
6767
Valid only if $filter is specified.
6868
Defaults to 10.
@@ -95,6 +95,9 @@ async def query(self, resource_uri: str, metric_names: List, duration: str = Non
9595
timespan = construct_iso8601(start, end, duration)
9696
kwargs.setdefault("metricnames", ",".join(metric_names))
9797
kwargs.setdefault("timespan", timespan)
98+
aggregation = kwargs.pop("aggregation", None)
99+
if aggregation:
100+
kwargs.setdefault("aggregation", ",".join(aggregation))
98101
generated = await self._metrics_op.list(resource_uri, connection_verify=False, **kwargs)
99102
return MetricsResult._from_generated(generated) # pylint: disable=protected-access
100103

sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT License.
33

44
import os
5-
from datetime import datetime
5+
from datetime import datetime, timedelta
66
import urllib3
77
from azure.monitor.query import MetricsQueryClient
88
from azure.identity import ClientSecretCredential
@@ -23,14 +23,21 @@
2323
metrics_uri = os.environ['METRICS_RESOURCE_URI']
2424
response = client.query(
2525
metrics_uri,
26-
metric_names=["PublishSuccessCount"],
27-
start_time=datetime(2021, 5, 25),
28-
duration='P1D'
26+
metric_names=["MatchedEventCount"],
27+
start_time=datetime(2021, 6, 21),
28+
duration='P1D',
29+
aggregation=['Count']
2930
)
3031

3132
for metric in response.metrics:
3233
print(metric.name)
3334
for time_series_element in metric.timeseries:
3435
for metric_value in time_series_element.data:
35-
print(metric_value.time_stamp)
36+
if metric_value.count != 0:
37+
print(
38+
"There are {} matched events at {}".format(
39+
metric_value.count,
40+
metric_value.time_stamp
41+
)
42+
)
3643
# [END send_metrics_query]

sdk/monitor/azure-monitor-query/tests/async/test_metrics_client_async.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import py
1+
from datetime import datetime
22
import pytest
33
import os
44
from azure.identity.aio import ClientSecretCredential
@@ -16,11 +16,15 @@ def _credential():
1616
async def test_metrics_auth():
1717
credential = _credential()
1818
client = MetricsQueryClient(credential)
19-
# returns LogsQueryResults
20-
response = await client.query(os.environ['METRICS_RESOURCE_URI'], metric_names=["PublishSuccessCount"], timespan='P2D')
21-
22-
assert response is not None
23-
assert response.metrics is not None
19+
response = await client.query(
20+
os.environ['METRICS_RESOURCE_URI'],
21+
metric_names=["MatchedEventCount"],
22+
start_time=datetime(2021, 6, 21),
23+
duration='P1D',
24+
aggregation=['Count']
25+
)
26+
assert response
27+
assert response.metrics
2428

2529
@pytest.mark.live_test_only
2630
async def test_metrics_namespaces():

sdk/monitor/azure-monitor-query/tests/test_metrics_client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import os
3+
from datetime import datetime
34
from azure.identity import ClientSecretCredential
45
from azure.monitor.query import MetricsQueryClient
56

@@ -15,11 +16,15 @@ def _credential():
1516
def test_metrics_auth():
1617
credential = _credential()
1718
client = MetricsQueryClient(credential)
18-
# returns LogsQueryResults
19-
response = client.query(os.environ['METRICS_RESOURCE_URI'], metric_names=["PublishSuccessCount"], timespan='P2D')
20-
21-
assert response is not None
22-
assert response.metrics is not None
19+
response = client.query(
20+
os.environ['METRICS_RESOURCE_URI'],
21+
metric_names=["MatchedEventCount"],
22+
start_time=datetime(2021, 6, 21),
23+
duration='P1D',
24+
aggregation=['Count']
25+
)
26+
assert response
27+
assert response.metrics
2328

2429
@pytest.mark.live_test_only
2530
def test_metrics_namespaces():

0 commit comments

Comments
 (0)