%global _empty_manifest_terminate_build 0 Name: python-azure-monitor-query Version: 1.2.0 Release: 1 Summary: Microsoft Azure Monitor Query Client Library for Python License: MIT License URL: https://github.com/Azure/azure-sdk-for-python Source0: https://mirrors.nju.edu.cn/pypi/web/packages/ad/16/fd06cccfc583d8d38d8d99ee92ec1bbc9604cf6e8c62e64ddca5644e0a60/azure-monitor-query-1.2.0.zip BuildArch: noarch %description # Azure Monitor Query client library for Python The Azure Monitor Query client library is used to execute read-only queries against [Azure Monitor][azure_monitor_overview]'s two data platforms: - [Logs](https://learn.microsoft.com/azure/azure-monitor/logs/data-platform-logs) - Collects and organizes log and performance data from monitored resources. Data from different sources such as platform logs from Azure services, log and performance data from virtual machines agents, and usage and performance data from apps can be consolidated into a single [Azure Log Analytics workspace](https://learn.microsoft.com/azure/azure-monitor/logs/data-platform-logs#log-analytics-and-workspaces). The various data types can be analyzed together using the [Kusto Query Language][kusto_query_language]. - [Metrics](https://learn.microsoft.com/azure/azure-monitor/essentials/data-platform-metrics) - Collects numeric data from monitored resources into a time series database. Metrics are numerical values that are collected at regular intervals and describe some aspect of a system at a particular time. Metrics are lightweight and capable of supporting near real-time scenarios, making them useful for alerting and fast detection of issues. **Resources:** - [Source code][source] - [Package (PyPI)][package] - [Package (Conda)](https://anaconda.org/microsoft/azure-monitor-query/) - [API reference documentation][python-query-ref-docs] - [Service documentation][azure_monitor_overview] - [Samples][samples] - [Change log][changelog] ## Getting started ### Prerequisites - Python 3.7 or later - An [Azure subscription][azure_subscription] - 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). - To query Logs, you need an [Azure Log Analytics workspace][azure_monitor_create_using_portal]. - To query Metrics, you need an Azure resource of any kind (Storage Account, Key Vault, Cosmos DB, etc.). ### Install the package Install the Azure Monitor Query client library for Python with [pip][pip]: ```bash pip install azure-monitor-query ``` ### Create the client 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` or `MetricsQueryClient`. The following examples use `DefaultAzureCredential` from the [azure-identity](https://pypi.org/project/azure-identity/) package. #### Synchronous clients Consider the following example, which creates synchronous clients for both Logs and Metrics querying: ```python from azure.identity import DefaultAzureCredential from azure.monitor.query import LogsQueryClient, MetricsQueryClient credential = DefaultAzureCredential() logs_client = LogsQueryClient(credential) metrics_client = MetricsQueryClient(credential) ``` #### Asynchronous clients The asynchronous forms of the query client APIs are found in the `.aio`-suffixed namespace. For example: ```python from azure.identity.aio import DefaultAzureCredential from azure.monitor.query.aio import LogsQueryClient, MetricsQueryClient credential = DefaultAzureCredential() async_logs_client = LogsQueryClient(credential) async_metrics_client = MetricsQueryClient(credential) ``` #### Configure clients for non-public Azure clouds 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: ```python logs_client = LogsQueryClient(credential, endpoint="https://api.loganalytics.azure.cn/v1") metrics_client = MetricsQueryClient(credential, endpoint="https://management.chinacloudapi.cn") ``` **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. ### Execute the query For examples of Logs and Metrics queries, see the [Examples](#examples) section. ## Key concepts ### Logs query rate limits and throttling The Log Analytics service applies throttling when the request rate is too high. Limits, such as the maximum number of rows returned, are also applied on the Kusto queries. For more information, see [Query API](https://learn.microsoft.com/azure/azure-monitor/service-limits#la-query-api). If you're executing a batch logs query, a throttled request will return a `LogsQueryError` object. That object's `code` value will be `ThrottledError`. ### Metrics data structure Each set of metric values is a time series with the following characteristics: - The time the value was collected - The resource associated with the value - A namespace that acts like a category for the metric - A metric name - The value itself - Some metrics may have multiple dimensions as described in multi-dimensional metrics. Custom metrics can have up to 10 dimensions. ## Examples - [Logs query](#logs-query) - [Specify timespan](#specify-timespan) - [Handle logs query response](#handle-logs-query-response) - [Batch logs query](#batch-logs-query) - [Resource logs query](#resource-logs-query) - [Advanced logs query scenarios](#advanced-logs-query-scenarios) - [Set logs query timeout](#set-logs-query-timeout) - [Query multiple workspaces](#query-multiple-workspaces) - [Include statistics](#include-statistics) - [Include visualization](#include-visualization) - [Metrics query](#metrics-query) - [Handle metrics query response](#handle-metrics-query-response) - [Example of handling response](#example-of-handling-response) ### Logs query This example shows how to query a Log Analytics workspace. To handle the response and view it in a tabular form, the [pandas](https://pypi.org/project/pandas/) library is used. See the [samples][samples] if you choose not to use pandas. #### Specify timespan The `timespan` parameter specifies the time duration for which to query the data. This value can be one of the following: - a `timedelta` - a `timedelta` and a start datetime - a start datetime/end datetime For example: ```python import os import pandas as pd from datetime import datetime, timezone from azure.monitor.query import LogsQueryClient, LogsQueryStatus from azure.identity import DefaultAzureCredential from azure.core.exceptions import HttpResponseError credential = DefaultAzureCredential() client = LogsQueryClient(credential) query = """AppRequests | take 5""" start_time=datetime(2021, 7, 2, tzinfo=timezone.utc) end_time=datetime(2021, 7, 4, tzinfo=timezone.utc) try: response = client.query_workspace( workspace_id=os.environ['LOG_WORKSPACE_ID'], query=query, timespan=(start_time, end_time) ) if response.status == LogsQueryStatus.PARTIAL: error = response.partial_error data = response.partial_data print(error) elif response.status == LogsQueryStatus.SUCCESS: data = response.tables for table in data: df = pd.DataFrame(data=table.rows, columns=table.columns) print(df) except HttpResponseError as err: print("something fatal happened") print(err) ``` #### Handle logs query response The `query_workspace` API returns either a `LogsQueryResult` or a `LogsQueryPartialResult` object. The `batch_query` API returns a list that may contain `LogsQueryResult`, `LogsQueryPartialResult`, and `LogsQueryError` objects. Here's a hierarchy of the response: ``` LogsQueryResult |---statistics |---visualization |---tables (list of `LogsTable` objects) |---name |---rows |---columns |---columns_types LogsQueryPartialResult |---statistics |---visualization |---partial_error (a `LogsQueryError` object) |---code |---message |---details |---status |---partial_data (list of `LogsTable` objects) |---name |---rows |---columns |---columns_types ``` The `LogsQueryResult` directly iterates over the table as a convenience. For example, to handle a logs query response with tables and display it using pandas: ```python response = client.query(...) for table in response: df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_single_query.py). In a similar fashion, to handle a batch logs query response: ```python for result in response: if result.status == LogsQueryStatus.SUCCESS: for table in result: df = pd.DataFrame(table.rows, columns=table.columns) print(df) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py). ### Batch logs query The following example demonstrates sending multiple queries at the same time using the batch query API. The queries can either be represented as a list of `LogsBatchQuery` objects or a dictionary. This example uses the former approach. ```python import os from datetime import timedelta, datetime, timezone import pandas as pd from azure.monitor.query import LogsQueryClient, LogsBatchQuery, LogsQueryStatus from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) requests = [ LogsBatchQuery( query="AzureActivity | summarize count()", timespan=timedelta(hours=1), workspace_id=os.environ['LOG_WORKSPACE_ID'] ), LogsBatchQuery( query= """bad query""", timespan=timedelta(days=1), workspace_id=os.environ['LOG_WORKSPACE_ID'] ), LogsBatchQuery( query= """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""", workspace_id=os.environ['LOG_WORKSPACE_ID'], timespan=(datetime(2021, 6, 2, tzinfo=timezone.utc), datetime(2021, 6, 5, tzinfo=timezone.utc)), # (start, end) include_statistics=True ), ] results = client.query_batch(requests) for res in results: if res.status == LogsQueryStatus.FAILURE: # this will be a LogsQueryError print(res.message) elif res.status == LogsQueryStatus.PARTIAL: ## this will be a LogsQueryPartialResult print(res.partial_error) for table in res.partial_data: df = pd.DataFrame(table.rows, columns=table.columns) print(df) elif res.status == LogsQueryStatus.SUCCESS: ## this will be a LogsQueryResult table = res.tables[0] df = pd.DataFrame(table.rows, columns=table.columns) print(df) ``` ### Resource logs query The following example demonstrates how to query logs directly from an Azure resource without the use of a Log Analytics workspace. Here, the `query_resource` method is used instead of `query_workspace`, and instead of a workspace ID, an Azure resource identifier is passed in (e.g. `/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider}/{resource-type}/{resource-name}`). ```python import os import pandas as pd from datetime import timedelta from azure.monitor.query import LogsQueryClient, LogsQueryStatus from azure.core.exceptions import HttpResponseError from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) query = """AzureActivity | take 5""" try: response = client.query_resource(os.environ['LOGS_RESOURCE_ID'], query, timespan=timedelta(days=1)) if response.status == LogsQueryStatus.PARTIAL: error = response.partial_error data = response.partial_data print(error) elif response.status == LogsQueryStatus.SUCCESS: data = response.tables for table in data: df = pd.DataFrame(data=table.rows, columns=table.columns) print(df) except HttpResponseError as err: print("something fatal happened") print(err) ``` ### Advanced logs query scenarios #### Set logs query timeout The following example shows setting a server timeout in seconds. A gateway timeout is raised if the query takes more time than the mentioned timeout. The default is 180 seconds and can be set up to 10 minutes (600 seconds). ```python import os from azure.monitor.query import LogsQueryClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) response = client.query_workspace( os.environ['LOG_WORKSPACE_ID'], "range x from 1 to 10000000000 step 1 | count", timespan=timedelta(days=1), server_timeout=600 # sets the timeout to 10 minutes ) ``` #### Query multiple workspaces The same logs query can be executed across multiple Log Analytics workspaces. In addition to the Kusto query, the following parameters are required: - `workspace_id` - The first (primary) workspace ID. - `additional_workspaces` - A list of workspaces, excluding the workspace provided in the `workspace_id` parameter. The parameter's list items may consist of the following identifier formats: - Qualified workspace names - Workspace IDs - Azure resource IDs For example, the following query executes in three workspaces: ```python client.query_workspace( , query, timespan=timedelta(days=1), additional_workspaces=['', ''] ) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py). #### Include statistics To get logs query execution statistics, such as CPU and memory consumption: 1. Set the `include_statistics` parameter to `True`. 2. Access the `statistics` field inside the `LogsQueryResult` object. The following example prints the query execution time: ```python query = "AzureActivity | top 10 by TimeGenerated" result = client.query_workspace( , query, timespan=timedelta(days=1), include_statistics=True ) execution_time = result.statistics.get("query", {}).get("executionTime") print(f"Query execution time: {execution_time}") ``` The `statistics` field is a `dict` that corresponds to the raw JSON response, and its structure can vary by query. The statistics are found within the `query` property. For example: ```python { "query": { "executionTime": 0.0156478, "resourceUsage": {...}, "inputDatasetStatistics": {...}, "datasetStatistics": [{...}] } } ``` #### Include visualization To get visualization data for logs queries using the [render operator](https://docs.microsoft.com/azure/data-explorer/kusto/query/renderoperator?pivots=azuremonitor): 1. Set the `include_visualization` property to `True`. 1. Access the `visualization` field inside the `LogsQueryResult` object. For example: ```python query = ( "StormEvents" "| summarize event_count = count() by State" "| where event_count > 10" "| project State, event_count" "| render columnchart" ) result = client.query_workspace( , query, timespan=timedelta(days=1), include_visualization=True ) print(f"Visualization result: {result.visualization}") ``` The `visualization` field is a `dict` that corresponds to the raw JSON response, and its structure can vary by query. For example: ```python { "visualization": "columnchart", "title": "the chart title", "accumulate": False, "isQuerySorted": False, "kind": None, "legend": None, "series": None, "yMin": "NaN", "yMax": "NaN", "xAxis": None, "xColumn": None, "xTitle": "x axis title", "yAxis": None, "yColumns": None, "ySplit": None, "yTitle": None, "anomalyColumns": None } ``` ### Metrics query The following example gets metrics for an Event Grid subscription. The resource URI is that of an Event Grid topic. The resource URI must be that of the resource for which metrics are being queried. It's normally of the format `/subscriptions//resourceGroups//providers//topics/`. To find the resource URI: 1. Navigate to your resource's page in the Azure portal. 2. From the **Overview** blade, select the **JSON View** link. 3. In the resulting JSON, copy the value of the `id` property. **NOTE**: The metrics are returned in the order of the metric_names sent. ```python import os from datetime import timedelta, datetime from azure.monitor.query import MetricsQueryClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = MetricsQueryClient(credential) start_time = datetime(2021, 5, 25) duration = timedelta(days=1) metrics_uri = os.environ['METRICS_RESOURCE_URI'] response = client.query_resource( metrics_uri, metric_names=["PublishSuccessCount"], timespan=(start_time, duration) ) for metric in response.metrics: print(metric.name) for time_series_element in metric.timeseries: for metric_value in time_series_element.data: print(metric_value.time_stamp) ``` #### Handle metrics query response The metrics query API returns a `MetricsQueryResult` object. The `MetricsQueryResult` object contains properties such as a list of `Metric`-typed objects, `granularity`, `namespace`, and `timespan`. The `Metric` objects list can be accessed using the `metrics` param. Each `Metric` object in this list contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` object contains `data` and `metadata_values` properties. In visual form, the object hierarchy of the response resembles the following structure: ``` MetricsQueryResult |---granularity |---timespan |---cost |---namespace |---resource_region |---metrics (list of `Metric` objects) |---id |---type |---name |---unit |---timeseries (list of `TimeSeriesElement` objects) |---metadata_values |---data (list of data points represented by `MetricValue` objects) ``` #### Example of handling response ```python import os from azure.monitor.query import MetricsQueryClient, MetricAggregationType from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = MetricsQueryClient(credential) metrics_uri = os.environ['METRICS_RESOURCE_URI'] response = client.query_resource( metrics_uri, metric_names=["MatchedEventCount"], aggregations=[MetricAggregationType.COUNT] ) for metric in response.metrics: print(metric.name) for time_series_element in metric.timeseries: for metric_value in time_series_element.data: if metric_value.count != 0: print( "There are {} matched events at {}".format( metric_value.count, metric_value.time_stamp ) ) ``` ## Troubleshooting See our [troubleshooting guide][troubleshooting_guide] for details on how to diagnose various failure scenarios. ## Next steps To learn more about Azure Monitor, see the [Azure Monitor service documentation][azure_monitor_overview]. ### Samples The following code samples show common scenarios with the Azure Monitor Query client library. #### Logs query samples - [Send a single query with LogsQueryClient and handle the response as a table](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_single_query.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_async.py)) - [Send a single query with LogsQueryClient and handle the response in key-value form](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_query_key_value_form.py) - [Send a single query with LogsQueryClient without pandas](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_single_log_query_without_pandas.py) - [Send a single query with LogsQueryClient across multiple workspaces](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py) - [Send multiple queries with LogsQueryClient](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py) - [Send a single query with LogsQueryClient using server timeout](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_server_timeout.py) #### Metrics query samples - [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)) - [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)) - [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)) ## Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit [cla.microsoft.com][cla]. When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA. This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments. [azure_core_exceptions]: https://aka.ms/azsdk/python/core/docs#module-azure.core.exceptions [azure_core_ref_docs]: https://aka.ms/azsdk/python/core/docs [azure_monitor_create_using_portal]: https://learn.microsoft.com/azure/azure-monitor/logs/quick-create-workspace [azure_monitor_overview]: https://learn.microsoft.com/azure/azure-monitor/ [azure_subscription]: https://azure.microsoft.com/free/python/ [changelog]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-query/CHANGELOG.md [kusto_query_language]: https://learn.microsoft.com/azure/data-explorer/kusto/query/ [package]: https://aka.ms/azsdk-python-monitor-query-pypi [pip]: https://pypi.org/project/pip/ [python_logging]: https://docs.python.org/3/library/logging.html [python-query-ref-docs]: https://aka.ms/azsdk/python/monitor-query/docs [samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-query/samples [source]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/ [troubleshooting_guide]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md [cla]: https://cla.microsoft.com [code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ [coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ [coc_contact]: mailto:opencode@microsoft.com # Release History ## 1.2.0 (2023-05-09) ### Features Added - Add the `query_resource` method to `LogsQueryClient` to allow users to query Azure resources directly without the context of a workspace. ([#29365](https://github.com/Azure/azure-sdk-for-python/pull/29365)) ### Bugs Fixed - Fixed an inconsistent keyword argument name in the `LogsTable` constructor, changing `column_types` to `columns_types`. Note that this is a class that is typically only instantiated internally, and not by users. ([#29076](https://github.com/Azure/azure-sdk-for-python/pull/29076)) ### Other Changes - Improved client configuration logic for non-public Azure clouds where credential scope will be determined based on the configured endpoint. ([#29602](https://github.com/Azure/azure-sdk-for-python/pull/29602)) ## 1.1.1 (2023-02-13) ### Bugs Fixed - Fixed a bug where the incorrect key `time_stamp` (should be `timeStamp`) was used in the creation of `MetricValue` objects (thanks @jamespic). ([#28777](https://github.com/Azure/azure-sdk-for-python/pull/28777)) ## 1.1.0 (2023-02-07) ### Bugs Fixed * Error details are now propagated inside the `LogsQueryError` object. ([#25137](https://github.com/Azure/azure-sdk-for-python/issues/25137)) ### Other Changes * Python 3.6 is no longer supported. Please use Python version 3.7 or later. For more details, see [Azure SDK for Python version support policy](https://github.com/Azure/azure-sdk-for-python/wiki/Azure-SDKs-Python-version-support-policy). * Removed `msrest` dependency. * Bumped minimum dependency on `azure-core` to `>=1.24.0`. * Added requirement for `isodate>=0.6.0` (`isodate` was required by `msrest`). * Added requirement for `typing-extensions>=4.0.1`. ## 1.0.3 (2022-07-07) ### Bugs Fixed - Fixed a bug where `query_resource` in metrics client is throwing an error with unexpected `metric_namespace` argument. ## 1.0.2 (2022-05-06) - This version and all future versions will require Python 3.6+. Python 2.7 is no longer supported. ### Bugs Fixed - Fixed a bug where having a None value in datetime throws ## 1.0.1 (2021-11-09) ### Bugs Fixed - Fixed a bug where Metadata values in timestamp don't show up sometimes. ## 1.0.0 (2021-10-06) ### Features Added - Added `LogsQueryPartialResult` and `LogsQueryError` to handle errors. - Added `status` attribute to `LogsQueryResult`. - Added `LogsQueryStatus` Enum to describe the status of a result. - Added a new `LogsTableRow` type that represents a single row in a table. - Items in `metrics` list in `MetricsQueryResult` can now be accessed by metric names. ### Breaking Changes - `LogsQueryResult` now iterates over the tables directly as a convenience. - `query` API in logs is renamed to `query_workspace` - `query` API in metrics is renamed to `query_resource` - `query_workspace` API now returns a union of `LogsQueryPartialResult` and `LogsQueryResult`. - `query_batch` API now returns a union of `LogsQueryPartialResult`, `LogsQueryError` and `LogsQueryResult`. - `metric_namespace` is renamed to `namespace` and is a keyword-only argument in `list_metric_definitions` API. - `MetricsResult` is renamed to `MetricsQueryResult`. ## 1.0.0b4 (2021-09-09) ### 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. - Added a `MetricUnit` enum to describe the unit of the metric. ### Breaking Changes - Rename `batch_query` to `query_batch`. - Rename `LogsBatchQueryRequest` to `LogsBatchQuery`. - `include_render` is now renamed to `include_visualization` in the query API. - `LogsQueryResult` now returns `visualization` instead of `render`. - `start_time`, `duration` and `end_time` are now replaced with a single param called `timespan` - `resourceregion` is renamed to `resource_region` in the MetricResult type. - `top` is renamed to `max_results` in the metric's `query` API. - `metric_namespace_name` is renamed to `fully_qualified_namespace` - `is_dimension_required` is renamed to `dimension_required` - `interval` and `time_grain` are renamed to `granularity` - `orderby` is renamed to `order_by` - `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` - `LogsTableColumn` is now removed. Column labels are strings instead. - `start_time` in `list_metric_namespaces` API is now a datetime. - The order of params in `LogsBatchQuery` is changed. Also, `headers` is no longer accepted. - `timespan` is now a required keyword-only argument in logs APIs. - batch api now returns a list of `LogsQueryResult` objects. ### Bugs Fixed - `include_statistics` and `include_visualization` args can now work together. ## 1.0.0b3 (2021-08-09) ### Features Added - Added enum `AggregationType` which can be used to specify aggregations in the query API. - Added `LogsBatchQueryResult` model that is returned for a logs batch query. - Added `error` attribute to `LogsQueryResult`. ### Breaking Changes - `aggregation` param in the query API is renamed to `aggregations` - `batch_query` API now returns a list of responses. - `LogsBatchResults` model is now removed. - `LogsQueryRequest` is renamed to `LogsBatchQueryRequest` - `LogsQueryResults` is now renamed to `LogsQueryResult` - `LogsBatchQueryResult` now has 4 additional attributes - `tables`, `error`, `statistics` and `render` instead of `body` attribute. ## 1.0.0b2 (2021-07-06) ### Breaking Changes - `workspaces`, `workspace_ids`, `qualified_names` and `azure_resource_ids` are now merged into a single `additional_workspaces` list in the query API. - The `LogQueryRequest` object now takes in a `workspace_id` and `additional_workspaces` instead of `workspace`. - `aggregation` param is now a list instead of a string in the `query` method. - `duration` must now be provided as a timedelta instead of a string. ## 1.0.0b1 (2021-06-10) **Features** - Version (1.0.0b1) is the first preview of our efforts to create a user-friendly and Pythonic client library for Azure Monitor Query. For more information about this, and preview releases of other Azure SDK libraries, please visit https://azure.github.io/azure-sdk/releases/latest/python.html. - Added `~azure.monitor.query.LogsQueryClient` to query log analytics along with `~azure.monitor.query.aio.LogsQueryClient`. - Implements the `~azure.monitor.query.MetricsQueryClient` for querying metrics, listing namespaces and metric definitions along with `~azure.monitor.query.aio.MetricsQueryClient`. %package -n python3-azure-monitor-query Summary: Microsoft Azure Monitor Query Client Library for Python Provides: python-azure-monitor-query BuildRequires: python3-devel BuildRequires: python3-setuptools BuildRequires: python3-pip %description -n python3-azure-monitor-query # Azure Monitor Query client library for Python The Azure Monitor Query client library is used to execute read-only queries against [Azure Monitor][azure_monitor_overview]'s two data platforms: - [Logs](https://learn.microsoft.com/azure/azure-monitor/logs/data-platform-logs) - Collects and organizes log and performance data from monitored resources. Data from different sources such as platform logs from Azure services, log and performance data from virtual machines agents, and usage and performance data from apps can be consolidated into a single [Azure Log Analytics workspace](https://learn.microsoft.com/azure/azure-monitor/logs/data-platform-logs#log-analytics-and-workspaces). The various data types can be analyzed together using the [Kusto Query Language][kusto_query_language]. - [Metrics](https://learn.microsoft.com/azure/azure-monitor/essentials/data-platform-metrics) - Collects numeric data from monitored resources into a time series database. Metrics are numerical values that are collected at regular intervals and describe some aspect of a system at a particular time. Metrics are lightweight and capable of supporting near real-time scenarios, making them useful for alerting and fast detection of issues. **Resources:** - [Source code][source] - [Package (PyPI)][package] - [Package (Conda)](https://anaconda.org/microsoft/azure-monitor-query/) - [API reference documentation][python-query-ref-docs] - [Service documentation][azure_monitor_overview] - [Samples][samples] - [Change log][changelog] ## Getting started ### Prerequisites - Python 3.7 or later - An [Azure subscription][azure_subscription] - 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). - To query Logs, you need an [Azure Log Analytics workspace][azure_monitor_create_using_portal]. - To query Metrics, you need an Azure resource of any kind (Storage Account, Key Vault, Cosmos DB, etc.). ### Install the package Install the Azure Monitor Query client library for Python with [pip][pip]: ```bash pip install azure-monitor-query ``` ### Create the client 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` or `MetricsQueryClient`. The following examples use `DefaultAzureCredential` from the [azure-identity](https://pypi.org/project/azure-identity/) package. #### Synchronous clients Consider the following example, which creates synchronous clients for both Logs and Metrics querying: ```python from azure.identity import DefaultAzureCredential from azure.monitor.query import LogsQueryClient, MetricsQueryClient credential = DefaultAzureCredential() logs_client = LogsQueryClient(credential) metrics_client = MetricsQueryClient(credential) ``` #### Asynchronous clients The asynchronous forms of the query client APIs are found in the `.aio`-suffixed namespace. For example: ```python from azure.identity.aio import DefaultAzureCredential from azure.monitor.query.aio import LogsQueryClient, MetricsQueryClient credential = DefaultAzureCredential() async_logs_client = LogsQueryClient(credential) async_metrics_client = MetricsQueryClient(credential) ``` #### Configure clients for non-public Azure clouds 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: ```python logs_client = LogsQueryClient(credential, endpoint="https://api.loganalytics.azure.cn/v1") metrics_client = MetricsQueryClient(credential, endpoint="https://management.chinacloudapi.cn") ``` **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. ### Execute the query For examples of Logs and Metrics queries, see the [Examples](#examples) section. ## Key concepts ### Logs query rate limits and throttling The Log Analytics service applies throttling when the request rate is too high. Limits, such as the maximum number of rows returned, are also applied on the Kusto queries. For more information, see [Query API](https://learn.microsoft.com/azure/azure-monitor/service-limits#la-query-api). If you're executing a batch logs query, a throttled request will return a `LogsQueryError` object. That object's `code` value will be `ThrottledError`. ### Metrics data structure Each set of metric values is a time series with the following characteristics: - The time the value was collected - The resource associated with the value - A namespace that acts like a category for the metric - A metric name - The value itself - Some metrics may have multiple dimensions as described in multi-dimensional metrics. Custom metrics can have up to 10 dimensions. ## Examples - [Logs query](#logs-query) - [Specify timespan](#specify-timespan) - [Handle logs query response](#handle-logs-query-response) - [Batch logs query](#batch-logs-query) - [Resource logs query](#resource-logs-query) - [Advanced logs query scenarios](#advanced-logs-query-scenarios) - [Set logs query timeout](#set-logs-query-timeout) - [Query multiple workspaces](#query-multiple-workspaces) - [Include statistics](#include-statistics) - [Include visualization](#include-visualization) - [Metrics query](#metrics-query) - [Handle metrics query response](#handle-metrics-query-response) - [Example of handling response](#example-of-handling-response) ### Logs query This example shows how to query a Log Analytics workspace. To handle the response and view it in a tabular form, the [pandas](https://pypi.org/project/pandas/) library is used. See the [samples][samples] if you choose not to use pandas. #### Specify timespan The `timespan` parameter specifies the time duration for which to query the data. This value can be one of the following: - a `timedelta` - a `timedelta` and a start datetime - a start datetime/end datetime For example: ```python import os import pandas as pd from datetime import datetime, timezone from azure.monitor.query import LogsQueryClient, LogsQueryStatus from azure.identity import DefaultAzureCredential from azure.core.exceptions import HttpResponseError credential = DefaultAzureCredential() client = LogsQueryClient(credential) query = """AppRequests | take 5""" start_time=datetime(2021, 7, 2, tzinfo=timezone.utc) end_time=datetime(2021, 7, 4, tzinfo=timezone.utc) try: response = client.query_workspace( workspace_id=os.environ['LOG_WORKSPACE_ID'], query=query, timespan=(start_time, end_time) ) if response.status == LogsQueryStatus.PARTIAL: error = response.partial_error data = response.partial_data print(error) elif response.status == LogsQueryStatus.SUCCESS: data = response.tables for table in data: df = pd.DataFrame(data=table.rows, columns=table.columns) print(df) except HttpResponseError as err: print("something fatal happened") print(err) ``` #### Handle logs query response The `query_workspace` API returns either a `LogsQueryResult` or a `LogsQueryPartialResult` object. The `batch_query` API returns a list that may contain `LogsQueryResult`, `LogsQueryPartialResult`, and `LogsQueryError` objects. Here's a hierarchy of the response: ``` LogsQueryResult |---statistics |---visualization |---tables (list of `LogsTable` objects) |---name |---rows |---columns |---columns_types LogsQueryPartialResult |---statistics |---visualization |---partial_error (a `LogsQueryError` object) |---code |---message |---details |---status |---partial_data (list of `LogsTable` objects) |---name |---rows |---columns |---columns_types ``` The `LogsQueryResult` directly iterates over the table as a convenience. For example, to handle a logs query response with tables and display it using pandas: ```python response = client.query(...) for table in response: df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_single_query.py). In a similar fashion, to handle a batch logs query response: ```python for result in response: if result.status == LogsQueryStatus.SUCCESS: for table in result: df = pd.DataFrame(table.rows, columns=table.columns) print(df) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py). ### Batch logs query The following example demonstrates sending multiple queries at the same time using the batch query API. The queries can either be represented as a list of `LogsBatchQuery` objects or a dictionary. This example uses the former approach. ```python import os from datetime import timedelta, datetime, timezone import pandas as pd from azure.monitor.query import LogsQueryClient, LogsBatchQuery, LogsQueryStatus from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) requests = [ LogsBatchQuery( query="AzureActivity | summarize count()", timespan=timedelta(hours=1), workspace_id=os.environ['LOG_WORKSPACE_ID'] ), LogsBatchQuery( query= """bad query""", timespan=timedelta(days=1), workspace_id=os.environ['LOG_WORKSPACE_ID'] ), LogsBatchQuery( query= """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""", workspace_id=os.environ['LOG_WORKSPACE_ID'], timespan=(datetime(2021, 6, 2, tzinfo=timezone.utc), datetime(2021, 6, 5, tzinfo=timezone.utc)), # (start, end) include_statistics=True ), ] results = client.query_batch(requests) for res in results: if res.status == LogsQueryStatus.FAILURE: # this will be a LogsQueryError print(res.message) elif res.status == LogsQueryStatus.PARTIAL: ## this will be a LogsQueryPartialResult print(res.partial_error) for table in res.partial_data: df = pd.DataFrame(table.rows, columns=table.columns) print(df) elif res.status == LogsQueryStatus.SUCCESS: ## this will be a LogsQueryResult table = res.tables[0] df = pd.DataFrame(table.rows, columns=table.columns) print(df) ``` ### Resource logs query The following example demonstrates how to query logs directly from an Azure resource without the use of a Log Analytics workspace. Here, the `query_resource` method is used instead of `query_workspace`, and instead of a workspace ID, an Azure resource identifier is passed in (e.g. `/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider}/{resource-type}/{resource-name}`). ```python import os import pandas as pd from datetime import timedelta from azure.monitor.query import LogsQueryClient, LogsQueryStatus from azure.core.exceptions import HttpResponseError from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) query = """AzureActivity | take 5""" try: response = client.query_resource(os.environ['LOGS_RESOURCE_ID'], query, timespan=timedelta(days=1)) if response.status == LogsQueryStatus.PARTIAL: error = response.partial_error data = response.partial_data print(error) elif response.status == LogsQueryStatus.SUCCESS: data = response.tables for table in data: df = pd.DataFrame(data=table.rows, columns=table.columns) print(df) except HttpResponseError as err: print("something fatal happened") print(err) ``` ### Advanced logs query scenarios #### Set logs query timeout The following example shows setting a server timeout in seconds. A gateway timeout is raised if the query takes more time than the mentioned timeout. The default is 180 seconds and can be set up to 10 minutes (600 seconds). ```python import os from azure.monitor.query import LogsQueryClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) response = client.query_workspace( os.environ['LOG_WORKSPACE_ID'], "range x from 1 to 10000000000 step 1 | count", timespan=timedelta(days=1), server_timeout=600 # sets the timeout to 10 minutes ) ``` #### Query multiple workspaces The same logs query can be executed across multiple Log Analytics workspaces. In addition to the Kusto query, the following parameters are required: - `workspace_id` - The first (primary) workspace ID. - `additional_workspaces` - A list of workspaces, excluding the workspace provided in the `workspace_id` parameter. The parameter's list items may consist of the following identifier formats: - Qualified workspace names - Workspace IDs - Azure resource IDs For example, the following query executes in three workspaces: ```python client.query_workspace( , query, timespan=timedelta(days=1), additional_workspaces=['', ''] ) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py). #### Include statistics To get logs query execution statistics, such as CPU and memory consumption: 1. Set the `include_statistics` parameter to `True`. 2. Access the `statistics` field inside the `LogsQueryResult` object. The following example prints the query execution time: ```python query = "AzureActivity | top 10 by TimeGenerated" result = client.query_workspace( , query, timespan=timedelta(days=1), include_statistics=True ) execution_time = result.statistics.get("query", {}).get("executionTime") print(f"Query execution time: {execution_time}") ``` The `statistics` field is a `dict` that corresponds to the raw JSON response, and its structure can vary by query. The statistics are found within the `query` property. For example: ```python { "query": { "executionTime": 0.0156478, "resourceUsage": {...}, "inputDatasetStatistics": {...}, "datasetStatistics": [{...}] } } ``` #### Include visualization To get visualization data for logs queries using the [render operator](https://docs.microsoft.com/azure/data-explorer/kusto/query/renderoperator?pivots=azuremonitor): 1. Set the `include_visualization` property to `True`. 1. Access the `visualization` field inside the `LogsQueryResult` object. For example: ```python query = ( "StormEvents" "| summarize event_count = count() by State" "| where event_count > 10" "| project State, event_count" "| render columnchart" ) result = client.query_workspace( , query, timespan=timedelta(days=1), include_visualization=True ) print(f"Visualization result: {result.visualization}") ``` The `visualization` field is a `dict` that corresponds to the raw JSON response, and its structure can vary by query. For example: ```python { "visualization": "columnchart", "title": "the chart title", "accumulate": False, "isQuerySorted": False, "kind": None, "legend": None, "series": None, "yMin": "NaN", "yMax": "NaN", "xAxis": None, "xColumn": None, "xTitle": "x axis title", "yAxis": None, "yColumns": None, "ySplit": None, "yTitle": None, "anomalyColumns": None } ``` ### Metrics query The following example gets metrics for an Event Grid subscription. The resource URI is that of an Event Grid topic. The resource URI must be that of the resource for which metrics are being queried. It's normally of the format `/subscriptions//resourceGroups//providers//topics/`. To find the resource URI: 1. Navigate to your resource's page in the Azure portal. 2. From the **Overview** blade, select the **JSON View** link. 3. In the resulting JSON, copy the value of the `id` property. **NOTE**: The metrics are returned in the order of the metric_names sent. ```python import os from datetime import timedelta, datetime from azure.monitor.query import MetricsQueryClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = MetricsQueryClient(credential) start_time = datetime(2021, 5, 25) duration = timedelta(days=1) metrics_uri = os.environ['METRICS_RESOURCE_URI'] response = client.query_resource( metrics_uri, metric_names=["PublishSuccessCount"], timespan=(start_time, duration) ) for metric in response.metrics: print(metric.name) for time_series_element in metric.timeseries: for metric_value in time_series_element.data: print(metric_value.time_stamp) ``` #### Handle metrics query response The metrics query API returns a `MetricsQueryResult` object. The `MetricsQueryResult` object contains properties such as a list of `Metric`-typed objects, `granularity`, `namespace`, and `timespan`. The `Metric` objects list can be accessed using the `metrics` param. Each `Metric` object in this list contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` object contains `data` and `metadata_values` properties. In visual form, the object hierarchy of the response resembles the following structure: ``` MetricsQueryResult |---granularity |---timespan |---cost |---namespace |---resource_region |---metrics (list of `Metric` objects) |---id |---type |---name |---unit |---timeseries (list of `TimeSeriesElement` objects) |---metadata_values |---data (list of data points represented by `MetricValue` objects) ``` #### Example of handling response ```python import os from azure.monitor.query import MetricsQueryClient, MetricAggregationType from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = MetricsQueryClient(credential) metrics_uri = os.environ['METRICS_RESOURCE_URI'] response = client.query_resource( metrics_uri, metric_names=["MatchedEventCount"], aggregations=[MetricAggregationType.COUNT] ) for metric in response.metrics: print(metric.name) for time_series_element in metric.timeseries: for metric_value in time_series_element.data: if metric_value.count != 0: print( "There are {} matched events at {}".format( metric_value.count, metric_value.time_stamp ) ) ``` ## Troubleshooting See our [troubleshooting guide][troubleshooting_guide] for details on how to diagnose various failure scenarios. ## Next steps To learn more about Azure Monitor, see the [Azure Monitor service documentation][azure_monitor_overview]. ### Samples The following code samples show common scenarios with the Azure Monitor Query client library. #### Logs query samples - [Send a single query with LogsQueryClient and handle the response as a table](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_single_query.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_async.py)) - [Send a single query with LogsQueryClient and handle the response in key-value form](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_query_key_value_form.py) - [Send a single query with LogsQueryClient without pandas](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_single_log_query_without_pandas.py) - [Send a single query with LogsQueryClient across multiple workspaces](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py) - [Send multiple queries with LogsQueryClient](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py) - [Send a single query with LogsQueryClient using server timeout](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_server_timeout.py) #### Metrics query samples - [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)) - [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)) - [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)) ## Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit [cla.microsoft.com][cla]. When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA. This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments. [azure_core_exceptions]: https://aka.ms/azsdk/python/core/docs#module-azure.core.exceptions [azure_core_ref_docs]: https://aka.ms/azsdk/python/core/docs [azure_monitor_create_using_portal]: https://learn.microsoft.com/azure/azure-monitor/logs/quick-create-workspace [azure_monitor_overview]: https://learn.microsoft.com/azure/azure-monitor/ [azure_subscription]: https://azure.microsoft.com/free/python/ [changelog]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-query/CHANGELOG.md [kusto_query_language]: https://learn.microsoft.com/azure/data-explorer/kusto/query/ [package]: https://aka.ms/azsdk-python-monitor-query-pypi [pip]: https://pypi.org/project/pip/ [python_logging]: https://docs.python.org/3/library/logging.html [python-query-ref-docs]: https://aka.ms/azsdk/python/monitor-query/docs [samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-query/samples [source]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/ [troubleshooting_guide]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md [cla]: https://cla.microsoft.com [code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ [coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ [coc_contact]: mailto:opencode@microsoft.com # Release History ## 1.2.0 (2023-05-09) ### Features Added - Add the `query_resource` method to `LogsQueryClient` to allow users to query Azure resources directly without the context of a workspace. ([#29365](https://github.com/Azure/azure-sdk-for-python/pull/29365)) ### Bugs Fixed - Fixed an inconsistent keyword argument name in the `LogsTable` constructor, changing `column_types` to `columns_types`. Note that this is a class that is typically only instantiated internally, and not by users. ([#29076](https://github.com/Azure/azure-sdk-for-python/pull/29076)) ### Other Changes - Improved client configuration logic for non-public Azure clouds where credential scope will be determined based on the configured endpoint. ([#29602](https://github.com/Azure/azure-sdk-for-python/pull/29602)) ## 1.1.1 (2023-02-13) ### Bugs Fixed - Fixed a bug where the incorrect key `time_stamp` (should be `timeStamp`) was used in the creation of `MetricValue` objects (thanks @jamespic). ([#28777](https://github.com/Azure/azure-sdk-for-python/pull/28777)) ## 1.1.0 (2023-02-07) ### Bugs Fixed * Error details are now propagated inside the `LogsQueryError` object. ([#25137](https://github.com/Azure/azure-sdk-for-python/issues/25137)) ### Other Changes * Python 3.6 is no longer supported. Please use Python version 3.7 or later. For more details, see [Azure SDK for Python version support policy](https://github.com/Azure/azure-sdk-for-python/wiki/Azure-SDKs-Python-version-support-policy). * Removed `msrest` dependency. * Bumped minimum dependency on `azure-core` to `>=1.24.0`. * Added requirement for `isodate>=0.6.0` (`isodate` was required by `msrest`). * Added requirement for `typing-extensions>=4.0.1`. ## 1.0.3 (2022-07-07) ### Bugs Fixed - Fixed a bug where `query_resource` in metrics client is throwing an error with unexpected `metric_namespace` argument. ## 1.0.2 (2022-05-06) - This version and all future versions will require Python 3.6+. Python 2.7 is no longer supported. ### Bugs Fixed - Fixed a bug where having a None value in datetime throws ## 1.0.1 (2021-11-09) ### Bugs Fixed - Fixed a bug where Metadata values in timestamp don't show up sometimes. ## 1.0.0 (2021-10-06) ### Features Added - Added `LogsQueryPartialResult` and `LogsQueryError` to handle errors. - Added `status` attribute to `LogsQueryResult`. - Added `LogsQueryStatus` Enum to describe the status of a result. - Added a new `LogsTableRow` type that represents a single row in a table. - Items in `metrics` list in `MetricsQueryResult` can now be accessed by metric names. ### Breaking Changes - `LogsQueryResult` now iterates over the tables directly as a convenience. - `query` API in logs is renamed to `query_workspace` - `query` API in metrics is renamed to `query_resource` - `query_workspace` API now returns a union of `LogsQueryPartialResult` and `LogsQueryResult`. - `query_batch` API now returns a union of `LogsQueryPartialResult`, `LogsQueryError` and `LogsQueryResult`. - `metric_namespace` is renamed to `namespace` and is a keyword-only argument in `list_metric_definitions` API. - `MetricsResult` is renamed to `MetricsQueryResult`. ## 1.0.0b4 (2021-09-09) ### 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. - Added a `MetricUnit` enum to describe the unit of the metric. ### Breaking Changes - Rename `batch_query` to `query_batch`. - Rename `LogsBatchQueryRequest` to `LogsBatchQuery`. - `include_render` is now renamed to `include_visualization` in the query API. - `LogsQueryResult` now returns `visualization` instead of `render`. - `start_time`, `duration` and `end_time` are now replaced with a single param called `timespan` - `resourceregion` is renamed to `resource_region` in the MetricResult type. - `top` is renamed to `max_results` in the metric's `query` API. - `metric_namespace_name` is renamed to `fully_qualified_namespace` - `is_dimension_required` is renamed to `dimension_required` - `interval` and `time_grain` are renamed to `granularity` - `orderby` is renamed to `order_by` - `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` - `LogsTableColumn` is now removed. Column labels are strings instead. - `start_time` in `list_metric_namespaces` API is now a datetime. - The order of params in `LogsBatchQuery` is changed. Also, `headers` is no longer accepted. - `timespan` is now a required keyword-only argument in logs APIs. - batch api now returns a list of `LogsQueryResult` objects. ### Bugs Fixed - `include_statistics` and `include_visualization` args can now work together. ## 1.0.0b3 (2021-08-09) ### Features Added - Added enum `AggregationType` which can be used to specify aggregations in the query API. - Added `LogsBatchQueryResult` model that is returned for a logs batch query. - Added `error` attribute to `LogsQueryResult`. ### Breaking Changes - `aggregation` param in the query API is renamed to `aggregations` - `batch_query` API now returns a list of responses. - `LogsBatchResults` model is now removed. - `LogsQueryRequest` is renamed to `LogsBatchQueryRequest` - `LogsQueryResults` is now renamed to `LogsQueryResult` - `LogsBatchQueryResult` now has 4 additional attributes - `tables`, `error`, `statistics` and `render` instead of `body` attribute. ## 1.0.0b2 (2021-07-06) ### Breaking Changes - `workspaces`, `workspace_ids`, `qualified_names` and `azure_resource_ids` are now merged into a single `additional_workspaces` list in the query API. - The `LogQueryRequest` object now takes in a `workspace_id` and `additional_workspaces` instead of `workspace`. - `aggregation` param is now a list instead of a string in the `query` method. - `duration` must now be provided as a timedelta instead of a string. ## 1.0.0b1 (2021-06-10) **Features** - Version (1.0.0b1) is the first preview of our efforts to create a user-friendly and Pythonic client library for Azure Monitor Query. For more information about this, and preview releases of other Azure SDK libraries, please visit https://azure.github.io/azure-sdk/releases/latest/python.html. - Added `~azure.monitor.query.LogsQueryClient` to query log analytics along with `~azure.monitor.query.aio.LogsQueryClient`. - Implements the `~azure.monitor.query.MetricsQueryClient` for querying metrics, listing namespaces and metric definitions along with `~azure.monitor.query.aio.MetricsQueryClient`. %package help Summary: Development documents and examples for azure-monitor-query Provides: python3-azure-monitor-query-doc %description help # Azure Monitor Query client library for Python The Azure Monitor Query client library is used to execute read-only queries against [Azure Monitor][azure_monitor_overview]'s two data platforms: - [Logs](https://learn.microsoft.com/azure/azure-monitor/logs/data-platform-logs) - Collects and organizes log and performance data from monitored resources. Data from different sources such as platform logs from Azure services, log and performance data from virtual machines agents, and usage and performance data from apps can be consolidated into a single [Azure Log Analytics workspace](https://learn.microsoft.com/azure/azure-monitor/logs/data-platform-logs#log-analytics-and-workspaces). The various data types can be analyzed together using the [Kusto Query Language][kusto_query_language]. - [Metrics](https://learn.microsoft.com/azure/azure-monitor/essentials/data-platform-metrics) - Collects numeric data from monitored resources into a time series database. Metrics are numerical values that are collected at regular intervals and describe some aspect of a system at a particular time. Metrics are lightweight and capable of supporting near real-time scenarios, making them useful for alerting and fast detection of issues. **Resources:** - [Source code][source] - [Package (PyPI)][package] - [Package (Conda)](https://anaconda.org/microsoft/azure-monitor-query/) - [API reference documentation][python-query-ref-docs] - [Service documentation][azure_monitor_overview] - [Samples][samples] - [Change log][changelog] ## Getting started ### Prerequisites - Python 3.7 or later - An [Azure subscription][azure_subscription] - 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). - To query Logs, you need an [Azure Log Analytics workspace][azure_monitor_create_using_portal]. - To query Metrics, you need an Azure resource of any kind (Storage Account, Key Vault, Cosmos DB, etc.). ### Install the package Install the Azure Monitor Query client library for Python with [pip][pip]: ```bash pip install azure-monitor-query ``` ### Create the client 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` or `MetricsQueryClient`. The following examples use `DefaultAzureCredential` from the [azure-identity](https://pypi.org/project/azure-identity/) package. #### Synchronous clients Consider the following example, which creates synchronous clients for both Logs and Metrics querying: ```python from azure.identity import DefaultAzureCredential from azure.monitor.query import LogsQueryClient, MetricsQueryClient credential = DefaultAzureCredential() logs_client = LogsQueryClient(credential) metrics_client = MetricsQueryClient(credential) ``` #### Asynchronous clients The asynchronous forms of the query client APIs are found in the `.aio`-suffixed namespace. For example: ```python from azure.identity.aio import DefaultAzureCredential from azure.monitor.query.aio import LogsQueryClient, MetricsQueryClient credential = DefaultAzureCredential() async_logs_client = LogsQueryClient(credential) async_metrics_client = MetricsQueryClient(credential) ``` #### Configure clients for non-public Azure clouds 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: ```python logs_client = LogsQueryClient(credential, endpoint="https://api.loganalytics.azure.cn/v1") metrics_client = MetricsQueryClient(credential, endpoint="https://management.chinacloudapi.cn") ``` **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. ### Execute the query For examples of Logs and Metrics queries, see the [Examples](#examples) section. ## Key concepts ### Logs query rate limits and throttling The Log Analytics service applies throttling when the request rate is too high. Limits, such as the maximum number of rows returned, are also applied on the Kusto queries. For more information, see [Query API](https://learn.microsoft.com/azure/azure-monitor/service-limits#la-query-api). If you're executing a batch logs query, a throttled request will return a `LogsQueryError` object. That object's `code` value will be `ThrottledError`. ### Metrics data structure Each set of metric values is a time series with the following characteristics: - The time the value was collected - The resource associated with the value - A namespace that acts like a category for the metric - A metric name - The value itself - Some metrics may have multiple dimensions as described in multi-dimensional metrics. Custom metrics can have up to 10 dimensions. ## Examples - [Logs query](#logs-query) - [Specify timespan](#specify-timespan) - [Handle logs query response](#handle-logs-query-response) - [Batch logs query](#batch-logs-query) - [Resource logs query](#resource-logs-query) - [Advanced logs query scenarios](#advanced-logs-query-scenarios) - [Set logs query timeout](#set-logs-query-timeout) - [Query multiple workspaces](#query-multiple-workspaces) - [Include statistics](#include-statistics) - [Include visualization](#include-visualization) - [Metrics query](#metrics-query) - [Handle metrics query response](#handle-metrics-query-response) - [Example of handling response](#example-of-handling-response) ### Logs query This example shows how to query a Log Analytics workspace. To handle the response and view it in a tabular form, the [pandas](https://pypi.org/project/pandas/) library is used. See the [samples][samples] if you choose not to use pandas. #### Specify timespan The `timespan` parameter specifies the time duration for which to query the data. This value can be one of the following: - a `timedelta` - a `timedelta` and a start datetime - a start datetime/end datetime For example: ```python import os import pandas as pd from datetime import datetime, timezone from azure.monitor.query import LogsQueryClient, LogsQueryStatus from azure.identity import DefaultAzureCredential from azure.core.exceptions import HttpResponseError credential = DefaultAzureCredential() client = LogsQueryClient(credential) query = """AppRequests | take 5""" start_time=datetime(2021, 7, 2, tzinfo=timezone.utc) end_time=datetime(2021, 7, 4, tzinfo=timezone.utc) try: response = client.query_workspace( workspace_id=os.environ['LOG_WORKSPACE_ID'], query=query, timespan=(start_time, end_time) ) if response.status == LogsQueryStatus.PARTIAL: error = response.partial_error data = response.partial_data print(error) elif response.status == LogsQueryStatus.SUCCESS: data = response.tables for table in data: df = pd.DataFrame(data=table.rows, columns=table.columns) print(df) except HttpResponseError as err: print("something fatal happened") print(err) ``` #### Handle logs query response The `query_workspace` API returns either a `LogsQueryResult` or a `LogsQueryPartialResult` object. The `batch_query` API returns a list that may contain `LogsQueryResult`, `LogsQueryPartialResult`, and `LogsQueryError` objects. Here's a hierarchy of the response: ``` LogsQueryResult |---statistics |---visualization |---tables (list of `LogsTable` objects) |---name |---rows |---columns |---columns_types LogsQueryPartialResult |---statistics |---visualization |---partial_error (a `LogsQueryError` object) |---code |---message |---details |---status |---partial_data (list of `LogsTable` objects) |---name |---rows |---columns |---columns_types ``` The `LogsQueryResult` directly iterates over the table as a convenience. For example, to handle a logs query response with tables and display it using pandas: ```python response = client.query(...) for table in response: df = pd.DataFrame(table.rows, columns=[col.name for col in table.columns]) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_single_query.py). In a similar fashion, to handle a batch logs query response: ```python for result in response: if result.status == LogsQueryStatus.SUCCESS: for table in result: df = pd.DataFrame(table.rows, columns=table.columns) print(df) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py). ### Batch logs query The following example demonstrates sending multiple queries at the same time using the batch query API. The queries can either be represented as a list of `LogsBatchQuery` objects or a dictionary. This example uses the former approach. ```python import os from datetime import timedelta, datetime, timezone import pandas as pd from azure.monitor.query import LogsQueryClient, LogsBatchQuery, LogsQueryStatus from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) requests = [ LogsBatchQuery( query="AzureActivity | summarize count()", timespan=timedelta(hours=1), workspace_id=os.environ['LOG_WORKSPACE_ID'] ), LogsBatchQuery( query= """bad query""", timespan=timedelta(days=1), workspace_id=os.environ['LOG_WORKSPACE_ID'] ), LogsBatchQuery( query= """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""", workspace_id=os.environ['LOG_WORKSPACE_ID'], timespan=(datetime(2021, 6, 2, tzinfo=timezone.utc), datetime(2021, 6, 5, tzinfo=timezone.utc)), # (start, end) include_statistics=True ), ] results = client.query_batch(requests) for res in results: if res.status == LogsQueryStatus.FAILURE: # this will be a LogsQueryError print(res.message) elif res.status == LogsQueryStatus.PARTIAL: ## this will be a LogsQueryPartialResult print(res.partial_error) for table in res.partial_data: df = pd.DataFrame(table.rows, columns=table.columns) print(df) elif res.status == LogsQueryStatus.SUCCESS: ## this will be a LogsQueryResult table = res.tables[0] df = pd.DataFrame(table.rows, columns=table.columns) print(df) ``` ### Resource logs query The following example demonstrates how to query logs directly from an Azure resource without the use of a Log Analytics workspace. Here, the `query_resource` method is used instead of `query_workspace`, and instead of a workspace ID, an Azure resource identifier is passed in (e.g. `/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/{resource-provider}/{resource-type}/{resource-name}`). ```python import os import pandas as pd from datetime import timedelta from azure.monitor.query import LogsQueryClient, LogsQueryStatus from azure.core.exceptions import HttpResponseError from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) query = """AzureActivity | take 5""" try: response = client.query_resource(os.environ['LOGS_RESOURCE_ID'], query, timespan=timedelta(days=1)) if response.status == LogsQueryStatus.PARTIAL: error = response.partial_error data = response.partial_data print(error) elif response.status == LogsQueryStatus.SUCCESS: data = response.tables for table in data: df = pd.DataFrame(data=table.rows, columns=table.columns) print(df) except HttpResponseError as err: print("something fatal happened") print(err) ``` ### Advanced logs query scenarios #### Set logs query timeout The following example shows setting a server timeout in seconds. A gateway timeout is raised if the query takes more time than the mentioned timeout. The default is 180 seconds and can be set up to 10 minutes (600 seconds). ```python import os from azure.monitor.query import LogsQueryClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = LogsQueryClient(credential) response = client.query_workspace( os.environ['LOG_WORKSPACE_ID'], "range x from 1 to 10000000000 step 1 | count", timespan=timedelta(days=1), server_timeout=600 # sets the timeout to 10 minutes ) ``` #### Query multiple workspaces The same logs query can be executed across multiple Log Analytics workspaces. In addition to the Kusto query, the following parameters are required: - `workspace_id` - The first (primary) workspace ID. - `additional_workspaces` - A list of workspaces, excluding the workspace provided in the `workspace_id` parameter. The parameter's list items may consist of the following identifier formats: - Qualified workspace names - Workspace IDs - Azure resource IDs For example, the following query executes in three workspaces: ```python client.query_workspace( , query, timespan=timedelta(days=1), additional_workspaces=['', ''] ) ``` A full sample can be found [here](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py). #### Include statistics To get logs query execution statistics, such as CPU and memory consumption: 1. Set the `include_statistics` parameter to `True`. 2. Access the `statistics` field inside the `LogsQueryResult` object. The following example prints the query execution time: ```python query = "AzureActivity | top 10 by TimeGenerated" result = client.query_workspace( , query, timespan=timedelta(days=1), include_statistics=True ) execution_time = result.statistics.get("query", {}).get("executionTime") print(f"Query execution time: {execution_time}") ``` The `statistics` field is a `dict` that corresponds to the raw JSON response, and its structure can vary by query. The statistics are found within the `query` property. For example: ```python { "query": { "executionTime": 0.0156478, "resourceUsage": {...}, "inputDatasetStatistics": {...}, "datasetStatistics": [{...}] } } ``` #### Include visualization To get visualization data for logs queries using the [render operator](https://docs.microsoft.com/azure/data-explorer/kusto/query/renderoperator?pivots=azuremonitor): 1. Set the `include_visualization` property to `True`. 1. Access the `visualization` field inside the `LogsQueryResult` object. For example: ```python query = ( "StormEvents" "| summarize event_count = count() by State" "| where event_count > 10" "| project State, event_count" "| render columnchart" ) result = client.query_workspace( , query, timespan=timedelta(days=1), include_visualization=True ) print(f"Visualization result: {result.visualization}") ``` The `visualization` field is a `dict` that corresponds to the raw JSON response, and its structure can vary by query. For example: ```python { "visualization": "columnchart", "title": "the chart title", "accumulate": False, "isQuerySorted": False, "kind": None, "legend": None, "series": None, "yMin": "NaN", "yMax": "NaN", "xAxis": None, "xColumn": None, "xTitle": "x axis title", "yAxis": None, "yColumns": None, "ySplit": None, "yTitle": None, "anomalyColumns": None } ``` ### Metrics query The following example gets metrics for an Event Grid subscription. The resource URI is that of an Event Grid topic. The resource URI must be that of the resource for which metrics are being queried. It's normally of the format `/subscriptions//resourceGroups//providers//topics/`. To find the resource URI: 1. Navigate to your resource's page in the Azure portal. 2. From the **Overview** blade, select the **JSON View** link. 3. In the resulting JSON, copy the value of the `id` property. **NOTE**: The metrics are returned in the order of the metric_names sent. ```python import os from datetime import timedelta, datetime from azure.monitor.query import MetricsQueryClient from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = MetricsQueryClient(credential) start_time = datetime(2021, 5, 25) duration = timedelta(days=1) metrics_uri = os.environ['METRICS_RESOURCE_URI'] response = client.query_resource( metrics_uri, metric_names=["PublishSuccessCount"], timespan=(start_time, duration) ) for metric in response.metrics: print(metric.name) for time_series_element in metric.timeseries: for metric_value in time_series_element.data: print(metric_value.time_stamp) ``` #### Handle metrics query response The metrics query API returns a `MetricsQueryResult` object. The `MetricsQueryResult` object contains properties such as a list of `Metric`-typed objects, `granularity`, `namespace`, and `timespan`. The `Metric` objects list can be accessed using the `metrics` param. Each `Metric` object in this list contains a list of `TimeSeriesElement` objects. Each `TimeSeriesElement` object contains `data` and `metadata_values` properties. In visual form, the object hierarchy of the response resembles the following structure: ``` MetricsQueryResult |---granularity |---timespan |---cost |---namespace |---resource_region |---metrics (list of `Metric` objects) |---id |---type |---name |---unit |---timeseries (list of `TimeSeriesElement` objects) |---metadata_values |---data (list of data points represented by `MetricValue` objects) ``` #### Example of handling response ```python import os from azure.monitor.query import MetricsQueryClient, MetricAggregationType from azure.identity import DefaultAzureCredential credential = DefaultAzureCredential() client = MetricsQueryClient(credential) metrics_uri = os.environ['METRICS_RESOURCE_URI'] response = client.query_resource( metrics_uri, metric_names=["MatchedEventCount"], aggregations=[MetricAggregationType.COUNT] ) for metric in response.metrics: print(metric.name) for time_series_element in metric.timeseries: for metric_value in time_series_element.data: if metric_value.count != 0: print( "There are {} matched events at {}".format( metric_value.count, metric_value.time_stamp ) ) ``` ## Troubleshooting See our [troubleshooting guide][troubleshooting_guide] for details on how to diagnose various failure scenarios. ## Next steps To learn more about Azure Monitor, see the [Azure Monitor service documentation][azure_monitor_overview]. ### Samples The following code samples show common scenarios with the Azure Monitor Query client library. #### Logs query samples - [Send a single query with LogsQueryClient and handle the response as a table](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_single_query.py) ([async sample](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_async.py)) - [Send a single query with LogsQueryClient and handle the response in key-value form](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_logs_query_key_value_form.py) - [Send a single query with LogsQueryClient without pandas](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_single_log_query_without_pandas.py) - [Send a single query with LogsQueryClient across multiple workspaces](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_log_query_multiple_workspaces.py) - [Send multiple queries with LogsQueryClient](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_batch_query.py) - [Send a single query with LogsQueryClient using server timeout](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/samples/sample_server_timeout.py) #### Metrics query samples - [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)) - [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)) - [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)) ## Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit [cla.microsoft.com][cla]. When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA. This project has adopted the [Microsoft Open Source Code of Conduct][code_of_conduct]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments. [azure_core_exceptions]: https://aka.ms/azsdk/python/core/docs#module-azure.core.exceptions [azure_core_ref_docs]: https://aka.ms/azsdk/python/core/docs [azure_monitor_create_using_portal]: https://learn.microsoft.com/azure/azure-monitor/logs/quick-create-workspace [azure_monitor_overview]: https://learn.microsoft.com/azure/azure-monitor/ [azure_subscription]: https://azure.microsoft.com/free/python/ [changelog]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-query/CHANGELOG.md [kusto_query_language]: https://learn.microsoft.com/azure/data-explorer/kusto/query/ [package]: https://aka.ms/azsdk-python-monitor-query-pypi [pip]: https://pypi.org/project/pip/ [python_logging]: https://docs.python.org/3/library/logging.html [python-query-ref-docs]: https://aka.ms/azsdk/python/monitor-query/docs [samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/monitor/azure-monitor-query/samples [source]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/ [troubleshooting_guide]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/monitor/azure-monitor-query/TROUBLESHOOTING.md [cla]: https://cla.microsoft.com [code_of_conduct]: https://opensource.microsoft.com/codeofconduct/ [coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ [coc_contact]: mailto:opencode@microsoft.com # Release History ## 1.2.0 (2023-05-09) ### Features Added - Add the `query_resource` method to `LogsQueryClient` to allow users to query Azure resources directly without the context of a workspace. ([#29365](https://github.com/Azure/azure-sdk-for-python/pull/29365)) ### Bugs Fixed - Fixed an inconsistent keyword argument name in the `LogsTable` constructor, changing `column_types` to `columns_types`. Note that this is a class that is typically only instantiated internally, and not by users. ([#29076](https://github.com/Azure/azure-sdk-for-python/pull/29076)) ### Other Changes - Improved client configuration logic for non-public Azure clouds where credential scope will be determined based on the configured endpoint. ([#29602](https://github.com/Azure/azure-sdk-for-python/pull/29602)) ## 1.1.1 (2023-02-13) ### Bugs Fixed - Fixed a bug where the incorrect key `time_stamp` (should be `timeStamp`) was used in the creation of `MetricValue` objects (thanks @jamespic). ([#28777](https://github.com/Azure/azure-sdk-for-python/pull/28777)) ## 1.1.0 (2023-02-07) ### Bugs Fixed * Error details are now propagated inside the `LogsQueryError` object. ([#25137](https://github.com/Azure/azure-sdk-for-python/issues/25137)) ### Other Changes * Python 3.6 is no longer supported. Please use Python version 3.7 or later. For more details, see [Azure SDK for Python version support policy](https://github.com/Azure/azure-sdk-for-python/wiki/Azure-SDKs-Python-version-support-policy). * Removed `msrest` dependency. * Bumped minimum dependency on `azure-core` to `>=1.24.0`. * Added requirement for `isodate>=0.6.0` (`isodate` was required by `msrest`). * Added requirement for `typing-extensions>=4.0.1`. ## 1.0.3 (2022-07-07) ### Bugs Fixed - Fixed a bug where `query_resource` in metrics client is throwing an error with unexpected `metric_namespace` argument. ## 1.0.2 (2022-05-06) - This version and all future versions will require Python 3.6+. Python 2.7 is no longer supported. ### Bugs Fixed - Fixed a bug where having a None value in datetime throws ## 1.0.1 (2021-11-09) ### Bugs Fixed - Fixed a bug where Metadata values in timestamp don't show up sometimes. ## 1.0.0 (2021-10-06) ### Features Added - Added `LogsQueryPartialResult` and `LogsQueryError` to handle errors. - Added `status` attribute to `LogsQueryResult`. - Added `LogsQueryStatus` Enum to describe the status of a result. - Added a new `LogsTableRow` type that represents a single row in a table. - Items in `metrics` list in `MetricsQueryResult` can now be accessed by metric names. ### Breaking Changes - `LogsQueryResult` now iterates over the tables directly as a convenience. - `query` API in logs is renamed to `query_workspace` - `query` API in metrics is renamed to `query_resource` - `query_workspace` API now returns a union of `LogsQueryPartialResult` and `LogsQueryResult`. - `query_batch` API now returns a union of `LogsQueryPartialResult`, `LogsQueryError` and `LogsQueryResult`. - `metric_namespace` is renamed to `namespace` and is a keyword-only argument in `list_metric_definitions` API. - `MetricsResult` is renamed to `MetricsQueryResult`. ## 1.0.0b4 (2021-09-09) ### 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. - Added a `MetricUnit` enum to describe the unit of the metric. ### Breaking Changes - Rename `batch_query` to `query_batch`. - Rename `LogsBatchQueryRequest` to `LogsBatchQuery`. - `include_render` is now renamed to `include_visualization` in the query API. - `LogsQueryResult` now returns `visualization` instead of `render`. - `start_time`, `duration` and `end_time` are now replaced with a single param called `timespan` - `resourceregion` is renamed to `resource_region` in the MetricResult type. - `top` is renamed to `max_results` in the metric's `query` API. - `metric_namespace_name` is renamed to `fully_qualified_namespace` - `is_dimension_required` is renamed to `dimension_required` - `interval` and `time_grain` are renamed to `granularity` - `orderby` is renamed to `order_by` - `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` - `LogsTableColumn` is now removed. Column labels are strings instead. - `start_time` in `list_metric_namespaces` API is now a datetime. - The order of params in `LogsBatchQuery` is changed. Also, `headers` is no longer accepted. - `timespan` is now a required keyword-only argument in logs APIs. - batch api now returns a list of `LogsQueryResult` objects. ### Bugs Fixed - `include_statistics` and `include_visualization` args can now work together. ## 1.0.0b3 (2021-08-09) ### Features Added - Added enum `AggregationType` which can be used to specify aggregations in the query API. - Added `LogsBatchQueryResult` model that is returned for a logs batch query. - Added `error` attribute to `LogsQueryResult`. ### Breaking Changes - `aggregation` param in the query API is renamed to `aggregations` - `batch_query` API now returns a list of responses. - `LogsBatchResults` model is now removed. - `LogsQueryRequest` is renamed to `LogsBatchQueryRequest` - `LogsQueryResults` is now renamed to `LogsQueryResult` - `LogsBatchQueryResult` now has 4 additional attributes - `tables`, `error`, `statistics` and `render` instead of `body` attribute. ## 1.0.0b2 (2021-07-06) ### Breaking Changes - `workspaces`, `workspace_ids`, `qualified_names` and `azure_resource_ids` are now merged into a single `additional_workspaces` list in the query API. - The `LogQueryRequest` object now takes in a `workspace_id` and `additional_workspaces` instead of `workspace`. - `aggregation` param is now a list instead of a string in the `query` method. - `duration` must now be provided as a timedelta instead of a string. ## 1.0.0b1 (2021-06-10) **Features** - Version (1.0.0b1) is the first preview of our efforts to create a user-friendly and Pythonic client library for Azure Monitor Query. For more information about this, and preview releases of other Azure SDK libraries, please visit https://azure.github.io/azure-sdk/releases/latest/python.html. - Added `~azure.monitor.query.LogsQueryClient` to query log analytics along with `~azure.monitor.query.aio.LogsQueryClient`. - Implements the `~azure.monitor.query.MetricsQueryClient` for querying metrics, listing namespaces and metric definitions along with `~azure.monitor.query.aio.MetricsQueryClient`. %prep %autosetup -n azure-monitor-query-1.2.0 %build %py3_build %install %py3_install install -d -m755 %{buildroot}/%{_pkgdocdir} if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi pushd %{buildroot} if [ -d usr/lib ]; then find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/lib64 ]; then find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/bin ]; then find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst fi if [ -d usr/sbin ]; then find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst fi touch doclist.lst if [ -d usr/share/man ]; then find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst fi popd mv %{buildroot}/filelist.lst . mv %{buildroot}/doclist.lst . %files -n python3-azure-monitor-query -f filelist.lst %dir %{python3_sitelib}/* %files help -f doclist.lst %{_docdir}/* %changelog * Tue May 30 2023 Python_Bot - 1.2.0-1 - Package Spec generated