diff --git a/sdk/monitor/azure-monitor-query/CHANGELOG.md b/sdk/monitor/azure-monitor-query/CHANGELOG.md index 6589176e48db..a65b1d2871ea 100644 --- a/sdk/monitor/azure-monitor-query/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-query/CHANGELOG.md @@ -17,6 +17,8 @@ - `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. +- `MetricsMetadataValues` is removed. A dictionary is used instead. ### 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..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,8 @@ 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 ( MonitorQueryClient, @@ -43,6 +45,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 +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 def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> AsyncItemPaged[MetricNamespace]: """Lists the metric namespaces for the resource. @@ -126,6 +130,7 @@ def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> AsyncItemP ), **kwargs) + @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..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 @@ -6,23 +6,25 @@ from azure.monitor.query.aio import MetricsQueryClient from azure.identity.aio import DefaultAzureCredential -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 = DefaultAzureCredential() - client = MetricsQueryClient(credential) + client = MetricsQueryClient(credential) - metrics_uri = os.environ['METRICS_RESOURCE_URI'] - response = client.list_metric_definitions(metrics_uri) + metrics_uri = os.environ['METRICS_RESOURCE_URI'] + 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..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 @@ -6,22 +6,23 @@ from azure.monitor.query.aio import MetricsQueryClient from azure.identity.aio import DefaultAzureCredential -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 = DefaultAzureCredential() - 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())