Skip to content

Commit

Permalink
Aggregation should be a list (Azure#19381)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Rakshith Bhyravabhotla authored Jun 23, 2021
1 parent 4834809 commit 24b79ab
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
1 change: 1 addition & 0 deletions sdk/monitor/azure-monitor-query/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

### Key Bugs Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def query(self, resource_uri, metric_names, duration=None, **kwargs):
:param resource_uri: The identifier of the resource.
:type resource_uri: str
:param metric_names: The names of the metrics to retrieve.
:type metric_names: list
:type metric_names: list[str]
:param str duration: The duration for which to query the data. This can also be accompanied
with either start_time or end_time. If start_time or end_time is not provided, the current time is
taken as the end time. This should be provided in a ISO8601 string format like 'PT1H', 'P1Y2M10DT2H30M'.
Expand All @@ -72,8 +72,8 @@ def query(self, resource_uri, metric_names, duration=None, **kwargs):
with either start_time or duration.
:keyword interval: The interval (i.e. timegrain) of the query.
:paramtype interval: ~datetime.timedelta
:keyword aggregation: The list of aggregation types (comma separated) to retrieve.
:paramtype aggregation: str
:keyword aggregation: The list of aggregation types to retrieve.
:paramtype aggregation: list[str]
:keyword top: The maximum number of records to retrieve.
Valid only if $filter is specified.
Defaults to 10.
Expand Down Expand Up @@ -112,6 +112,9 @@ def query(self, resource_uri, metric_names, duration=None, **kwargs):
"""
start = kwargs.pop('start_time', None)
end = kwargs.pop('end_time', None)
aggregation = kwargs.pop("aggregation", None)
if aggregation:
kwargs.setdefault("aggregation", ",".join(aggregation))
timespan = construct_iso8601(start, end, duration)
kwargs.setdefault("metricnames", ",".join(metric_names))
kwargs.setdefault("timespan", timespan)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ async def query(self, resource_uri: str, metric_names: List, duration: str = Non
with either start_time or duration.
:keyword interval: The interval (i.e. timegrain) of the query.
:paramtype interval: ~datetime.timedelta
:keyword aggregation: The list of aggregation types (comma separated) to retrieve.
:paramtype aggregation: str
:keyword aggregation: The list of aggregation types to retrieve.
:paramtype aggregation: list[str]
:keyword top: The maximum number of records to retrieve.
Valid only if $filter is specified.
Defaults to 10.
Expand Down Expand Up @@ -95,6 +95,9 @@ async def query(self, resource_uri: str, metric_names: List, duration: str = Non
timespan = construct_iso8601(start, end, duration)
kwargs.setdefault("metricnames", ",".join(metric_names))
kwargs.setdefault("timespan", timespan)
aggregation = kwargs.pop("aggregation", None)
if aggregation:
kwargs.setdefault("aggregation", ",".join(aggregation))
generated = await self._metrics_op.list(resource_uri, connection_verify=False, **kwargs)
return MetricsResult._from_generated(generated) # pylint: disable=protected-access

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the MIT License.

import os
from datetime import datetime
from datetime import datetime, timedelta
import urllib3
from azure.monitor.query import MetricsQueryClient
from azure.identity import ClientSecretCredential
Expand All @@ -23,14 +23,21 @@
metrics_uri = os.environ['METRICS_RESOURCE_URI']
response = client.query(
metrics_uri,
metric_names=["PublishSuccessCount"],
start_time=datetime(2021, 5, 25),
duration='P1D'
metric_names=["MatchedEventCount"],
start_time=datetime(2021, 6, 21),
duration='P1D',
aggregation=['Count']
)

for metric in response.metrics:
print(metric.name)
for time_series_element in metric.timeseries:
for metric_value in time_series_element.data:
print(metric_value.time_stamp)
if metric_value.count != 0:
print(
"There are {} matched events at {}".format(
metric_value.count,
metric_value.time_stamp
)
)
# [END send_metrics_query]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import py
from datetime import datetime
import pytest
import os
from azure.identity.aio import ClientSecretCredential
Expand All @@ -16,11 +16,15 @@ def _credential():
async def test_metrics_auth():
credential = _credential()
client = MetricsQueryClient(credential)
# returns LogsQueryResults
response = await client.query(os.environ['METRICS_RESOURCE_URI'], metric_names=["PublishSuccessCount"], timespan='P2D')

assert response is not None
assert response.metrics is not None
response = await client.query(
os.environ['METRICS_RESOURCE_URI'],
metric_names=["MatchedEventCount"],
start_time=datetime(2021, 6, 21),
duration='P1D',
aggregation=['Count']
)
assert response
assert response.metrics

@pytest.mark.live_test_only
async def test_metrics_namespaces():
Expand Down
15 changes: 10 additions & 5 deletions sdk/monitor/azure-monitor-query/tests/test_metrics_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import os
from datetime import datetime
from azure.identity import ClientSecretCredential
from azure.monitor.query import MetricsQueryClient

Expand All @@ -15,11 +16,15 @@ def _credential():
def test_metrics_auth():
credential = _credential()
client = MetricsQueryClient(credential)
# returns LogsQueryResults
response = client.query(os.environ['METRICS_RESOURCE_URI'], metric_names=["PublishSuccessCount"], timespan='P2D')

assert response is not None
assert response.metrics is not None
response = client.query(
os.environ['METRICS_RESOURCE_URI'],
metric_names=["MatchedEventCount"],
start_time=datetime(2021, 6, 21),
duration='P1D',
aggregation=['Count']
)
assert response
assert response.metrics

@pytest.mark.live_test_only
def test_metrics_namespaces():
Expand Down

0 comments on commit 24b79ab

Please sign in to comment.