Skip to content

Commit 21d0ed4

Browse files
authored
[Monitor Query] Rename MetricsClient param (#34760)
This renames the `resource_uris` argument to `resource_ids` since this is the the preferred term that is used both in the Azure Portal and the metrics dataplane API. Signed-off-by: Paul Van Eck <[email protected]>
1 parent 6bed7da commit 21d0ed4

File tree

9 files changed

+42
-41
lines changed

9 files changed

+42
-41
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- `MetricsBatchQueryClient` has been renamed to `MetricsClient`. ([#33958](https://github.com/Azure/azure-sdk-for-python/pull/33958))
1313
- 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))
1414
- Positional arguments in `MetricsClient.query_resources` are now required keyword-only arguments. ([#33958](https://github.com/Azure/azure-sdk-for-python/pull/33958))
15+
- The `resource_uris` argument in `MetricsClient.query_resources` has been renamed to `resource_ids`. ([#34760](https://github.com/Azure/azure-sdk-for-python/pull/34760))
1516

1617
### Bugs Fixed
1718

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,15 @@ Interpretation of the visualization data is left to the library consumer. To use
449449

450450
### Metrics query
451451

452-
The following example gets metrics for an Event Grid subscription. The resource URI is that of an Event Grid topic.
452+
The following example gets metrics for an Event Grid subscription. The resource ID (also known as resource URI) is that of an Event Grid topic.
453453

454-
The resource URI must be that of the resource for which metrics are being queried. It's normally of the format `/subscriptions/<id>/resourceGroups/<rg-name>/providers/<source>/topics/<resource-name>`.
454+
The resource ID must be that of the resource for which metrics are being queried. It's normally of the format `/subscriptions/<id>/resourceGroups/<rg-name>/providers/<source>/topics/<resource-name>`.
455455

456-
To find the resource URI:
456+
To find the resource ID/URI:
457457

458458
1. Navigate to your resource's page in the Azure portal.
459-
2. From the **Overview** blade, select the **JSON View** link.
460-
3. In the resulting JSON, copy the value of the `id` property.
459+
1. Select the **JSON View** link in the **Overview** section.
460+
1. Copy the value in the **Resource ID** text box at the top of the JSON view.
461461

462462
**NOTE**: The metrics are returned in the order of the metric_names sent.
463463

@@ -551,17 +551,17 @@ from azure.core.exceptions import HttpResponseError
551551
from azure.identity import DefaultAzureCredential
552552
from azure.monitor.query import MetricsClient, MetricAggregationType
553553

554-
554+
endpoint = "https://westus3.metrics.monitor.azure.com"
555555
credential = DefaultAzureCredential()
556556
client = MetricsClient(endpoint, credential)
557557

558-
resource_uris = [
558+
resource_ids = [
559559
"/subscriptions/<id>/resourceGroups/<rg-name>/providers/<source>/storageAccounts/<resource-name-1>",
560560
"/subscriptions/<id>/resourceGroups/<rg-name>/providers/<source>/storageAccounts/<resource-name-2>"
561561
]
562562

563563
response = client.query_resources(
564-
resource_uris=resource_uris,
564+
resource_ids=resource_ids,
565565
metric_namespace="Microsoft.Storage/storageAccounts",
566566
metric_names=["Ingress"],
567567
timespan=timedelta(hours=2),
@@ -618,7 +618,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
618618
[azure_subscription]: https://azure.microsoft.com/free/python/
619619
[changelog]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-query/CHANGELOG.md
620620
[kusto_query_language]: https://learn.microsoft.com/azure/data-explorer/kusto/query/
621-
[metric_namespaces]: https://learn.microsoft.com/azure/azure-monitor/reference/supported-metrics/metrics-index#metrics-by-resource-provider
621+
[metric_namespaces]: https://learn.microsoft.com/azure/azure-monitor/reference/supported-metrics/metrics-index#supported-metrics-and-log-categories-by-resource-type
622622
[package]: https://aka.ms/azsdk-python-monitor-query-pypi
623623
[pip]: https://pypi.org/project/pip/
624624
[python_logging]: https://docs.python.org/3/library/logging.html

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,22 @@ def process_prefer(server_timeout, include_statistics, include_visualization):
168168
return prefer.rstrip(",")
169169

170170

171-
def get_subscription_id_from_resource(resource_uri: str) -> str:
172-
"""Get the subscription ID from the provided resource URI.
171+
def get_subscription_id_from_resource(resource_id: str) -> str:
172+
"""Get the subscription ID from the provided resource ID.
173173
174-
The format of the resource URI is:
174+
The format of the resource ID is:
175175
/subscriptions/{subscriptionId}/resourceGroups/{group}/providers/{provider}/{type}/{name}
176176
177-
:param str resource_uri: The resource URI to parse.
177+
:param str resource_id: The resource ID to parse.
178178
:returns: The subscription ID.
179179
:rtype: str
180180
"""
181-
if not resource_uri:
182-
raise ValueError("Resource URI must not be None or empty.")
181+
if not resource_id:
182+
raise ValueError("Resource ID must not be None or empty.")
183183

184-
parts = resource_uri.split("subscriptions/")
184+
parts = resource_id.split("subscriptions/")
185185
if len(parts) != 2:
186-
raise ValueError("Resource URI must contain a subscription ID.")
186+
raise ValueError("Resource ID must contain a subscription ID.")
187187

188188
subscription_id = parts[1].split("/")[0]
189189
return subscription_id

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, endpoint: str, credential: TokenCredential, **kwargs: Any) ->
5050
def query_resources(
5151
self,
5252
*,
53-
resource_uris: Sequence[str],
53+
resource_ids: Sequence[str],
5454
metric_namespace: str,
5555
metric_names: Sequence[str],
5656
timespan: Optional[Union[timedelta, Tuple[datetime, timedelta], Tuple[datetime, datetime]]] = None,
@@ -64,8 +64,8 @@ def query_resources(
6464
) -> List[MetricsQueryResult]:
6565
"""Lists the metric values for multiple resources.
6666
67-
:keyword resource_uris: A list of resource URIs to query metrics for. Required.
68-
:paramtype resource_uris: list[str]
67+
:keyword resource_ids: A list of resource IDs to query metrics for. Required.
68+
:paramtype resource_ids: list[str]
6969
:keyword metric_namespace: Metric namespace that contains the requested metric names. Required.
7070
:paramtype metric_namespace: str
7171
:keyword metric_names: The names of the metrics (comma separated) to retrieve. Required.
@@ -116,15 +116,15 @@ def query_resources(
116116
:dedent: 0
117117
:caption: Get a response for a batch metrics query.
118118
"""
119-
if not resource_uris:
120-
raise ValueError("resource_uris must be provided and must not be empty.")
119+
if not resource_ids:
120+
raise ValueError("'resource_ids' must be provided and must not be empty.")
121121

122122
# Metric names with commas need to be encoded.
123123
metric_names = [x.replace(",", "%2") for x in metric_names]
124124

125125
start_time, end_time = get_timespan_iso8601_endpoints(timespan)
126-
resource_id_json: JSON = {"resourceids": list(resource_uris)}
127-
subscription_id = get_subscription_id_from_resource(resource_uris[0])
126+
resource_id_json: JSON = {"resourceids": list(resource_ids)}
127+
subscription_id = get_subscription_id_from_resource(resource_ids[0])
128128

129129
generated = self._batch_metrics_op.batch(
130130
subscription_id,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, endpoint: str, credential: AsyncTokenCredential, **kwargs: An
5151
async def query_resources(
5252
self,
5353
*,
54-
resource_uris: Sequence[str],
54+
resource_ids: Sequence[str],
5555
metric_namespace: str,
5656
metric_names: Sequence[str],
5757
timespan: Optional[Union[timedelta, Tuple[datetime, timedelta], Tuple[datetime, datetime]]] = None,
@@ -65,8 +65,8 @@ async def query_resources(
6565
) -> List[MetricsQueryResult]:
6666
"""Lists the metric values for multiple resources.
6767
68-
:keyword resource_uris: A list of resource URIs to query metrics for. Required.
69-
:paramtype resource_uris: list[str]
68+
:keyword resource_ids: A list of resource IDs to query metrics for. Required.
69+
:paramtype resource_ids: list[str]
7070
:keyword metric_namespace: Metric namespace that contains the requested metric names. Required.
7171
:paramtype metric_namespace: str
7272
:keyword metric_names: The names of the metrics (comma separated) to retrieve. Required.
@@ -117,15 +117,15 @@ async def query_resources(
117117
:dedent: 0
118118
:caption: Get a response for a batch metrics query.
119119
"""
120-
if not resource_uris:
121-
raise ValueError("resource_uris must be provided and must not be empty.")
120+
if not resource_ids:
121+
raise ValueError("resource_ids must be provided and must not be empty.")
122122

123123
# Metric names with commas need to be encoded.
124124
metric_names = [x.replace(",", "%2") for x in metric_names]
125125

126126
start_time, end_time = get_timespan_iso8601_endpoints(timespan)
127-
resource_id_json: JSON = {"resourceids": list(resource_uris)}
128-
subscription_id = get_subscription_id_from_resource(resource_uris[0])
127+
resource_id_json: JSON = {"resourceids": list(resource_ids)}
128+
subscription_id = get_subscription_id_from_resource(resource_ids[0])
129129

130130
generated = await self._batch_metrics_op.batch(
131131
subscription_id,

sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_multiple_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
This example uses DefaultAzureCredential, which requests a token from Azure Active Directory.
1414
For more information on DefaultAzureCredential, see https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential.
1515
16-
In this example, storage account resource URIs are queried for metrics.
16+
In this example, storage account resources are queried for metrics.
1717
"""
1818
import asyncio
1919

@@ -33,14 +33,14 @@ async def query_metrics_batch():
3333
credential = DefaultAzureCredential()
3434
client = MetricsClient(endpoint, credential)
3535

36-
resource_uris = [
36+
resource_ids = [
3737
'/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<account-1>',
3838
'/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<account-2>'
3939
]
4040
async with client:
4141
try:
4242
response = await client.query_resources(
43-
resource_uris=resource_uris,
43+
resource_ids=resource_ids,
4444
metric_namespace="Microsoft.Storage/storageAccounts",
4545
metric_names=["Ingress"],
4646
timespan=timedelta(hours=2),

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
This example uses DefaultAzureCredential, which requests a token from Azure Active Directory.
1414
For more information on DefaultAzureCredential, see https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential.
1515
16-
In this example, storage account resource URIs are queried for metrics.
16+
In this example, storage account resources are queried for metrics.
1717
"""
1818

1919
# [START send_metrics_batch_query]
@@ -30,14 +30,14 @@
3030
credential = DefaultAzureCredential()
3131
client = MetricsClient(endpoint, credential)
3232

33-
resource_uris = [
33+
resource_ids = [
3434
'/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<account-1>',
3535
'/subscriptions/<id>/resourceGroups/<rg>/providers/Microsoft.Storage/storageAccounts/<account-2>'
3636
]
3737

3838
try:
3939
response = client.query_resources(
40-
resource_uris=resource_uris,
40+
resource_ids=resource_ids,
4141
metric_namespace="Microsoft.Storage/storageAccounts",
4242
metric_names=["Ingress"],
4343
timespan=timedelta(hours=2),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TestMetricsClient(MetricsClientTestCase):
1919
def test_batch_metrics_auth(self, recorded_test, monitor_info):
2020
client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient))
2121
responses = client.query_resources(
22-
resource_uris=[monitor_info['metrics_resource_id']],
22+
resource_ids=[monitor_info['metrics_resource_id']],
2323
metric_namespace=METRIC_RESOURCE_PROVIDER,
2424
metric_names=[METRIC_NAME],
2525
aggregations=[MetricAggregationType.COUNT],
@@ -30,7 +30,7 @@ def test_batch_metrics_auth(self, recorded_test, monitor_info):
3030
def test_batch_metrics_granularity(self, recorded_test, monitor_info):
3131
client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient))
3232
responses = client.query_resources(
33-
resource_uris=[monitor_info['metrics_resource_id']],
33+
resource_ids=[monitor_info['metrics_resource_id']],
3434
metric_namespace=METRIC_RESOURCE_PROVIDER,
3535
metric_names=[METRIC_NAME],
3636
granularity=timedelta(minutes=5),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def test_batch_metrics_auth(self, recorded_test, monitor_info):
2525
MetricsClient, self.get_credential(MetricsClient, is_async=True))
2626
async with client:
2727
responses = await client.query_resources(
28-
resource_uris=[monitor_info['metrics_resource_id']],
28+
resource_ids=[monitor_info['metrics_resource_id']],
2929
metric_namespace=METRIC_RESOURCE_PROVIDER,
3030
metric_names=[METRIC_NAME],
3131
aggregations=[MetricAggregationType.COUNT],
@@ -39,7 +39,7 @@ async def test_batch_metrics_granularity(self, recorded_test, monitor_info):
3939
MetricsClient, self.get_credential(MetricsClient, is_async=True))
4040
async with client:
4141
responses = await client.query_resources(
42-
resource_uris=[monitor_info['metrics_resource_id']],
42+
resource_ids=[monitor_info['metrics_resource_id']],
4343
metric_namespace=METRIC_RESOURCE_PROVIDER,
4444
metric_names=[METRIC_NAME],
4545
granularity=timedelta(minutes=5),

0 commit comments

Comments
 (0)