From b62e704579d703642ebfc3e6d75bc1bb1921ba38 Mon Sep 17 00:00:00 2001 From: Rakshith Bhyravabhotla Date: Mon, 23 Aug 2021 11:53:37 -0700 Subject: [PATCH] Consistency related changes (#20385) * time stamp * Rename AggragationType to MetricAggregationType * Metric Class * logs batch result * MetricNamespaceClassification * LogsTable + LogsTableColumn * lint * more lint --- sdk/monitor/azure-monitor-query/CHANGELOG.md | 8 ++ sdk/monitor/azure-monitor-query/README.md | 8 +- .../azure/monitor/query/__init__.py | 18 ++-- .../monitor/query/_metrics_query_client.py | 6 +- .../azure/monitor/query/_models.py | 89 ++++++++++--------- .../query/aio/_metrics_query_client_async.py | 6 +- .../sample_log_query_client_async.py | 2 +- .../sample_log_query_client_without_pandas.py | 2 +- .../samples/sample_metrics_query_client.py | 4 +- .../tests/async/test_metrics_client_async.py | 6 +- .../tests/perfstress_tests/metric_query.py | 4 +- .../tests/test_metrics_client.py | 6 +- 12 files changed, 86 insertions(+), 73 deletions(-) diff --git a/sdk/monitor/azure-monitor-query/CHANGELOG.md b/sdk/monitor/azure-monitor-query/CHANGELOG.md index c5cbf831efbe..ea4ae667b247 100644 --- a/sdk/monitor/azure-monitor-query/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-query/CHANGELOG.md @@ -5,6 +5,9 @@ ### Features Added - Added additional `display_description` attribute to the `Metric` type. +- Added a `MetricClass` enum to provide the class of a metric. +- Added a `metric_class` attribute to the `MetricDefinition` type. +- Added a `MetricNamespaceClassification` enum to support the `namespace_classification` attribute on `MetricNamespace` type. ### Breaking Changes @@ -22,6 +25,11 @@ - `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. +- `time_stamp` is renamed to `timestamp` in `MetricValue` type. +- `AggregationType` is renamed to `MetricAggregationType`. +- Removed `LogsBatchResultError` type. +- `LogsQueryResultTable` is named to `LogsTable` +- `LogsQueryResultColumn` is renamed to `LogsTableColumn` ### Bugs Fixed diff --git a/sdk/monitor/azure-monitor-query/README.md b/sdk/monitor/azure-monitor-query/README.md index a87aa1ae263d..8d3d209f4c24 100644 --- a/sdk/monitor/azure-monitor-query/README.md +++ b/sdk/monitor/azure-monitor-query/README.md @@ -226,10 +226,10 @@ LogsQueryResult / LogsBatchQueryResult |---statistics |---visualization |---error -|---tables (list of `LogsQueryResultTable` objects) +|---tables (list of `LogsTable` objects) |---name |---rows - |---columns (list of `LogsQueryResultColumn` objects) + |---columns (list of `LogsTableColumn` objects) |---name |---type ``` @@ -313,7 +313,7 @@ MetricsResult ```python import os from datetime import datetime, timedelta -from azure.monitor.query import MetricsQueryClient, AggregationType +from azure.monitor.query import MetricsQueryClient, MetricAggregationType from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() @@ -323,7 +323,7 @@ metrics_uri = os.environ['METRICS_RESOURCE_URI'] response = client.query( metrics_uri, metric_names=["MatchedEventCount"], - aggregations=[AggregationType.COUNT] + aggregations=[MetricAggregationType.COUNT] ) for metric in response.metrics: 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 13d5a58a442f..386aec140be1 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/__init__.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/__init__.py @@ -8,40 +8,42 @@ from ._metrics_query_client import MetricsQueryClient from ._models import ( - AggregationType, + MetricAggregationType, LogsBatchQueryResult, LogsQueryResult, - LogsQueryResultTable, - LogsQueryResultColumn, + LogsTable, + LogsTableColumn, MetricsResult, - LogsBatchResultError, LogsBatchQuery, MetricNamespace, + MetricNamespaceClassification, MetricDefinition, TimeSeriesElement, Metric, MetricValue, + MetricClass, MetricAvailability ) from ._version import VERSION __all__ = [ - "AggregationType", + "MetricAggregationType", "LogsQueryClient", "LogsBatchQueryResult", - "LogsBatchResultError", "LogsQueryResult", - "LogsQueryResultColumn", - "LogsQueryResultTable", + "LogsTableColumn", + "LogsTable", "LogsBatchQuery", "MetricsQueryClient", "MetricNamespace", + "MetricNamespaceClassification", "MetricDefinition", "MetricsResult", "TimeSeriesElement", "Metric", "MetricValue", + "MetricClass", "MetricAvailability" ] 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 cc3d69571b4b..aae99348a479 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 @@ -72,8 +72,8 @@ def query(self, resource_uri, metric_names, **kwargs): or tuple[~datetime.datetime, ~datetime.datetime] :keyword granularity: The granularity (i.e. timegrain) of the query. :paramtype granularity: ~datetime.timedelta - :keyword aggregations: The list of aggregation types to retrieve. Use `azure.monitor.query.AggregationType` - enum to get each aggregation type. + :keyword aggregations: The list of aggregation types to retrieve. Use + `azure.monitor.query.MetricAggregationType` enum to get each aggregation type. :paramtype aggregations: list[str] :keyword max_results: The maximum number of records to retrieve. Valid only if $filter is specified. @@ -134,7 +134,7 @@ def list_metric_namespaces(self, resource_uri, **kwargs): :keyword start_time: The ISO 8601 conform Date start time from which to query for metric namespaces. :paramtype start_time: str - :return: An iterator like instance of either MetricNamespaceCollection or the result of cls(response) + :return: An iterator like instance of either MetricNamespace or the result of cls(response) :rtype: ~azure.core.paging.ItemPaged[~azure.monitor.query.MetricNamespace] :raises: ~azure.core.exceptions.HttpResponseError """ 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 49264f9ac651..2b3a33672ec6 100644 --- a/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py +++ b/sdk/monitor/azure-monitor-query/azure/monitor/query/_models.py @@ -16,7 +16,7 @@ ) -class LogsQueryResultTable(object): +class LogsTable(object): """Contains the columns and rows for one table in a query response. All required parameters must be populated in order to send to Azure. @@ -24,12 +24,12 @@ class LogsQueryResultTable(object): :param name: Required. The name of the table. :type name: str :param columns: Required. The list of columns in this table. - :type columns: list[~azure.monitor.query.LogsQueryResultColumn] + :type columns: list[~azure.monitor.query.LogsTableColumn] :param rows: Required. The resulting rows from this query. :type rows: list[list[str]] """ def __init__(self, name, columns, rows): - # type: (str, List[LogsQueryResultColumn], List[List[str]]) -> None + # type: (str, List[LogsTableColumn], List[List[str]]) -> None self.name = name self.columns = columns self.rows = [process_row(self.columns, row) for row in rows] @@ -38,12 +38,12 @@ def __init__(self, name, columns, rows): def _from_generated(cls, generated): return cls( name=generated.name, - columns=[LogsQueryResultColumn(name=col.name, type=col.type) for col in generated.columns], + columns=[LogsTableColumn(name=col.name, type=col.type) for col in generated.columns], rows=generated.rows ) -class LogsQueryResultColumn(InternalColumn): +class LogsTableColumn(InternalColumn): """A column in a table. :ivar name: The name of this column. @@ -59,7 +59,7 @@ class LogsQueryResultColumn(InternalColumn): def __init__(self, **kwargs): # type: (Any) -> None - super(LogsQueryResultColumn, self).__init__(**kwargs) + super(LogsTableColumn, self).__init__(**kwargs) self.name = kwargs.get("name", None) self.type = kwargs.get("type", None) @@ -68,7 +68,7 @@ class LogsQueryResult(object): """Contains the tables, columns & rows resulting from a query. :ivar tables: The list of tables, columns and rows. - :vartype tables: list[~azure.monitor.query.LogsQueryResultTable] + :vartype tables: list[~azure.monitor.query.LogsTable] :ivar statistics: This will include a statistics property in the response that describes various performance statistics such as query execution time and resource usage. :vartype statistics: object @@ -92,7 +92,7 @@ def _from_generated(cls, generated): tables = None if generated.tables is not None: tables = [ - LogsQueryResultTable._from_generated( # pylint: disable=protected-access + LogsTable._from_generated( # pylint: disable=protected-access table ) for table in generated.tables ] @@ -222,7 +222,7 @@ class LogsBatchQueryResult(object): :ivar status: status code of the response. :vartype status: int :ivar tables: The list of tables, columns and rows. - :vartype tables: list[~azure.monitor.query.LogsQueryResultTable] + :vartype tables: list[~azure.monitor.query.LogsTable] :ivar statistics: This will include a statistics property in the response that describes various performance statistics such as query execution time and resource usage. :vartype statistics: object @@ -250,7 +250,7 @@ def _from_generated(cls, generated): tables = None if generated.body.tables is not None: tables = [ - LogsQueryResultTable._from_generated( # pylint: disable=protected-access + LogsTable._from_generated( # pylint: disable=protected-access table ) for table in generated.body.tables ] @@ -264,31 +264,13 @@ def _from_generated(cls, generated): ) -class LogsBatchResultError(object): - """Error response for a batch request. - - :ivar message: The error message describing the cause of the error. - :vartype message: str - :param code: The error code. - :vartype code: str - :param details: The details of the error. - :vartype inner_error: list[~azure.monitor.query.ErrorDetails] +class MetricNamespaceClassification(str, Enum): + """Kind of namespace """ - def __init__(self, **kwargs): - # type: (Any) -> None - self.message = kwargs.get("message", None) - self.code = kwargs.get("code", None) - self.details = kwargs.get("details", None) - @classmethod - def _from_generated(cls, generated): - if not generated: - return cls() - return cls( - message=generated.inner_error.message, - code=generated.code, - details=generated.inner_error.details - ) + PLATFORM = "Platform" + CUSTOM = "Custom" + QOS = "Qos" class MetricNamespace(object): @@ -302,6 +284,8 @@ class MetricNamespace(object): :paramtype name: str :keyword fully_qualified_namespace: The fully qualified namespace name. :paramtype fully_qualified_namespace: str + :keyword namespace_classification: Kind of namespace. Possible values include: "Platform", "Custom", "Qos". + :paramtype namespace_classification: str or ~azure.monitor.query.MetricNamespaceClassification """ def __init__( self, @@ -311,6 +295,7 @@ def __init__( self.type = kwargs.get('type', None) self.name = kwargs.get('name', None) self.fully_qualified_namespace = kwargs.get('fully_qualified_namespace', None) + self.namespace_classification = kwargs.get('namespace_classification', None) @classmethod def _from_generated(cls, generated): @@ -323,10 +308,23 @@ def _from_generated(cls, generated): id=generated.id, type=generated.type, name=generated.name, - fully_qualified_namespace=fully_qualified_namespace + fully_qualified_namespace=fully_qualified_namespace, + namespace_classification=generated.classification ) -class MetricDefinition(object): + +class MetricClass(str, Enum): + """The class of the metric. + """ + + AVAILABILITY = "Availability" + TRANSACTIONS = "Transactions" + ERRORS = "Errors" + LATENCY = "Latency" + SATURATION = "Saturation" + + +class MetricDefinition(object): #pylint: disable=too-many-instance-attributes """Metric definition class specifies the metadata for a metric. :keyword dimension_required: Flag to indicate whether the dimension is required. @@ -344,12 +342,15 @@ class MetricDefinition(object): :keyword primary_aggregation_type: the primary aggregation type value defining how to use the values for display. Possible values include: "None", "Average", "Count", "Minimum", "Maximum", "Total". - :paramtype primary_aggregation_type: str or ~monitor_query_client.models.AggregationType + :paramtype primary_aggregation_type: str or ~azure.monitor.query.MetricAggregationType + :keyword metric_class: The class of the metric. Possible values include: "Availability", + "Transactions", "Errors", "Latency", "Saturation". + :paramtype metric_class: str or ~azure.monitor.query.MetricClass :keyword supported_aggregation_types: the collection of what aggregation types are supported. - :paramtype supported_aggregation_types: list[str or ~monitor_query_client.models.AggregationType] + :paramtype supported_aggregation_types: list[str or ~azure.monitor.query.MetricAggregationType] :keyword metric_availabilities: the collection of what aggregation intervals are available to be queried. - :paramtype metric_availabilities: list[~monitor_query_client.models.MetricAvailability] + :paramtype metric_availabilities: list[~azure.monitor.query.MetricAvailability] :keyword id: the resource identifier of the metric definition. :paramtype id: str :keyword dimensions: the name and the display name of the dimension, i.e. it is a localizable @@ -371,6 +372,7 @@ def __init__( self.metric_availabilities = kwargs.get('metric_availabilities', None) # type: List[MetricAvailability] self.id = kwargs.get('id', None) # type: Optional[str] self.dimensions = kwargs.get('dimensions', None) # type: Optional[List[str]] + self.metric_class = kwargs.get('metric_class', None) # type: Optional[str] @classmethod def _from_generated(cls, generated): @@ -387,6 +389,7 @@ def _from_generated(cls, generated): unit=generated.unit, primary_aggregation_type=generated.primary_aggregation_type, supported_aggregation_types=generated.supported_aggregation_types, + metric_class=generated.metric_class, metric_availabilities=[ MetricAvailability._from_generated( # pylint: disable=protected-access val @@ -401,8 +404,8 @@ class MetricValue(object): All required parameters must be populated in order to send to Azure. - :ivar time_stamp: Required. The timestamp for the metric value in ISO 8601 format. - :vartype time_stamp: ~datetime.datetime + :ivar timestamp: Required. The timestamp for the metric value in ISO 8601 format. + :vartype timestamp: ~datetime.datetime :ivar average: The average value in the time range. :vartype average: float :ivar minimum: The least value in the time range. @@ -420,7 +423,7 @@ def __init__( **kwargs ): # type: (Any) -> None - self.time_stamp = kwargs['time_stamp'] + self.timestamp = kwargs['timestamp'] self.average = kwargs.get('average', None) self.minimum = kwargs.get('minimum', None) self.maximum = kwargs.get('maximum', None) @@ -432,7 +435,7 @@ def _from_generated(cls, generated): if not generated: return cls() return cls( - time_stamp=generated.time_stamp, + timestamp=generated.time_stamp, average=generated.average, minimum=generated.minimum, maximum=generated.maximum, @@ -552,7 +555,7 @@ def _from_generated(cls, generated): ) -class AggregationType(str, Enum): +class MetricAggregationType(str, Enum): """The aggregation type of the metric. """ 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 ed307d6e6fc7..dfdc2906f38b 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 @@ -67,8 +67,8 @@ async def query( or tuple[~datetime.datetime, ~datetime.datetime] :keyword granularity: The interval (i.e. timegrain) of the query. :paramtype granularity: ~datetime.timedelta - :keyword aggregations: The list of aggregation types to retrieve. Use `azure.monitor.query.AggregationType` - enum to get each aggregation type. + :keyword aggregations: The list of aggregation types to retrieve. + Use `azure.monitor.query.MetricAggregationType` enum to get each aggregation type. :paramtype aggregations: list[str] :keyword max_results: The maximum number of records to retrieve. Valid only if $filter is specified. @@ -118,7 +118,7 @@ def list_metric_namespaces(self, resource_uri: str, **kwargs: Any) -> AsyncItemP :keyword start_time: The ISO 8601 conform Date start time from which to query for metric namespaces. :paramtype start_time: str - :return: An iterator like instance of either MetricNamespaceCollection or the result of cls(response) + :return: An iterator like instance of either MetricNamespace or the result of cls(response) :rtype: ~azure.core.paging.ItemPaged[~azure.monitor.query.MetricNamespace] :raises: ~azure.core.exceptions.HttpResponseError """ diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_client_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_client_async.py index 4aebbde5e27f..15aa900e6a26 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_client_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_client_async.py @@ -42,7 +42,7 @@ async def logs_query(): # if you dont want to use pandas - here's how you can process it. - #response.tables is a LogsQueryResultTable + #response.tables is a LogsTable for table in response.tables: for col in table.columns: #LogsQueryResultColumn print(col.name + "/"+ col.type + " | ", end="") diff --git a/sdk/monitor/azure-monitor-query/samples/sample_log_query_client_without_pandas.py b/sdk/monitor/azure-monitor-query/samples/sample_log_query_client_without_pandas.py index f6a1bff2517d..da3aa5dd0cab 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_log_query_client_without_pandas.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_log_query_client_without_pandas.py @@ -22,7 +22,7 @@ if not response.tables: print("No results for the query") -#response.tables is a LogsQueryResultTable +#response.tables is a LogsTable for table in response.tables: for col in table.columns: #LogsQueryResultColumn print(col.name + "/"+ col.type + " | ", end="") diff --git a/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py b/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py index 15c6dcbdf157..224d7f05e8ea 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_client.py @@ -4,7 +4,7 @@ import os from datetime import datetime, timedelta import urllib3 -from azure.monitor.query import MetricsQueryClient, AggregationType +from azure.monitor.query import MetricsQueryClient, MetricAggregationType from azure.identity import DefaultAzureCredential urllib3.disable_warnings() @@ -21,7 +21,7 @@ metrics_uri, metric_names=["MatchedEventCount", "DeliverySuccesssCount"], timespan=timedelta(days=1), - aggregations=[AggregationType.COUNT] + aggregations=[MetricAggregationType.COUNT] ) for metric in response.metrics: diff --git a/sdk/monitor/azure-monitor-query/tests/async/test_metrics_client_async.py b/sdk/monitor/azure-monitor-query/tests/async/test_metrics_client_async.py index ab796d7d0924..e205e67734e2 100644 --- a/sdk/monitor/azure-monitor-query/tests/async/test_metrics_client_async.py +++ b/sdk/monitor/azure-monitor-query/tests/async/test_metrics_client_async.py @@ -2,7 +2,7 @@ import pytest import os from azure.identity.aio import ClientSecretCredential -from azure.monitor.query import AggregationType +from azure.monitor.query import MetricAggregationType from azure.monitor.query.aio import MetricsQueryClient def _credential(): @@ -22,7 +22,7 @@ async def test_metrics_auth(): os.environ['METRICS_RESOURCE_URI'], metric_names=["MatchedEventCount"], timespan=timedelta(days=1), - aggregations=[AggregationType.COUNT] + aggregations=[MetricAggregationType.COUNT] ) assert response assert response.metrics @@ -37,7 +37,7 @@ async def test_metrics_granularity(): metric_names=["MatchedEventCount"], timespan=timedelta(days=1), granularity=timedelta(minutes=5), - aggregations=[AggregationType.COUNT] + aggregations=[MetricAggregationType.COUNT] ) assert response assert response.granularity == timedelta(minutes=5) diff --git a/sdk/monitor/azure-monitor-query/tests/perfstress_tests/metric_query.py b/sdk/monitor/azure-monitor-query/tests/perfstress_tests/metric_query.py index 11995ed8812c..f91e2c9a3b23 100644 --- a/sdk/monitor/azure-monitor-query/tests/perfstress_tests/metric_query.py +++ b/sdk/monitor/azure-monitor-query/tests/perfstress_tests/metric_query.py @@ -8,7 +8,7 @@ from datetime import datetime, timezone from azure_devtools.perfstress_tests import PerfStressTest -from azure.monitor.query import MetricsQueryClient as SyncMetricsQueryClient, AggregationType +from azure.monitor.query import MetricsQueryClient as SyncMetricsQueryClient, MetricAggregationType from azure.monitor.query.aio import MetricsQueryClient as AsyncMetricsQueryClient from azure.identity import DefaultAzureCredential as SyncDefaultAzureCredential @@ -21,7 +21,7 @@ def __init__(self, arguments): # auth configuration self.metrics_uri = self.get_from_env('METRICS_RESOURCE_URI') self.names = ["MatchedEventCount"] - self.aggregations = [AggregationType.COUNT] + self.aggregations = [MetricAggregationType.COUNT] # Create clients self.metrics_client = SyncMetricsQueryClient( diff --git a/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py b/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py index 082da65c4385..ecdb7e7c8a3a 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py +++ b/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py @@ -2,7 +2,7 @@ import os from datetime import datetime, timedelta from azure.identity import ClientSecretCredential -from azure.monitor.query import MetricsQueryClient, AggregationType +from azure.monitor.query import MetricsQueryClient, MetricAggregationType def _credential(): credential = ClientSecretCredential( @@ -20,7 +20,7 @@ def test_metrics_auth(): os.environ['METRICS_RESOURCE_URI'], metric_names=["MatchedEventCount"], timespan=timedelta(days=1), - aggregations=[AggregationType.COUNT] + aggregations=[MetricAggregationType.COUNT] ) assert response assert response.metrics @@ -34,7 +34,7 @@ def test_metrics_granularity(): metric_names=["MatchedEventCount"], timespan=timedelta(days=1), granularity=timedelta(minutes=5), - aggregations=[AggregationType.COUNT] + aggregations=[MetricAggregationType.COUNT] ) assert response assert response.granularity == timedelta(minutes=5)