Skip to content

Commit 929bf71

Browse files
pvanecksofiar-msft
authored andcommitted
[Monitor Query] Update metric batch client (Azure#33958)
- Renamed the client to `MetricsClient`. - Renamed `query_batch` to `query_resources` - Made `query_resources` positional arguments keyword only Signed-off-by: Paul Van Eck <[email protected]>
1 parent 331d32d commit 929bf71

20 files changed

+401
-395
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
### Features Added
66

7-
- Added `roll_up_by` keyword argument to `MetricsBatchQueryClient.query_batch` to support rolling up metrics by dimension. ([#33752](https://github.com/Azure/azure-sdk-for-python/pull/33752))
7+
- Added `roll_up_by` keyword argument to `MetricsClient.query_resources` to support rolling up metrics by dimension. ([#33752](https://github.com/Azure/azure-sdk-for-python/pull/33752))
88

99
### Breaking Changes
1010

1111
- The following changes are breaking against the previous preview release (i.e. `1.3.0b2`/`1.3.0b1`):
12-
- Reordered the arguments for the async `MetricsBatchQueryClient` constructor so that `endpoint` is now the first positional argument. ([#33752](https://github.com/Azure/azure-sdk-for-python/pull/33752))
13-
- Reordered the `metric_names` and `metric_namespace` positional arguments in `MetricsBatchQueryClient.query_batch`. ([#33752](https://github.com/Azure/azure-sdk-for-python/pull/33752))
12+
- `MetricsBatchQueryClient` has been renamed to `MetricsClient`. ([#33958](https://github.com/Azure/azure-sdk-for-python/pull/33958))
13+
- Reordered the arguments for the async `MetricsClient` constructor so that `endpoint` is now the first positional argument. ([#33752](https://github.com/Azure/azure-sdk-for-python/pull/33752))
14+
- Positional arguments in `MetricsClient.query_resources` are now required keyword-only arguments. ([#33958](https://github.com/Azure/azure-sdk-for-python/pull/33958))
1415

1516
### Bugs Fixed
1617

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The Azure Monitor Query client library is used to execute read-only queries agai
1919

2020
### Prerequisites
2121

22-
- Python 3.7 or later
22+
- Python 3.8 or later
2323
- An [Azure subscription][azure_subscription]
2424
- A [TokenCredential](https://learn.microsoft.com/python/api/azure-core/azure.core.credentials.tokencredential?view=azure-python) implementation, such as an [Azure Identity library credential type](https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes).
2525
- To query Logs, you need one of the following things:
@@ -37,19 +37,20 @@ pip install azure-monitor-query
3737

3838
### Create the client
3939

40-
An authenticated client is required to query Logs or Metrics. The library includes both synchronous and asynchronous forms of the clients. To authenticate, create an instance of a token credential. Use that instance when creating a `LogsQueryClient`, `MetricsQueryClient`, or `MetricsBatchQueryClient`. The following examples use `DefaultAzureCredential` from the [azure-identity](https://pypi.org/project/azure-identity/) package.
40+
An authenticated client is required to query Logs or Metrics. The library includes both synchronous and asynchronous forms of the clients. To authenticate, create an instance of a token credential. Use that instance when creating a `LogsQueryClient`, `MetricsQueryClient`, or `MetricsClient`. The following examples use `DefaultAzureCredential` from the [azure-identity](https://pypi.org/project/azure-identity/) package.
4141

4242
#### Synchronous clients
4343

4444
Consider the following example, which creates synchronous clients for both Logs and Metrics querying:
4545

4646
```python
4747
from azure.identity import DefaultAzureCredential
48-
from azure.monitor.query import LogsQueryClient, MetricsQueryClient
48+
from azure.monitor.query import LogsQueryClient, MetricsQueryClient, MetricsClient
4949

5050
credential = DefaultAzureCredential()
51-
logs_client = LogsQueryClient(credential)
52-
metrics_client = MetricsQueryClient(credential)
51+
logs_query_client = LogsQueryClient(credential)
52+
metrics_query_client = MetricsQueryClient(credential)
53+
metrics_client = MetricsClient("https://<regional endpoint>", credential)
5354
```
5455

5556
#### Asynchronous clients
@@ -58,20 +59,22 @@ The asynchronous forms of the query client APIs are found in the `.aio`-suffixed
5859

5960
```python
6061
from azure.identity.aio import DefaultAzureCredential
61-
from azure.monitor.query.aio import LogsQueryClient, MetricsQueryClient
62+
from azure.monitor.query.aio import LogsQueryClient, MetricsQueryClient, MetricsClient
6263

6364
credential = DefaultAzureCredential()
64-
async_logs_client = LogsQueryClient(credential)
65-
async_metrics_client = MetricsQueryClient(credential)
65+
async_logs_query_client = LogsQueryClient(credential)
66+
async_metrics_query_client = MetricsQueryClient(credential)
67+
async_metrics_client = MetricsClient("https://<regional endpoint>", credential)
68+
```
6669
```
6770
6871
#### Configure clients for non-public Azure clouds
6972
7073
By default, `LogsQueryClient` and `MetricsQueryClient` are configured to connect to the public Azure cloud. These can be configured to connect to non-public Azure clouds by passing in the correct `endpoint` argument: For example:
7174
7275
```python
73-
logs_client = LogsQueryClient(credential, endpoint="https://api.loganalytics.azure.cn/v1")
74-
metrics_client = MetricsQueryClient(credential, endpoint="https://management.chinacloudapi.cn")
76+
logs_query_client = LogsQueryClient(credential, endpoint="https://api.loganalytics.azure.cn/v1")
77+
metrics_query_client = MetricsQueryClient(credential, endpoint="https://management.chinacloudapi.cn")
7578
```
7679

7780
**Note**: Currently, `MetricsQueryClient` uses the Azure Resource Manager (ARM) endpoint for querying metrics, so you will need the corresponding management endpoint for your cloud when using this client. This is subject to change in the future.
@@ -114,6 +117,7 @@ Each set of metric values is a time series with the following characteristics:
114117
- [Metrics query](#metrics-query)
115118
- [Handle metrics query response](#handle-metrics-query-response)
116119
- [Example of handling response](#example-of-handling-response)
120+
- [Metrics batch query](#metrics-batch-query)
117121

118122
### Logs query
119123

@@ -527,7 +531,7 @@ for metric in response.metrics:
527531

528532
### Metrics batch query
529533

530-
A user can also query metrics from multiple resources at once using the `query_batch` method of `MetricsBatchQueryClient`. This uses a different API than the `MetricsQueryClient` and requires that a user pass in a regional endpoint when instantiating the client (for example, "https://westus3.metrics.monitor.azure.com").
534+
A user can also query metrics from multiple resources at once using the `query_resources` method of `MetricsClient`. This uses a different API than the `MetricsQueryClient` and requires that a user pass in a regional endpoint when instantiating the client (for example, "https://westus3.metrics.monitor.azure.com").
531535

532536
Note, each resource must be in the same region as the endpoint passed in when instantiating the client, and each resource must be in the same Azure subscription. Furthermore, the metric namespace that contains the metrics to be queried must also be passed. A list of metric namespaces can be found [here][metric_namespaces].
533537

@@ -538,19 +542,19 @@ import os
538542

539543
from azure.core.exceptions import HttpResponseError
540544
from azure.identity import DefaultAzureCredential
541-
from azure.monitor.query import MetricsBatchQueryClient, MetricAggregationType
545+
from azure.monitor.query import MetricsClient, MetricAggregationType
542546

543547

544548
credential = DefaultAzureCredential()
545-
client = MetricsBatchQueryClient(endpoint, credential)
549+
client = MetricsClient(endpoint, credential)
546550

547551
resource_uris = [
548552
"/subscriptions/<id>/resourceGroups/<rg-name>/providers/<source>/storageAccounts/<resource-name-1>",
549553
"/subscriptions/<id>/resourceGroups/<rg-name>/providers/<source>/storageAccounts/<resource-name-2>"
550554
]
551555

552-
response = client.query_batch(
553-
resource_uris,
556+
response = client.query_resources(
557+
resource_uris=resource_uris,
554558
metric_namespace="Microsoft.Storage/storageAccounts",
555559
metric_names=["Ingress"],
556560
timespan=timedelta(hours=2),
@@ -586,6 +590,7 @@ The following code samples show common scenarios with the Azure Monitor Query cl
586590
#### Metrics query samples
587591

588592
- [Send a query using MetricsQueryClient](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metrics_query.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_async.py))
593+
- [Send a query to multiple resources in a region and subscription using MetricsClient](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_multiple.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_multiple_async.py))
589594
- [Get a list of metric namespaces](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metric_namespaces.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py))
590595
- [Get a list of metric definitions](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metric_definitions.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py))
591596

sdk/monitor/azure-monitor-query/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "python",
44
"TagPrefix": "python/monitor/azure-monitor-query",
5-
"Tag": "python/monitor/azure-monitor-query_7462cb1df1"
5+
"Tag": "python/monitor/azure-monitor-query_152690fb22"
66
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from ._logs_query_client import LogsQueryClient
88
from ._metrics_query_client import MetricsQueryClient
9-
from ._metrics_batch_query_client import MetricsBatchQueryClient
9+
from ._metrics_client import MetricsClient
1010

1111
from ._enums import (
1212
LogsQueryStatus,
@@ -46,7 +46,7 @@
4646
"LogsTableRow",
4747
"LogsBatchQuery",
4848
"MetricsQueryClient",
49-
"MetricsBatchQueryClient",
49+
"MetricsClient",
5050
"MetricNamespace",
5151
"MetricNamespaceClassification",
5252
"MetricDefinition",

sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_batch_query_client.py renamed to sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_client.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
JSON = MutableMapping[str, Any] # pylint: disable=unsubscriptable-object
1919

2020

21-
class MetricsBatchQueryClient: # pylint: disable=client-accepts-api-version-keyword
22-
"""MetricsBatchQueryClient should be used for performing metrics queries on multiple monitored resources in the
21+
class MetricsClient: # pylint: disable=client-accepts-api-version-keyword
22+
"""MetricsClient should be used for performing metrics queries on multiple monitored resources in the
2323
same region. A credential with authorization at the subscription level is required when using this client.
2424
2525
:param str endpoint: The regional endpoint to use, for example
@@ -47,12 +47,12 @@ def __init__(self, endpoint: str, credential: TokenCredential, **kwargs: Any) ->
4747
self._batch_metrics_op = self._client.metrics_batch
4848

4949
@distributed_trace
50-
def query_batch(
50+
def query_resources(
5151
self,
52+
*,
5253
resource_uris: Sequence[str],
53-
metric_names: Sequence[str],
5454
metric_namespace: str,
55-
*,
55+
metric_names: Sequence[str],
5656
timespan: Optional[Union[timedelta, Tuple[datetime, timedelta], Tuple[datetime, datetime]]] = None,
5757
granularity: Optional[timedelta] = None,
5858
aggregations: Optional[Sequence[str]] = None,
@@ -64,12 +64,12 @@ def query_batch(
6464
) -> List[MetricsQueryResult]:
6565
"""Lists the metric values for multiple resources.
6666
67-
:param resource_uris: A list of resource URIs to query metrics for. Required.
68-
:type resource_uris: list[str]
69-
:param metric_names: The names of the metrics (comma separated) to retrieve. Required.
70-
:type metric_names: list[str]
71-
:param metric_namespace: Metric namespace that contains the requested metric names. Required.
72-
:type metric_namespace: str
67+
:keyword resource_uris: A list of resource URIs to query metrics for. Required.
68+
:paramtype resource_uris: list[str]
69+
:keyword metric_namespace: Metric namespace that contains the requested metric names. Required.
70+
:paramtype metric_namespace: str
71+
:keyword metric_names: The names of the metrics (comma separated) to retrieve. Required.
72+
:paramtype metric_names: list[str]
7373
:keyword timespan: The timespan for which to query the data. This can be a timedelta,
7474
a timedelta and a start datetime, or a start datetime/end datetime.
7575
:paramtype timespan: Optional[Union[~datetime.timedelta, tuple[~datetime.datetime, ~datetime.timedelta],
@@ -109,7 +109,7 @@ def query_batch(
109109
110110
.. admonition:: Example:
111111
112-
.. literalinclude:: ../samples/sample_metrics_batch_query.py
112+
.. literalinclude:: ../samples/sample_metrics_query_multiple.py
113113
:start-after: [START send_metrics_batch_query]
114114
:end-before: [END send_metrics_batch_query]
115115
:language: python
@@ -156,7 +156,7 @@ def close(self) -> None:
156156
"""Close the client session."""
157157
return self._client.close()
158158

159-
def __enter__(self) -> "MetricsBatchQueryClient":
159+
def __enter__(self) -> "MetricsClient":
160160
self._client.__enter__() # pylint:disable=no-member
161161
return self
162162

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def query_resource(
137137
orderby=order_by,
138138
filter=filter,
139139
metricnamespace=metric_namespace,
140-
connection_verify=False,
141140
**kwargs
142141
)
143142
return MetricsQueryResult._from_generated(generated) # pylint: disable=protected-access

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
from ._logs_query_client_async import LogsQueryClient
88
from ._metrics_query_client_async import MetricsQueryClient
9-
from ._metrics_batch_query_client_async import MetricsBatchQueryClient
9+
from ._metrics_client_async import MetricsClient
1010

11-
__all__ = ["LogsQueryClient", "MetricsQueryClient", "MetricsBatchQueryClient"]
11+
__all__ = ["LogsQueryClient", "MetricsQueryClient", "MetricsClient"]
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
JSON = MutableMapping[str, Any] # pylint: disable=unsubscriptable-object
2020

2121

22-
class MetricsBatchQueryClient: # pylint: disable=client-accepts-api-version-keyword
23-
"""MetricsBatchQueryClient should be used for performing metrics queries on multiple monitored resources in the
22+
class MetricsClient: # pylint: disable=client-accepts-api-version-keyword
23+
"""MetricsClient should be used for performing metrics queries on multiple monitored resources in the
2424
same region. A credential with authorization at the subscription level is required when using this client.
2525
2626
:param str endpoint: The regional endpoint to use, for example
@@ -48,12 +48,12 @@ def __init__(self, endpoint: str, credential: AsyncTokenCredential, **kwargs: An
4848
self._batch_metrics_op = self._client.metrics_batch
4949

5050
@distributed_trace_async
51-
async def query_batch(
51+
async def query_resources(
5252
self,
53+
*,
5354
resource_uris: Sequence[str],
54-
metric_names: Sequence[str],
5555
metric_namespace: str,
56-
*,
56+
metric_names: Sequence[str],
5757
timespan: Optional[Union[timedelta, Tuple[datetime, timedelta], Tuple[datetime, datetime]]] = None,
5858
granularity: Optional[timedelta] = None,
5959
aggregations: Optional[Sequence[str]] = None,
@@ -65,12 +65,12 @@ async def query_batch(
6565
) -> List[MetricsQueryResult]:
6666
"""Lists the metric values for multiple resources.
6767
68-
:param resource_uris: A list of resource URIs to query metrics for. Required.
69-
:type resource_uris: list[str]
70-
:param metric_names: The names of the metrics (comma separated) to retrieve. Required.
71-
:type metric_names: list[str]
72-
:param metric_namespace: Metric namespace that contains the requested metric names. Required.
73-
:type metric_namespace: str
68+
:keyword resource_uris: A list of resource URIs to query metrics for. Required.
69+
:paramtype resource_uris: list[str]
70+
:keyword metric_namespace: Metric namespace that contains the requested metric names. Required.
71+
:paramtype metric_namespace: str
72+
:keyword metric_names: The names of the metrics (comma separated) to retrieve. Required.
73+
:paramtype metric_names: list[str]
7474
:keyword timespan: The timespan for which to query the data. This can be a timedelta,
7575
a timedelta and a start datetime, or a start datetime/end datetime.
7676
:paramtype timespan: Optional[Union[~datetime.timedelta, tuple[~datetime.datetime, ~datetime.timedelta],
@@ -110,7 +110,7 @@ async def query_batch(
110110
111111
.. admonition:: Example:
112112
113-
.. literalinclude:: ../samples/async_samples/sample_metrics_batch_query_async.py
113+
.. literalinclude:: ../samples/async_samples/sample_metrics_query_multiple_async.py
114114
:start-after: [START send_metrics_batch_query_async]
115115
:end-before: [END send_metrics_batch_query_async]
116116
:language: python
@@ -153,7 +153,7 @@ async def query_batch(
153153
for value in generated["values"]
154154
]
155155

156-
async def __aenter__(self) -> "MetricsBatchQueryClient":
156+
async def __aenter__(self) -> "MetricsClient":
157157
await self._client.__aenter__()
158158
return self
159159

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ async def query_resource(
138138
orderby=order_by,
139139
filter=filter,
140140
metricnamespace=metric_namespace,
141-
connection_verify=False,
142141
**kwargs
143142
)
144143
return MetricsQueryResult._from_generated(generated) # pylint: disable=protected-access

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ For examples on authenticating with the Azure Monitor service, see [sample_authe
3535
### Metrics query samples
3636

3737
- [Send a query using MetricsQueryClient](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metrics_query.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_async.py))
38-
- [Send a batch of queries using MetricBatchQueryClient](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metrics_batch_query.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_batch_query_async.py))
38+
- [Send a query to multiple resources at once using MetricsClient](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_multiple.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_multiple_async.py))
3939
- [Get a list of metric namespaces](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metric_namespaces.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py))
4040
- [Get a list of metric definitions](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_metric_definitions.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py))
4141

4242
## Prerequisites
4343

44-
- Python 3.7 or later
44+
- Python 3.8 or later
4545
- An [Azure subscription][azure_subscription]
4646
- To query Logs, you need an [Azure Log Analytics workspace][azure_monitor_create_using_portal].
4747
- To query Metrics, you need an Azure resource of any kind (Storage Account, Key Vault, Cosmos DB, etc.).

0 commit comments

Comments
 (0)