Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 +16,13 @@ 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')

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

Expand Down
11 changes: 8 additions & 3 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,9 +16,13 @@ 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')

response = client.query(
os.environ['METRICS_RESOURCE_URI'],
metric_names=["MatchedEventCount"],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it by design to change it from "PublishSuccessCount" to "MatchedEventCount"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the metric name publishe success count does not have any aggregations supported - these metric names are defined by each service

start_time=datetime(2021, 6, 21),
duration='P1D',
aggregation=['Count']
)
assert response is not None
assert response.metrics is not None

Expand Down