From 1f6673c5557211e2b2f68d49733a891aa9e4876f Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Thu, 19 Aug 2021 17:02:11 -0700 Subject: [PATCH 1/5] Update metadata values --- sdk/monitor/azure-monitor-query/CHANGELOG.md | 1 + .../azure/monitor/query/__init__.py | 2 - .../azure/monitor/query/_logs_query_client.py | 3 ++ .../monitor/query/_metrics_query_client.py | 4 ++ .../azure/monitor/query/_models.py | 39 +++---------------- .../query/aio/_logs_query_client_async.py | 6 ++- .../query/aio/_metrics_query_client_async.py | 4 ++ 7 files changed, 22 insertions(+), 37 deletions(-) diff --git a/sdk/monitor/azure-monitor-query/CHANGELOG.md b/sdk/monitor/azure-monitor-query/CHANGELOG.md index 6589176e48db..ee9b7eaf013f 100644 --- a/sdk/monitor/azure-monitor-query/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-query/CHANGELOG.md @@ -17,6 +17,7 @@ - `is_dimension_required` is renamed to `dimension_required` - `time_grain` is renamed to `granularity` - `LogsQueryResult` now returns `datetime` objects for a time values. +- `LogsBatchQuery` doesn't accept a `request_id` anymore. ### Bugs Fixed diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/__init__.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/__init__.py index e3ceb3cbd419..13d5a58a442f 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/__init__.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/__init__.py @@ -18,7 +18,6 @@ LogsBatchQuery, MetricNamespace, MetricDefinition, - MetricsMetadataValue, TimeSeriesElement, Metric, MetricValue, @@ -40,7 +39,6 @@ "MetricNamespace", "MetricDefinition", "MetricsResult", - "MetricsMetadataValue", "TimeSeriesElement", "Metric", "MetricValue", diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/_logs_query_client.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/_logs_query_client.py index 30ae9238e579..7431510fb04d 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/_logs_query_client.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/_logs_query_client.py @@ -7,6 +7,7 @@ from typing import TYPE_CHECKING, Any, Union, Sequence, Dict, Optional from azure.core.exceptions import HttpResponseError +from azure.core.tracing.decorator import distributed_trace from ._generated._monitor_query_client import MonitorQueryClient @@ -49,6 +50,7 @@ def __init__(self, credential, **kwargs): ) self._query_op = self._client.query + @distributed_trace def query(self, workspace_id, query, timespan=None, **kwargs): # type: (str, str, Optional[timedelta], Any) -> LogsQueryResult """Execute an Analytics query. @@ -122,6 +124,7 @@ def query(self, workspace_id, query, timespan=None, **kwargs): except HttpResponseError as e: process_error(e) + @distributed_trace def query_batch(self, queries, **kwargs): # type: (Union[Sequence[Dict], Sequence[LogsBatchQuery]], Any) -> Sequence[LogsBatchQueryResult] """Execute a list of analytics queries. Each request can be either a LogQueryRequest diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py index 0b45d1bc10b3..7f490bebc238 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/_metrics_query_client.py @@ -8,6 +8,7 @@ # pylint: disable=anomalous-backslash-in-string from typing import TYPE_CHECKING, Any, Optional +from azure.core.tracing.decorator import distributed_trace from ._generated._monitor_query_client import ( MonitorQueryClient, @@ -53,6 +54,7 @@ def __init__(self, credential, **kwargs): self._namespace_op = self._client.metric_namespaces self._definitions_op = self._client.metric_definitions + @distributed_trace def query(self, resource_uri, metric_names, **kwargs): # type: (str, list, Optional[timedelta], Any) -> MetricsResult """Lists the metric values for a resource. @@ -120,6 +122,7 @@ def query(self, resource_uri, metric_names, **kwargs): generated = self._metrics_op.list(resource_uri, connection_verify=False, **kwargs) return MetricsResult._from_generated(generated) # pylint: disable=protected-access + @distributed_trace def list_metric_namespaces(self, resource_uri, **kwargs): # type: (str, Any) -> ItemPaged[MetricNamespace] """Lists the metric namespaces for the resource. @@ -143,6 +146,7 @@ def list_metric_namespaces(self, resource_uri, **kwargs): ), **kwargs) + @distributed_trace def list_metric_definitions(self, resource_uri, metric_namespace=None, **kwargs): # type: (str, str, Any) -> ItemPaged[MetricDefinition] """Lists the metric definitions for the resource. diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py index fcc22a6ba687..dee3c4f9d893 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py @@ -165,8 +165,6 @@ class LogsBatchQuery(object): :keyword additional_workspaces: A list of workspaces that are included in the query. These can be qualified workspace names, workspace Ids, or Azure resource Ids. :paramtype additional_workspaces: list[str] - :keyword request_id: The error details. - :paramtype request_id: str :keyword int server_timeout: the server timeout. The default timeout is 3 minutes, and the maximum timeout is 10 minutes. :keyword bool include_statistics: To get information about query statistics. @@ -201,7 +199,7 @@ def __init__(self, query, workspace_id, timespan, **kwargs): #pylint: disable=su headers = {'Prefer': prefer} timespan = construct_iso8601(timespan) additional_workspaces = kwargs.pop("additional_workspaces", None) - self.id = kwargs.get("request_id", str(uuid.uuid4())) + self.id = str(uuid.uuid4()) self.body = { "query": query, "timespan": timespan, "workspaces": additional_workspaces } @@ -490,7 +488,7 @@ class TimeSeriesElement(object): """A time series result type. The discriminator value is always TimeSeries in this case. :ivar metadata_values: The metadata values returned if $filter was specified in the call. - :vartype metadata_values: list[~monitor_query_client.models.MetadataValue] + :vartype metadata_values: dict(str, str) :ivar data: An array of data points representing the metric values. This is only returned if a result type of data is specified. :vartype data: list[~monitor_query_client.models.MetricValue] @@ -514,39 +512,12 @@ def _from_generated(cls, generated): if not generated: return cls() return cls( - metadata_values=[ - MetricsMetadataValue._from_generated( # pylint: disable=protected-access - mval - ) for mval in generated.metadatavalues - ], + metadata_values={ + obj.name.value: obj.value for obj in generated.metadatavalues + }, data=[MetricValue._from_generated(val) for val in generated.data] # pylint: disable=protected-access ) -class MetricsMetadataValue(object): - """Represents a metric metadata value. - - :ivar name: The name of the metadata. - :vartype name: str - :ivar value: The value of the metadata. - :vartype value: str - """ - def __init__( - self, - **kwargs - ): - # type: (Any) -> None - self.name = kwargs.get('name', None) - self.value = kwargs.get('value', None) - - @classmethod - def _from_generated(cls, generated): - if not generated: - return cls() - return cls( - name=generated.name.value, - value=generated.value - ) - class MetricAvailability(object): """Metric availability specifies the time grain (aggregation interval or frequency) diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_logs_query_client_async.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_logs_query_client_async.py index 06718475a095..deb3ba2a99f9 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_logs_query_client_async.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_logs_query_client_async.py @@ -8,6 +8,8 @@ from datetime import datetime, timedelta from typing import Any, Tuple, Union, Sequence, Dict, Optional, TYPE_CHECKING from azure.core.exceptions import HttpResponseError +from azure.core.tracing.decorator_async import distributed_trace_async + from .._generated.aio._monitor_query_client import MonitorQueryClient from .._generated.models import BatchRequest, QueryBody as LogsQueryBody @@ -38,6 +40,7 @@ def __init__(self, credential: "AsyncTokenCredential", **kwargs: Any) -> None: ) self._query_op = self._client.query + @distributed_trace_async async def query( self, workspace_id: str, @@ -106,6 +109,7 @@ async def query( except HttpResponseError as e: process_error(e) + @distributed_trace_async async def query_batch( self, queries: Union[Sequence[Dict], Sequence[LogsBatchQuery]], @@ -118,7 +122,7 @@ async def query_batch( :param queries: The list of queries that should be processed :type queries: list[dict] or list[~azure.monitor.query.LogsBatchQuery] - :return: BatchResponse, or the result of cls(response) + :return: list of LogsBatchQueryResult objects, or the result of cls(response) :rtype: ~list[~azure.monitor.query.LogsBatchQueryResult] :raises: ~azure.core.exceptions.HttpResponseError """ diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py index 69da6544cb8c..a801f6468d95 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py @@ -11,6 +11,7 @@ from typing import TYPE_CHECKING, Any, List, Optional from azure.core.async_paging import AsyncItemPaged +from azure.core.tracing.decorator_async import distributed_trace_async from .._generated.aio._monitor_query_client import ( MonitorQueryClient, @@ -43,6 +44,7 @@ def __init__(self, credential: "AsyncTokenCredential", **kwargs: Any) -> None: self._namespace_op = self._client.metric_namespaces self._definitions_op = self._client.metric_definitions + @distributed_trace_async async def query( self, resource_uri: str, @@ -104,6 +106,7 @@ async def query( generated = await self._metrics_op.list(resource_uri, connection_verify=False, **kwargs) return MetricsResult._from_generated(generated) # pylint: disable=protected-access + @distributed_trace_async def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> AsyncItemPaged[MetricNamespace]: """Lists the metric namespaces for the resource. @@ -126,6 +129,7 @@ def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> AsyncItemP ), **kwargs) + @distributed_trace_async def list_metric_definitions( self, resource_uri: str, From 027447fbca0b5ecda7114be8c5298cb5f11a4974 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Thu, 19 Aug 2021 17:53:47 -0700 Subject: [PATCH 2/5] distributed trace --- .../query/aio/_metrics_query_client_async.py | 5 +-- .../sample_metric_definitions_async.py | 36 +++++++++++-------- .../sample_metric_namespaces_async.py | 33 +++++++++-------- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py b/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py index a801f6468d95..4703b4af2214 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/aio/_metrics_query_client_async.py @@ -11,6 +11,7 @@ from typing import TYPE_CHECKING, Any, List, Optional from azure.core.async_paging import AsyncItemPaged +from azure.core.tracing.decorator import distributed_trace from azure.core.tracing.decorator_async import distributed_trace_async from .._generated.aio._monitor_query_client import ( @@ -106,7 +107,7 @@ async def query( generated = await self._metrics_op.list(resource_uri, connection_verify=False, **kwargs) return MetricsResult._from_generated(generated) # pylint: disable=protected-access - @distributed_trace_async + @distributed_trace def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> AsyncItemPaged[MetricNamespace]: """Lists the metric namespaces for the resource. @@ -129,7 +130,7 @@ def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> AsyncItemP ), **kwargs) - @distributed_trace_async + @distributed_trace def list_metric_definitions( self, resource_uri: str, diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py index c18265716966..c74010459cde 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py @@ -4,25 +4,31 @@ import os import asyncio from azure.monitor.query.aio import MetricsQueryClient -from azure.identity.aio import DefaultAzureCredential +from azure.identity.aio import ClientSecretCredential -async def list_namespaces(): - credential = DefaultAzureCredential( - client_id = os.environ['AZURE_CLIENT_ID'], - client_secret = os.environ['AZURE_CLIENT_SECRET'], - tenant_id = os.environ['AZURE_TENANT_ID'] - ) +class ListDefinitions(): + async def list_definitions(self): + credential = ClientSecretCredential( + client_id = os.environ['AZURE_CLIENT_ID'], + client_secret = os.environ['AZURE_CLIENT_SECRET'], + tenant_id = os.environ['AZURE_TENANT_ID'] + ) - client = MetricsQueryClient(credential) + client = MetricsQueryClient(credential) - metrics_uri = os.environ['METRICS_RESOURCE_URI'] - response = client.list_metric_definitions(metrics_uri) + metrics_uri = '/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/sabhyrav-resourcegroup/providers/Microsoft.EventGrid/topics/rakshith-cloud' + async with client: + response = client.list_metric_definitions(metrics_uri) - async for item in response: - print(item) - for availability in item.metric_availabilities: - print(availability.time_grain) + async for item in response: + print(item.namespace) + for availability in item.metric_availabilities: + print(availability.granularity) + +async def main(): + sample = ListDefinitions() + await sample.list_definitions() if __name__ == '__main__': loop = asyncio.get_event_loop() - loop.run_until_complete(list_namespaces()) + loop.run_until_complete(main()) diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py index 99cfbb7bef5a..39cd074413d4 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py @@ -4,24 +4,29 @@ import os import asyncio from azure.monitor.query.aio import MetricsQueryClient -from azure.identity.aio import DefaultAzureCredential +from azure.identity.aio import ClientSecretCredential -async def list_namespaces(): - credential = DefaultAzureCredential( - client_id = os.environ['AZURE_CLIENT_ID'], - client_secret = os.environ['AZURE_CLIENT_SECRET'], - tenant_id = os.environ['AZURE_TENANT_ID'] - ) +class ListNameSpaces(): + async def list_namespaces(self): + credential = ClientSecretCredential( + client_id = os.environ['AZURE_CLIENT_ID'], + client_secret = os.environ['AZURE_CLIENT_SECRET'], + tenant_id = os.environ['AZURE_TENANT_ID'] + ) - client = MetricsQueryClient(credential) + client = MetricsQueryClient(credential) - metrics_uri = os.environ['METRICS_RESOURCE_URI'] - response = client.list_metric_namespaces(metrics_uri) + metrics_uri = os.environ['METRICS_RESOURCE_URI'] + async with client: + response = client.list_metric_namespaces(metrics_uri) + async for item in response: + print(item.fully_qualified_namespace) + print(item.type) - async for item in response: - print(item.metric_namespace_name) - print(item.type) +async def main(): + sample = ListNameSpaces() + await sample.list_namespaces() if __name__ == '__main__': loop = asyncio.get_event_loop() - loop.run_until_complete(list_namespaces()) + loop.run_until_complete(main()) From d30e955a421dfe2fe838be90e77d2d9b8524126d Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Thu, 19 Aug 2021 17:54:33 -0700 Subject: [PATCH 3/5] oops --- .../samples/async_samples/sample_metric_definitions_async.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py index c74010459cde..daf9c8e00a9b 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py @@ -16,7 +16,7 @@ async def list_definitions(self): client = MetricsQueryClient(credential) - metrics_uri = '/subscriptions/faa080af-c1d8-40ad-9cce-e1a450ca5b57/resourceGroups/sabhyrav-resourcegroup/providers/Microsoft.EventGrid/topics/rakshith-cloud' + metrics_uri = os.environ['METRICS_RESOURCE_URI'] async with client: response = client.list_metric_definitions(metrics_uri) From 6358201d1e16a2402b253b87f10095d1f6d97c09 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Thu, 19 Aug 2021 17:57:10 -0700 Subject: [PATCH 4/5] Update sdk/monitor/azure-monitor-query/CHANGELOG.md --- sdk/monitor/azure-monitor-query/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/monitor/azure-monitor-query/CHANGELOG.md b/sdk/monitor/azure-monitor-query/CHANGELOG.md index ee9b7eaf013f..a65b1d2871ea 100644 --- a/sdk/monitor/azure-monitor-query/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-query/CHANGELOG.md @@ -18,6 +18,7 @@ - `time_grain` is renamed to `granularity` - `LogsQueryResult` now returns `datetime` objects for a time values. - `LogsBatchQuery` doesn't accept a `request_id` anymore. +- `MetricsMetadataValues` is removed. A dictionary is used instead. ### Bugs Fixed From 46737f638a753f5c42662b164c676ebfa17de6d6 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Fri, 20 Aug 2021 10:57:33 -0700 Subject: [PATCH 5/5] Apply suggestions from code review --- .../async_samples/sample_metric_definitions_async.py | 8 ++------ .../async_samples/sample_metric_namespaces_async.py | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py index daf9c8e00a9b..0184a48bd59d 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py @@ -4,15 +4,11 @@ import os import asyncio from azure.monitor.query.aio import MetricsQueryClient -from azure.identity.aio import ClientSecretCredential +from azure.identity.aio import DefaultAzureCredential class ListDefinitions(): async def list_definitions(self): - credential = ClientSecretCredential( - client_id = os.environ['AZURE_CLIENT_ID'], - client_secret = os.environ['AZURE_CLIENT_SECRET'], - tenant_id = os.environ['AZURE_TENANT_ID'] - ) + credential = DefaultAzureCredential() client = MetricsQueryClient(credential) diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py index 39cd074413d4..0991b3927b78 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py @@ -4,15 +4,11 @@ import os import asyncio from azure.monitor.query.aio import MetricsQueryClient -from azure.identity.aio import ClientSecretCredential +from azure.identity.aio import DefaultAzureCredential class ListNameSpaces(): async def list_namespaces(self): - credential = ClientSecretCredential( - client_id = os.environ['AZURE_CLIENT_ID'], - client_secret = os.environ['AZURE_CLIENT_SECRET'], - tenant_id = os.environ['AZURE_TENANT_ID'] - ) + credential = DefaultAzureCredential() client = MetricsQueryClient(credential)