From f4ddc9640bd1bab33edbc67a9994945b74df6572 Mon Sep 17 00:00:00 2001 From: Paul Van Eck Date: Mon, 28 Oct 2024 19:31:13 +0000 Subject: [PATCH 1/2] [Monitor] Apply black formatting Signed-off-by: Paul Van Eck --- .../ingestion/_operations/_operations.py | 6 +- .../ingestion/aio/_operations/_operations.py | 6 +- .../sample_custom_error_callback_async.py | 7 +- .../samples/sample_custom_error_callback.py | 10 +- sdk/monitor/azure-monitor-ingestion/setup.py | 86 +++++++------- .../tests/base_testcase.py | 2 +- .../azure-monitor-ingestion/tests/conftest.py | 16 +-- .../tests/perf_tests/upload_logs.py | 64 ++++++----- .../tests/test_helpers.py | 12 +- .../tests/test_logs_ingestion.py | 87 +++++++------- .../tests/test_logs_ingestion_async.py | 87 +++++++------- .../sample_authentication_async.py | 2 +- .../async_samples/sample_batch_query_async.py | 8 +- .../async_samples/sample_log_query_async.py | 1 + .../sample_metric_definitions_async.py | 1 + .../sample_metric_namespaces_async.py | 1 + .../sample_metrics_query_async.py | 1 + .../sample_metrics_query_multiple_async.py | 6 +- .../sample_resource_logs_query_async.py | 1 + .../samples/sample_authentication.py | 2 +- .../samples/sample_metrics_query.py | 3 - .../samples/sample_metrics_query_multiple.py | 4 +- sdk/monitor/azure-monitor-query/setup.py | 88 +++++++------- .../tests/base_testcase.py | 12 +- .../azure-monitor-query/tests/conftest.py | 2 +- .../tests/perfstress_tests/batch_query.py | 38 +++--- .../tests/perfstress_tests/metric_query.py | 35 ++---- .../tests/perfstress_tests/single_query.py | 45 +++----- .../tests/test_exceptions.py | 65 ++++++----- .../tests/test_exceptions_async.py | 63 +++++----- .../azure-monitor-query/tests/test_helpers.py | 23 ++-- .../tests/test_logs_client.py | 94 +++++++-------- .../tests/test_logs_client_async.py | 108 ++++++++---------- .../tests/test_logs_response.py | 9 +- .../tests/test_logs_timespans.py | 35 +++--- .../tests/test_metrics_client.py | 4 +- .../tests/test_metrics_client_async.py | 10 +- .../tests/test_metrics_query_client.py | 34 +++--- .../tests/test_metrics_query_client_async.py | 54 ++++----- 39 files changed, 545 insertions(+), 587 deletions(-) diff --git a/sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/_operations/_operations.py b/sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/_operations/_operations.py index 3d6dc22e4243..ea0d1703bb52 100644 --- a/sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/_operations/_operations.py +++ b/sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/_operations/_operations.py @@ -81,8 +81,7 @@ def _upload( # pylint: disable=inconsistent-return-statements content_encoding: Optional[str] = None, content_type: str = "application/json", **kwargs: Any - ) -> None: - ... + ) -> None: ... @overload def _upload( # pylint: disable=inconsistent-return-statements @@ -94,8 +93,7 @@ def _upload( # pylint: disable=inconsistent-return-statements content_encoding: Optional[str] = None, content_type: str = "application/json", **kwargs: Any - ) -> None: - ... + ) -> None: ... @distributed_trace def _upload( # pylint: disable=inconsistent-return-statements diff --git a/sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/aio/_operations/_operations.py b/sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/aio/_operations/_operations.py index 965e3180520e..5f66be399dc0 100644 --- a/sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/aio/_operations/_operations.py +++ b/sdk/monitor/azure-monitor-ingestion/azure/monitor/ingestion/aio/_operations/_operations.py @@ -46,8 +46,7 @@ async def _upload( # pylint: disable=inconsistent-return-statements content_encoding: Optional[str] = None, content_type: str = "application/json", **kwargs: Any - ) -> None: - ... + ) -> None: ... @overload async def _upload( # pylint: disable=inconsistent-return-statements @@ -59,8 +58,7 @@ async def _upload( # pylint: disable=inconsistent-return-statements content_encoding: Optional[str] = None, content_type: str = "application/json", **kwargs: Any - ) -> None: - ... + ) -> None: ... @distributed_trace_async async def _upload( # pylint: disable=inconsistent-return-statements diff --git a/sdk/monitor/azure-monitor-ingestion/samples/async_samples/sample_custom_error_callback_async.py b/sdk/monitor/azure-monitor-ingestion/samples/async_samples/sample_custom_error_callback_async.py index 58a5c470841d..ab4a9413b6d9 100644 --- a/sdk/monitor/azure-monitor-ingestion/samples/async_samples/sample_custom_error_callback_async.py +++ b/sdk/monitor/azure-monitor-ingestion/samples/async_samples/sample_custom_error_callback_async.py @@ -59,9 +59,10 @@ async def on_error_pass(_) -> None: # Sample callback that raises the error if it corresponds to a specific HTTP error code. # This aborts the rest of the upload. async def on_error_abort(error: LogsUploadError) -> None: - if isinstance(error.error, HttpResponseError) and cast(HttpResponseError, error.error).status_code in (400, 401, 403): - print("Aborting upload...") - raise error.error + if isinstance(error.error, HttpResponseError): + if cast(HttpResponseError, error.error).status_code in (400, 401, 403): + print("Aborting upload...") + raise error.error client = LogsIngestionClient(endpoint=endpoint, credential=credential, logging_enable=True) async with client: diff --git a/sdk/monitor/azure-monitor-ingestion/samples/sample_custom_error_callback.py b/sdk/monitor/azure-monitor-ingestion/samples/sample_custom_error_callback.py index fdaa7207bbe3..66187edbc297 100644 --- a/sdk/monitor/azure-monitor-ingestion/samples/sample_custom_error_callback.py +++ b/sdk/monitor/azure-monitor-ingestion/samples/sample_custom_error_callback.py @@ -45,7 +45,8 @@ {"Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", "AdditionalContext": "context"}, ] -failed_logs: List[MutableMapping[str, str]] = [] +failed_logs: List[MutableMapping[str, str]] = [] + # Sample callback that stores the logs that failed to upload. def on_error_save(error: LogsUploadError) -> None: @@ -61,9 +62,10 @@ def on_error_pass(_) -> None: # Sample callback that raises the error if it corresponds to a specific HTTP error code. # This aborts the rest of the upload. def on_error_abort(error: LogsUploadError) -> None: - if isinstance(error.error, HttpResponseError) and cast(HttpResponseError, error.error).status_code in (400, 401, 403): - print("Aborting upload...") - raise error.error + if isinstance(error.error, HttpResponseError): + if cast(HttpResponseError, error.error).status_code in (400, 401, 403): + print("Aborting upload...") + raise error.error client.upload(rule_id=rule_id, stream_name=os.environ["LOGS_DCR_STREAM_NAME"], logs=body, on_error=on_error_save) diff --git a/sdk/monitor/azure-monitor-ingestion/setup.py b/sdk/monitor/azure-monitor-ingestion/setup.py index ba017184ce6a..1ad99e1bb287 100644 --- a/sdk/monitor/azure-monitor-ingestion/setup.py +++ b/sdk/monitor/azure-monitor-ingestion/setup.py @@ -1,10 +1,10 @@ #!/usr/bin/env python -#------------------------------------------------------------------------- +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -#-------------------------------------------------------------------------- +# -------------------------------------------------------------------------- import re import os.path @@ -16,19 +16,19 @@ PACKAGE_PPRINT_NAME = "Azure Monitor Ingestion" # a-b-c => a/b/c -package_folder_path = PACKAGE_NAME.replace('-', '/') +package_folder_path = PACKAGE_NAME.replace("-", "/") # a-b-c => a.b.c -namespace_name = PACKAGE_NAME.replace('-', '.') +namespace_name = PACKAGE_NAME.replace("-", ".") # azure v0.x is not compatible with this package # azure v0.x used to have a __version__ attribute (newer versions don't) try: import azure + try: ver = azure.__version__ raise Exception( - 'This package is incompatible with azure=={}. '.format(ver) + - 'Uninstall it with "pip uninstall azure".' + "This package is incompatible with azure=={}. ".format(ver) + 'Uninstall it with "pip uninstall azure".' ) except AttributeError: pass @@ -36,56 +36,58 @@ pass # Version extraction inspired from 'requests' -with open(os.path.join(package_folder_path, 'version.py') - if os.path.exists(os.path.join(package_folder_path, 'version.py')) - else os.path.join(package_folder_path, '_version.py'), 'r') as fd: - version = re.search(r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]', - fd.read(), re.MULTILINE).group(1) +with open( + ( + os.path.join(package_folder_path, "version.py") + if os.path.exists(os.path.join(package_folder_path, "version.py")) + else os.path.join(package_folder_path, "_version.py") + ), + "r", +) as fd: + version = re.search(r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]', fd.read(), re.MULTILINE).group(1) if not version: - raise RuntimeError('Cannot find version information') + raise RuntimeError("Cannot find version information") -with open('README.md', encoding='utf-8') as f: +with open("README.md", encoding="utf-8") as f: readme = f.read() -with open('CHANGELOG.md', encoding='utf-8') as f: +with open("CHANGELOG.md", encoding="utf-8") as f: changelog = f.read() setup( name=PACKAGE_NAME, version=version, - description='Microsoft {} Client Library for Python'.format(PACKAGE_PPRINT_NAME), - long_description=readme + '\n\n' + changelog, - long_description_content_type='text/markdown', - license='MIT License', - author='Microsoft Corporation', - author_email='azpysdkhelp@microsoft.com', - url='https://github.com/Azure/azure-sdk-for-python', + description="Microsoft {} Client Library for Python".format(PACKAGE_PPRINT_NAME), + long_description=readme + "\n\n" + changelog, + long_description_content_type="text/markdown", + license="MIT License", + author="Microsoft Corporation", + author_email="azpysdkhelp@microsoft.com", + url="https://github.com/Azure/azure-sdk-for-python", keywords="azure, azure sdk", classifiers=[ "Development Status :: 5 - Production/Stable", - 'Programming Language :: Python', - 'Programming Language :: Python :: 3 :: Only', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', - 'Programming Language :: Python :: 3.10', - 'Programming Language :: Python :: 3.11', - 'Programming Language :: Python :: 3.12', - 'License :: OSI Approved :: MIT License', + "Programming Language :: Python", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "License :: OSI Approved :: MIT License", ], python_requires=">=3.8", zip_safe=False, - packages=find_packages(exclude=[ - 'tests', - 'samples', - # Exclude packages that will be covered by PEP420 or nspkg - 'azure', - 'azure.monitor', - ]), + packages=find_packages( + exclude=[ + "tests", + "samples", + # Exclude packages that will be covered by PEP420 or nspkg + "azure", + "azure.monitor", + ] + ), include_package_data=True, - install_requires=[ - 'azure-core>=1.28.0', - 'isodate>=0.6.0', - "typing-extensions>=4.0.1" - ] + install_requires=["azure-core>=1.28.0", "isodate>=0.6.0", "typing-extensions>=4.0.1"], ) diff --git a/sdk/monitor/azure-monitor-ingestion/tests/base_testcase.py b/sdk/monitor/azure-monitor-ingestion/tests/base_testcase.py index f38c9cbb6e4a..cde9b62b753f 100644 --- a/sdk/monitor/azure-monitor-ingestion/tests/base_testcase.py +++ b/sdk/monitor/azure-monitor-ingestion/tests/base_testcase.py @@ -12,7 +12,7 @@ AUDIENCE_MAP = { "AzureCloud": "https://monitor.azure.com", "AzureChinaCloud": "https://monitor.azure.cn", - "AzureUSGovernment": "https://monitor.azure.us" + "AzureUSGovernment": "https://monitor.azure.us", } diff --git a/sdk/monitor/azure-monitor-ingestion/tests/conftest.py b/sdk/monitor/azure-monitor-ingestion/tests/conftest.py index fb21e700afb2..f2fdaab5cfd2 100644 --- a/sdk/monitor/azure-monitor-ingestion/tests/conftest.py +++ b/sdk/monitor/azure-monitor-ingestion/tests/conftest.py @@ -32,7 +32,7 @@ add_general_regex_sanitizer, add_header_regex_sanitizer, set_custom_default_matcher, - add_oauth_response_sanitizer + add_oauth_response_sanitizer, ) @@ -60,15 +60,14 @@ def add_sanitizers(test_proxy, environment_variables): ENV_CLIENT_SECRET: TEST_ID, ENV_DCE: TEST_DCE, ENV_STREAM_NAME: TEST_STREAM_NAME, - ENV_DCR_ID: TEST_ID + ENV_DCR_ID: TEST_ID, } environment_variables.sanitize_batch(sanitization_mapping) set_custom_default_matcher( compare_bodies=False, excluded_headers="Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id" ) add_general_regex_sanitizer( - value="fakeresource", - regex="(?<=\\/\\/)[a-z-]+(?=\\.westus2-1\\.ingest\\.monitor\\.azure\\.com)" + value="fakeresource", regex="(?<=\\/\\/)[a-z-]+(?=\\.westus2-1\\.ingest\\.monitor\\.azure\\.com)" ) add_body_key_sanitizer(json_path="access_token", value="fakekey") add_header_regex_sanitizer(key="Set-Cookie", value="[set-cookie;]") @@ -80,19 +79,16 @@ def monitor_info(environment_variables): yield { "stream_name": environment_variables.get(ENV_STREAM_NAME), "dce": environment_variables.get(ENV_DCE), - "dcr_id": environment_variables.get(ENV_DCR_ID) + "dcr_id": environment_variables.get(ENV_DCR_ID), } @pytest.fixture(scope="session") def large_data(): logs = [] - content = "a" * (1024 * 100) # 100 KiB string + content = "a" * (1024 * 100) # 100 KiB string # Ensure total size is > 2 MiB data for i in range(24): - logs.append({ - "Time": datetime.now().isoformat(), - "AdditionalContext": content - }) + logs.append({"Time": datetime.now().isoformat(), "AdditionalContext": content}) return logs diff --git a/sdk/monitor/azure-monitor-ingestion/tests/perf_tests/upload_logs.py b/sdk/monitor/azure-monitor-ingestion/tests/perf_tests/upload_logs.py index 4f6b69e5fc91..176665f16b84 100644 --- a/sdk/monitor/azure-monitor-ingestion/tests/perf_tests/upload_logs.py +++ b/sdk/monitor/azure-monitor-ingestion/tests/perf_tests/upload_logs.py @@ -1,8 +1,8 @@ -#------------------------------------------------------------------------- +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -#-------------------------------------------------------------------------- +# -------------------------------------------------------------------------- from datetime import datetime import json import random @@ -17,8 +17,9 @@ ALPHANUMERIC_CHARACTERS = string.ascii_letters + string.digits + def _get_random_string(length: int): - return ''.join(random.choice(ALPHANUMERIC_CHARACTERS) for _ in range(length)) + return "".join(random.choice(ALPHANUMERIC_CHARACTERS) for _ in range(length)) def _get_repeating_string(length: int): @@ -37,13 +38,9 @@ def __init__(self, arguments): self.async_credential = AsyncDefaultAzureCredential() # Create clients - self.client = LogsIngestionClient( - endpoint=self.data_collection_endpoint, - credential=self.credential - ) + self.client = LogsIngestionClient(endpoint=self.data_collection_endpoint, credential=self.credential) self.async_client = AsyncLogsIngestionClient( - endpoint=self.data_collection_endpoint, - credential=self.async_credential + endpoint=self.data_collection_endpoint, credential=self.async_credential ) async def close(self): @@ -58,36 +55,43 @@ async def setup(self): # Create log entries to upload self.logs = [] for i in range(self.args.num_logs): - content = _get_random_string(self.args.log_content_length) if self.args.random_log_content \ + content = ( + _get_random_string(self.args.log_content_length) + if self.args.random_log_content else _get_repeating_string(self.args.log_content_length) - self.logs.append({ - "Time": datetime.now().isoformat(), - "Computer": f"Computer {i}", - "AdditionalContext": content - }) - print(f'{len(json.dumps(self.logs))} bytes of logs to be uploaded.') + ) + self.logs.append( + {"Time": datetime.now().isoformat(), "Computer": f"Computer {i}", "AdditionalContext": content} + ) + print(f"{len(json.dumps(self.logs))} bytes of logs to be uploaded.") @staticmethod def add_arguments(parser): super(UploadLogsTest, UploadLogsTest).add_arguments(parser) - parser.add_argument("-n", "--num-logs", nargs="?", type=int, - help="Number of logs to be uploaded. Defaults to 100", default=100) - parser.add_argument("-l", "--log-content-length", nargs="?", type=int, - help="Length of the 'AdditionalContext' value for each log entry. Defaults to 20", default=20) - parser.add_argument("-r", "--random-log-content", action="store_true", + parser.add_argument( + "-n", "--num-logs", nargs="?", type=int, help="Number of logs to be uploaded. Defaults to 100", default=100 + ) + parser.add_argument( + "-l", + "--log-content-length", + nargs="?", + type=int, + help="Length of the 'AdditionalContext' value for each log entry. Defaults to 20", + default=20, + ) + parser.add_argument( + "-r", + "--random-log-content", + action="store_true", help="Whether to use a random alphanumeric string for each 'AdditionalContext' value. " - "If False, uses a repeating 'a' character. Defaults to False", default=False) + "If False, uses a repeating 'a' character. Defaults to False", + default=False, + ) def run_sync(self): - self.client.upload( - rule_id=self.data_collection_rule_id, - stream_name=self.stream_name, - logs=self.logs - ) + self.client.upload(rule_id=self.data_collection_rule_id, stream_name=self.stream_name, logs=self.logs) async def run_async(self): await self.async_client.upload( - rule_id=self.data_collection_rule_id, - stream_name=self.stream_name, - logs=self.logs + rule_id=self.data_collection_rule_id, stream_name=self.stream_name, logs=self.logs ) diff --git a/sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py b/sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py index c5cac5acb8f2..778f5dabde72 100644 --- a/sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py +++ b/sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py @@ -14,17 +14,17 @@ _create_gzip_requests, _split_chunks, MAX_CHUNK_SIZE_BYTES, - GZIP_MAGIC_NUMBER + GZIP_MAGIC_NUMBER, ) ALPHANUMERIC_CHARACTERS = string.ascii_letters + string.digits -random.seed(42) # For repeatibility +random.seed(42) # For repeatibility def _get_random_string(length: int): - return ''.join(random.choice(ALPHANUMERIC_CHARACTERS) for _ in range(length)) + return "".join(random.choice(ALPHANUMERIC_CHARACTERS) for _ in range(length)) class TestHelpers: @@ -39,10 +39,10 @@ def test_split_chunks(self, content): chunks = list(_split_chunks(logs, max_size_bytes=entry_size)) assert len(chunks) == 100 - chunks = list(_split_chunks(logs, max_size_bytes=entry_size*2)) + chunks = list(_split_chunks(logs, max_size_bytes=entry_size * 2)) assert len(chunks) == 50 - chunks = list(_split_chunks(logs, max_size_bytes=entry_size*100)) + chunks = list(_split_chunks(logs, max_size_bytes=entry_size * 100)) assert len(chunks) == 1 def test_split_chunks_larger_than_max(self): @@ -58,4 +58,4 @@ def test_create_gzip_requests(self, num_entries): for compressed_bytes, raw_data in _create_gzip_requests(logs): assert len(compressed_bytes) < MAX_CHUNK_SIZE_BYTES assert compressed_bytes[:2] == GZIP_MAGIC_NUMBER - assert zlib.decompress(compressed_bytes, 16+zlib.MAX_WBITS) == json.dumps(raw_data).encode("utf-8") + assert zlib.decompress(compressed_bytes, 16 + zlib.MAX_WBITS) == json.dumps(raw_data).encode("utf-8") diff --git a/sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion.py b/sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion.py index d194c7921233..6a921511a41f 100644 --- a/sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion.py +++ b/sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion.py @@ -21,19 +21,13 @@ { "Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer1", - "AdditionalContext": { - "testContextKey": 3, - "CounterName": "AppMetric1" - } + "AdditionalContext": {"testContextKey": 3, "CounterName": "AppMetric1"}, }, { "Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", - "AdditionalContext": { - "testContextKey": 2, - "CounterName": "AppMetric1" - } - } + "AdditionalContext": {"testContextKey": 2, "CounterName": "AppMetric1"}, + }, ] @@ -41,27 +35,31 @@ class TestLogsIngestionClient(LogsIngestionClientTestCase): def test_send_logs(self, recorded_test, monitor_info): client = self.get_client( - LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce']) + LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"] + ) with client: - client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=LOGS_BODY) + client.upload(rule_id=monitor_info["dcr_id"], stream_name=monitor_info["stream_name"], logs=LOGS_BODY) def test_send_logs_large(self, recorded_test, monitor_info, large_data): client = self.get_client( - LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce']) + LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"] + ) with client: - client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=large_data) + client.upload(rule_id=monitor_info["dcr_id"], stream_name=monitor_info["stream_name"], logs=large_data) def test_send_logs_error(self, recorded_test, monitor_info): client = self.get_client( - LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce']) + LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"] + ) body = [{"foo": "bar"}] with pytest.raises(HttpResponseError) as ex: - client.upload(rule_id='bad-rule', stream_name=monitor_info['stream_name'], logs=body) + client.upload(rule_id="bad-rule", stream_name=monitor_info["stream_name"], logs=body) def test_send_logs_error_custom(self, recorded_test, monitor_info): client = self.get_client( - LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce']) + LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"] + ) body = [{"foo": "bar"}] def on_error(e): @@ -71,39 +69,41 @@ def on_error(e): on_error.called = False - client.upload( - rule_id='bad-rule', stream_name=monitor_info['stream_name'], logs=body, on_error=on_error) + client.upload(rule_id="bad-rule", stream_name=monitor_info["stream_name"], logs=body, on_error=on_error) assert on_error.called def test_send_logs_json_file(self, recorded_test, monitor_info): client = self.get_client( - LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce']) + LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"] + ) - temp_file = str(uuid.uuid4()) + '.json' - with open(temp_file, 'w') as f: + temp_file = str(uuid.uuid4()) + ".json" + with open(temp_file, "w") as f: json.dump(LOGS_BODY, f) with client: - with open(temp_file, 'r') as f: - client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=f) + with open(temp_file, "r") as f: + client.upload(rule_id=monitor_info["dcr_id"], stream_name=monitor_info["stream_name"], logs=f) os.remove(temp_file) @pytest.mark.live_test_only("Issues recording binary streams with test-proxy") def test_send_logs_gzip_file(self, monitor_info): client = self.get_client( - LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce']) + LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"] + ) - temp_file = str(uuid.uuid4()) + '.json.gz' - with gzip.open(temp_file, 'wb') as f: - f.write(json.dumps(LOGS_BODY).encode('utf-8')) + temp_file = str(uuid.uuid4()) + ".json.gz" + with gzip.open(temp_file, "wb") as f: + f.write(json.dumps(LOGS_BODY).encode("utf-8")) - with open(temp_file, 'rb') as f: - client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=f) + with open(temp_file, "rb") as f: + client.upload(rule_id=monitor_info["dcr_id"], stream_name=monitor_info["stream_name"], logs=f) os.remove(temp_file) def test_abort_error_handler(self, monitor_info): client = self.get_client( - LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce']) + LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"] + ) class TestException(Exception): pass @@ -118,33 +118,38 @@ def on_error(e): with client: # No exception should be raised - with mock.patch("azure.monitor.ingestion._operations._patch.GeneratedOps._upload", - side_effect=ConnectionError): + with mock.patch( + "azure.monitor.ingestion._operations._patch.GeneratedOps._upload", side_effect=ConnectionError + ): client.upload( - rule_id=monitor_info['dcr_id'], - stream_name=monitor_info['stream_name'], + rule_id=monitor_info["dcr_id"], + stream_name=monitor_info["stream_name"], logs=LOGS_BODY, - on_error=on_error) + on_error=on_error, + ) assert on_error.called on_error.called = False # Exception should now be raised since error handler checked for RuntimeError. - with mock.patch("azure.monitor.ingestion._operations._patch.GeneratedOps._upload", - side_effect=RuntimeError): + with mock.patch( + "azure.monitor.ingestion._operations._patch.GeneratedOps._upload", side_effect=RuntimeError + ): with pytest.raises(TestException): client.upload( - rule_id=monitor_info['dcr_id'], - stream_name=monitor_info['stream_name'], + rule_id=monitor_info["dcr_id"], + stream_name=monitor_info["stream_name"], logs=LOGS_BODY, - on_error=on_error) + on_error=on_error, + ) assert on_error.called @pytest.mark.parametrize("logs", ['[{"foo": "bar"}]', "foo", {"foo": "bar"}, None]) def test_invalid_logs_format(self, monitor_info, logs): client = self.get_client( - LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info['dce']) + LogsIngestionClient, self.get_credential(LogsIngestionClient), endpoint=monitor_info["dce"] + ) with pytest.raises(ValueError): client.upload(rule_id="rule", stream_name="stream", logs=logs) diff --git a/sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion_async.py b/sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion_async.py index 154fd2762282..4df72825e83e 100644 --- a/sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion_async.py +++ b/sdk/monitor/azure-monitor-ingestion/tests/test_logs_ingestion_async.py @@ -21,19 +21,13 @@ { "Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer1", - "AdditionalContext": { - "testContextKey": 3, - "CounterName": "AppMetric1" - } + "AdditionalContext": {"testContextKey": 3, "CounterName": "AppMetric1"}, }, { "Time": "2021-12-08T23:51:14.1104269Z", "Computer": "Computer2", - "AdditionalContext": { - "testContextKey": 2, - "CounterName": "AppMetric1" - } - } + "AdditionalContext": {"testContextKey": 2, "CounterName": "AppMetric1"}, + }, ] @@ -42,39 +36,36 @@ class TestLogsIngestionClientAsync(LogsIngestionClientTestCase): @pytest.mark.asyncio async def test_send_logs_async(self, recorded_test, monitor_info): credential = self.get_credential(LogsIngestionClient, is_async=True) - client = self.get_client( - LogsIngestionClient, credential, endpoint=monitor_info['dce']) + client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"]) async with client: - await client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=LOGS_BODY) + await client.upload(rule_id=monitor_info["dcr_id"], stream_name=monitor_info["stream_name"], logs=LOGS_BODY) await credential.close() @pytest.mark.asyncio async def test_send_logs_large(self, recorded_test, monitor_info, large_data): credential = self.get_credential(LogsIngestionClient, is_async=True) - client = self.get_client( - LogsIngestionClient, credential, endpoint=monitor_info['dce']) + client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"]) async with client: await client.upload( - rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=large_data) + rule_id=monitor_info["dcr_id"], stream_name=monitor_info["stream_name"], logs=large_data + ) await credential.close() @pytest.mark.asyncio async def test_send_logs_error(self, recorded_test, monitor_info): credential = self.get_credential(LogsIngestionClient, is_async=True) - client = self.get_client( - LogsIngestionClient, credential, endpoint=monitor_info['dce']) + client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"]) body = [{"foo": "bar"}] with pytest.raises(HttpResponseError) as ex: async with client: - await client.upload(rule_id='bad-rule', stream_name=monitor_info['stream_name'], logs=body) + await client.upload(rule_id="bad-rule", stream_name=monitor_info["stream_name"], logs=body) await credential.close() @pytest.mark.asyncio async def test_send_logs_error_custom(self, recorded_test, monitor_info): credential = self.get_credential(LogsIngestionClient, is_async=True) - client = self.get_client( - LogsIngestionClient, credential, endpoint=monitor_info['dce']) + client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"]) body = [{"foo": "bar"}] async def on_error(e): @@ -86,23 +77,23 @@ async def on_error(e): async with client: await client.upload( - rule_id='bad-rule', stream_name=monitor_info['stream_name'], logs=body, on_error=on_error) + rule_id="bad-rule", stream_name=monitor_info["stream_name"], logs=body, on_error=on_error + ) assert on_error.called await credential.close() @pytest.mark.asyncio async def test_send_logs_json_file(self, recorded_test, monitor_info): credential = self.get_credential(LogsIngestionClient, is_async=True) - client = self.get_client( - LogsIngestionClient, credential, endpoint=monitor_info['dce']) + client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"]) - temp_file = str(uuid.uuid4()) + '.json' - with open(temp_file, 'w') as f: + temp_file = str(uuid.uuid4()) + ".json" + with open(temp_file, "w") as f: json.dump(LOGS_BODY, f) async with client: - with open(temp_file, 'r') as f: - await client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=f) + with open(temp_file, "r") as f: + await client.upload(rule_id=monitor_info["dcr_id"], stream_name=monitor_info["stream_name"], logs=f) os.remove(temp_file) await credential.close() @@ -110,24 +101,22 @@ async def test_send_logs_json_file(self, recorded_test, monitor_info): @pytest.mark.live_test_only("Issues recording binary streams with test-proxy") async def test_send_logs_gzip_file(self, monitor_info): credential = self.get_credential(LogsIngestionClient, is_async=True) - client = self.get_client( - LogsIngestionClient, credential, endpoint=monitor_info['dce']) + client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"]) - temp_file = str(uuid.uuid4()) + '.json.gz' - with gzip.open(temp_file, 'wb') as f: - f.write(json.dumps(LOGS_BODY).encode('utf-8')) + temp_file = str(uuid.uuid4()) + ".json.gz" + with gzip.open(temp_file, "wb") as f: + f.write(json.dumps(LOGS_BODY).encode("utf-8")) async with client: - with open(temp_file, 'rb') as f: - await client.upload(rule_id=monitor_info['dcr_id'], stream_name=monitor_info['stream_name'], logs=f) + with open(temp_file, "rb") as f: + await client.upload(rule_id=monitor_info["dcr_id"], stream_name=monitor_info["stream_name"], logs=f) os.remove(temp_file) await credential.close() @pytest.mark.asyncio async def test_abort_error_handler(self, monitor_info): credential = self.get_credential(LogsIngestionClient, is_async=True) - client = self.get_client( - LogsIngestionClient, credential, endpoint=monitor_info['dce']) + client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"]) class TestException(Exception): pass @@ -142,26 +131,30 @@ async def on_error(e): async with client: # No exception should be raised - with mock.patch("azure.monitor.ingestion.aio._operations._patch.GeneratedOps._upload", - side_effect=ConnectionError): + with mock.patch( + "azure.monitor.ingestion.aio._operations._patch.GeneratedOps._upload", side_effect=ConnectionError + ): await client.upload( - rule_id=monitor_info['dcr_id'], - stream_name=monitor_info['stream_name'], + rule_id=monitor_info["dcr_id"], + stream_name=monitor_info["stream_name"], logs=LOGS_BODY, - on_error=on_error) + on_error=on_error, + ) assert on_error.called on_error.called = False # Exception should now be raised since error handler checked for RuntimeError. - with mock.patch("azure.monitor.ingestion.aio._operations._patch.GeneratedOps._upload", - side_effect=RuntimeError): + with mock.patch( + "azure.monitor.ingestion.aio._operations._patch.GeneratedOps._upload", side_effect=RuntimeError + ): with pytest.raises(TestException): await client.upload( - rule_id=monitor_info['dcr_id'], - stream_name=monitor_info['stream_name'], + rule_id=monitor_info["dcr_id"], + stream_name=monitor_info["stream_name"], logs=LOGS_BODY, - on_error=on_error) + on_error=on_error, + ) assert on_error.called await credential.close() @@ -170,7 +163,7 @@ async def on_error(e): @pytest.mark.parametrize("logs", ['[{"foo": "bar"}]', "foo", {"foo": "bar"}, None]) async def test_invalid_logs_format(self, monitor_info, logs): credential = self.get_credential(LogsIngestionClient, is_async=True) - client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info['dce']) + client = self.get_client(LogsIngestionClient, credential, endpoint=monitor_info["dce"]) async with client: with pytest.raises(ValueError): diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_authentication_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_authentication_async.py index 64a7db81d2bf..df2c540179e3 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_authentication_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_authentication_async.py @@ -90,5 +90,5 @@ async def main(): await create_metrics_client_sovereign_cloud_async() -if __name__ == '__main__': +if __name__ == "__main__": asyncio.run(main()) diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_batch_query_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_batch_query_async.py index 0a7a0685fa2d..31319dccd8b4 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_batch_query_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_batch_query_async.py @@ -23,7 +23,7 @@ from azure.core.exceptions import HttpResponseError from azure.identity.aio import DefaultAzureCredential -from azure.monitor.query.aio import LogsQueryClient +from azure.monitor.query.aio import LogsQueryClient from azure.monitor.query import LogsBatchQuery, LogsQueryStatus import pandas as pd @@ -44,7 +44,10 @@ async def logs_batch_query(): range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""", workspace_id=os.environ["LOGS_WORKSPACE_ID"], - timespan=(datetime(2021, 6, 2, tzinfo=timezone.utc), datetime(2021, 6, 5, tzinfo=timezone.utc)), # (start, end) + timespan=( + datetime(2021, 6, 2, tzinfo=timezone.utc), + datetime(2021, 6, 5, tzinfo=timezone.utc), + ), # (start, end) include_statistics=True, ), ] @@ -73,6 +76,7 @@ async def logs_batch_query(): print(err) await credential.close() + # [END send_query_batch_async] if __name__ == "__main__": diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_async.py index dac86167fce5..c9892b4dcca3 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_log_query_async.py @@ -53,6 +53,7 @@ async def logs_query(): print(err) await credential.close() + # [END send_logs_query_async] if __name__ == "__main__": diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py index 365aaa01d670..690a9fcbc9c4 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_definitions_async.py @@ -42,6 +42,7 @@ async def list_definitions(): print(err) await credential.close() + # [END send_metric_definitions_query_async] if __name__ == "__main__": diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py index 17e28eb35d76..0dcd0ae8215b 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metric_namespaces_async.py @@ -40,6 +40,7 @@ async def list_namespaces(): print(err) await credential.close() + # [END send_metric_namespaces_query_async] if __name__ == "__main__": diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_async.py index 2bfe803ec9d5..8961f920bde3 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_async.py @@ -53,6 +53,7 @@ async def query_metrics(): print(err) await credential.close() + # [END send_metrics_query_async] if __name__ == "__main__": diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_multiple_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_multiple_async.py index c025d2f4b996..57e1f6114a6d 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_multiple_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_metrics_query_multiple_async.py @@ -34,8 +34,8 @@ async def query_metrics_batch(): client = MetricsClient(endpoint, credential) resource_ids = [ - '/subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/', - '/subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/' + "/subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/", + "/subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/", ] async with client: try: @@ -58,6 +58,8 @@ async def query_metrics_batch(): print("something fatal happened") print(err) await credential.close() + + # [END send_metrics_batch_query_async] if __name__ == "__main__": diff --git a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_resource_logs_query_async.py b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_resource_logs_query_async.py index fcba42c204e8..6873c54d6ebd 100644 --- a/sdk/monitor/azure-monitor-query/samples/async_samples/sample_resource_logs_query_async.py +++ b/sdk/monitor/azure-monitor-query/samples/async_samples/sample_resource_logs_query_async.py @@ -55,6 +55,7 @@ async def resource_logs_query(): print(err) await credential.close() + # [END resource_logs_query_async] if __name__ == "__main__": diff --git a/sdk/monitor/azure-monitor-query/samples/sample_authentication.py b/sdk/monitor/azure-monitor-query/samples/sample_authentication.py index bcd81f9f0b75..aa56af872331 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_authentication.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_authentication.py @@ -77,7 +77,7 @@ def create_metrics_client_sovereign_cloud(): # [END create_metrics_client_sovereign_cloud] -if __name__ == '__main__': +if __name__ == "__main__": create_logs_query_client() create_logs_query_client_sovereign_cloud() create_metrics_query_client() diff --git a/sdk/monitor/azure-monitor-query/samples/sample_metrics_query.py b/sdk/monitor/azure-monitor-query/samples/sample_metrics_query.py index a1ef837882f5..73a7aaf2ce0f 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_metrics_query.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_metrics_query.py @@ -16,9 +16,6 @@ In this example, a Storage account resource URI is taken. """ -# Provide a cert or disable warnings to run this sample -import urllib3 -urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) # [START send_metrics_query] from datetime import timedelta diff --git a/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_multiple.py b/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_multiple.py index 79f3f489976b..620756a9822a 100644 --- a/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_multiple.py +++ b/sdk/monitor/azure-monitor-query/samples/sample_metrics_query_multiple.py @@ -31,8 +31,8 @@ client = MetricsClient(endpoint, credential) resource_ids = [ - '/subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/', - '/subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/' + "/subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/", + "/subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/", ] try: diff --git a/sdk/monitor/azure-monitor-query/setup.py b/sdk/monitor/azure-monitor-query/setup.py index 1354d38a54ba..ffc8c2e6343d 100644 --- a/sdk/monitor/azure-monitor-query/setup.py +++ b/sdk/monitor/azure-monitor-query/setup.py @@ -1,10 +1,10 @@ #!/usr/bin/env python -#------------------------------------------------------------------------- +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -#-------------------------------------------------------------------------- +# -------------------------------------------------------------------------- import re import os.path @@ -16,19 +16,19 @@ PACKAGE_PPRINT_NAME = "Azure Monitor Query" # a-b-c => a/b/c -package_folder_path = PACKAGE_NAME.replace('-', '/') +package_folder_path = PACKAGE_NAME.replace("-", "/") # a-b-c => a.b.c -namespace_name = PACKAGE_NAME.replace('-', '.') +namespace_name = PACKAGE_NAME.replace("-", ".") # azure v0.x is not compatible with this package # azure v0.x used to have a __version__ attribute (newer versions don't) try: import azure + try: ver = azure.__version__ raise Exception( - 'This package is incompatible with azure=={}. '.format(ver) + - 'Uninstall it with "pip uninstall azure".' + "This package is incompatible with azure=={}. ".format(ver) + 'Uninstall it with "pip uninstall azure".' ) except AttributeError: pass @@ -36,59 +36,61 @@ pass # Version extraction inspired from 'requests' -with open(os.path.join(package_folder_path, 'version.py') - if os.path.exists(os.path.join(package_folder_path, 'version.py')) - else os.path.join(package_folder_path, '_version.py'), 'r') as fd: - version = re.search(r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]', - fd.read(), re.MULTILINE).group(1) +with open( + ( + os.path.join(package_folder_path, "version.py") + if os.path.exists(os.path.join(package_folder_path, "version.py")) + else os.path.join(package_folder_path, "_version.py") + ), + "r", +) as fd: + version = re.search(r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]', fd.read(), re.MULTILINE).group(1) if not version: - raise RuntimeError('Cannot find version information') + raise RuntimeError("Cannot find version information") -with open('README.md', encoding='utf-8') as f: +with open("README.md", encoding="utf-8") as f: readme = f.read() -with open('CHANGELOG.md', encoding='utf-8') as f: +with open("CHANGELOG.md", encoding="utf-8") as f: changelog = f.read() setup( name=PACKAGE_NAME, version=version, - description='Microsoft {} Client Library for Python'.format(PACKAGE_PPRINT_NAME), - long_description=readme + '\n\n' + changelog, - long_description_content_type='text/markdown', - license='MIT License', - author='Microsoft Corporation', - author_email='azpysdkhelp@microsoft.com', - url='https://github.com/Azure/azure-sdk-for-python', + description="Microsoft {} Client Library for Python".format(PACKAGE_PPRINT_NAME), + long_description=readme + "\n\n" + changelog, + long_description_content_type="text/markdown", + license="MIT License", + author="Microsoft Corporation", + author_email="azpysdkhelp@microsoft.com", + url="https://github.com/Azure/azure-sdk-for-python", keywords="azure, azure sdk", classifiers=[ "Development Status :: 5 - Production/Stable", - 'Programming Language :: Python', - 'Programming Language :: Python :: 3 :: Only', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', - 'Programming Language :: Python :: 3.10', - 'Programming Language :: Python :: 3.11', - 'Programming Language :: Python :: 3.12', - 'License :: OSI Approved :: MIT License', + "Programming Language :: Python", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "License :: OSI Approved :: MIT License", ], python_requires=">=3.8", zip_safe=False, - packages=find_packages(exclude=[ - 'tests', - 'samples', - # Exclude packages that will be covered by PEP420 or nspkg - 'azure', - 'azure.monitor', - ]), + packages=find_packages( + exclude=[ + "tests", + "samples", + # Exclude packages that will be covered by PEP420 or nspkg + "azure", + "azure.monitor", + ] + ), include_package_data=True, package_data={ - 'pytyped': ['py.typed'], + "pytyped": ["py.typed"], }, - install_requires=[ - 'azure-core>=1.28.0', - 'isodate>=0.6.0', - "typing-extensions>=4.0.1" - ] + install_requires=["azure-core>=1.28.0", "isodate>=0.6.0", "typing-extensions>=4.0.1"], ) diff --git a/sdk/monitor/azure-monitor-query/tests/base_testcase.py b/sdk/monitor/azure-monitor-query/tests/base_testcase.py index ef8c84779e94..7c67f9dca21a 100644 --- a/sdk/monitor/azure-monitor-query/tests/base_testcase.py +++ b/sdk/monitor/azure-monitor-query/tests/base_testcase.py @@ -15,20 +15,16 @@ LOGS_ENVIRONMENT_ENDPOINT_MAP = { "AzureCloud": "https://api.loganalytics.io/v1", "AzureChinaCloud": "https://api.loganalytics.azure.cn/v1", - "AzureUSGovernment": "https://api.loganalytics.us/v1" + "AzureUSGovernment": "https://api.loganalytics.us/v1", } METRICS_CLIENT_ENVIRONMENT_AUDIENCE_MAP = { "AzureCloud": "https://metrics.monitor.azure.com", "AzureChinaCloud": "https://metrics.monitor.azure.cn", - "AzureUSGovernment": "https://metrics.monitor.azure.us" + "AzureUSGovernment": "https://metrics.monitor.azure.us", } -TLD_MAP = { - "AzureCloud": "com", - "AzureChinaCloud": "cn", - "AzureUSGovernment": "us" -} +TLD_MAP = {"AzureCloud": "com", "AzureChinaCloud": "cn", "AzureUSGovernment": "us"} class AzureMonitorQueryLogsTestCase(AzureRecordedTestCase): @@ -57,7 +53,7 @@ def get_client(self, client_class, credential): class MetricsClientTestCase(AzureRecordedTestCase): - def get_client(self, client_class, credential, endpoint = None): + def get_client(self, client_class, credential, endpoint=None): environment = os.getenv(ENV_MONITOR_ENVIRONMENT) kwargs = {} diff --git a/sdk/monitor/azure-monitor-query/tests/conftest.py b/sdk/monitor/azure-monitor-query/tests/conftest.py index aab875160332..592e643fc94f 100644 --- a/sdk/monitor/azure-monitor-query/tests/conftest.py +++ b/sdk/monitor/azure-monitor-query/tests/conftest.py @@ -36,7 +36,7 @@ def add_sanitizers(test_proxy, environment_variables): ENV_CLIENT_ID: TEST_ID, ENV_CLIENT_SECRET: TEST_ID, ENV_TABLE_NAME: TEST_TABLE_NAME, - ENV_DCR_ID: TEST_ID + ENV_DCR_ID: TEST_ID, } environment_variables.sanitize_batch(sanitization_mapping) add_header_regex_sanitizer(key="Set-Cookie", value="[set-cookie;]") diff --git a/sdk/monitor/azure-monitor-query/tests/perfstress_tests/batch_query.py b/sdk/monitor/azure-monitor-query/tests/perfstress_tests/batch_query.py index 3d366100b94c..ee4ac9eee8c4 100644 --- a/sdk/monitor/azure-monitor-query/tests/perfstress_tests/batch_query.py +++ b/sdk/monitor/azure-monitor-query/tests/perfstress_tests/batch_query.py @@ -1,8 +1,8 @@ -#------------------------------------------------------------------------- +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -#-------------------------------------------------------------------------- +# -------------------------------------------------------------------------- import asyncio from datetime import date, datetime, timezone @@ -14,42 +14,36 @@ from azure.identity import DefaultAzureCredential as SyncDefaultAzureCredential from azure.identity.aio import DefaultAzureCredential as AsyncDefaultAzureCredential + class LogsBatchPerfTest(PerfStressTest): def __init__(self, arguments): super().__init__(arguments) # auth configuration - self.workspace_id = self.get_from_env('LOG_WORKSPACE_ID') + self.workspace_id = self.get_from_env("LOG_WORKSPACE_ID") # Create clients - self.logs_client = SyncLogsQueryClient( - credential=SyncDefaultAzureCredential() - ) - self.async_logs_client = AsyncLogsQueryClient( - credential=AsyncDefaultAzureCredential() - ) + self.logs_client = SyncLogsQueryClient(credential=SyncDefaultAzureCredential()) + self.async_logs_client = AsyncLogsQueryClient(credential=AsyncDefaultAzureCredential()) self.requests = [ LogsBatchQuery( query="AzureActivity | summarize count()", start_time=datetime(2021, 7, 25, 0, 0, 0, tzinfo=timezone.utc), end_time=datetime(2021, 7, 26, 0, 0, 0, tzinfo=timezone.utc), - workspace_id= self.workspace_id, - timespan=None + workspace_id=self.workspace_id, + timespan=None, ), LogsBatchQuery( - query= """AppRequests | take 10 | + query="""AppRequests | take 10 | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""", start_time=datetime(2021, 7, 25, 0, 0, 0, tzinfo=timezone.utc), end_time=datetime(2021, 7, 26, 0, 0, 0, tzinfo=timezone.utc), - workspace_id= self.workspace_id, - timespan=None + workspace_id=self.workspace_id, + timespan=None, ), LogsBatchQuery( - query= "AppRequests | take 20", - workspace_id= self.workspace_id, - include_statistics=True, - timespan=None + query="AppRequests | take 20", workspace_id=self.workspace_id, include_statistics=True, timespan=None ), ] @@ -68,9 +62,7 @@ def run_sync(self): Avoid putting any ancillary logic (e.g. generating UUIDs), and put this in the setup/init instead so that we're only measuring the client API call. """ - self.logs_client.query_batch( - self.requests - ) + self.logs_client.query_batch(self.requests) async def run_async(self): """The asynchronous perf test. @@ -79,6 +71,4 @@ async def run_async(self): Avoid putting any ancillary logic (e.g. generating UUIDs), and put this in the setup/init instead so that we're only measuring the client API call. """ - await self.async_logs_client.query_batch( - self.requests - ) + await self.async_logs_client.query_batch(self.requests) 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 c765c0f15f89..1538774dd6b0 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 @@ -1,8 +1,8 @@ -#------------------------------------------------------------------------- +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -#-------------------------------------------------------------------------- +# -------------------------------------------------------------------------- import asyncio from datetime import datetime, timezone @@ -14,26 +14,23 @@ from azure.identity import DefaultAzureCredential as SyncDefaultAzureCredential from azure.identity.aio import DefaultAzureCredential as AsyncDefaultAzureCredential + class MetricsPerfTest(PerfStressTest): def __init__(self, arguments): super().__init__(arguments) # auth configuration - self.metrics_uri = self.get_from_env('METRICS_RESOURCE_URI') - self.names = ["MatchedEventCount"] + self.metrics_uri = self.get_from_env("METRICS_RESOURCE_URI") + self.names = ["MatchedEventCount"] self.aggregations = [MetricAggregationType.COUNT] # Create clients - self.metrics_client = SyncMetricsQueryClient( - credential=SyncDefaultAzureCredential() - ) - self.async_metrics_client = AsyncMetricsQueryClient( - credential=AsyncDefaultAzureCredential() - ) + self.metrics_client = SyncMetricsQueryClient(credential=SyncDefaultAzureCredential()) + self.async_metrics_client = AsyncMetricsQueryClient(credential=AsyncDefaultAzureCredential()) async def close(self): """This is run after cleanup. - + Use this to close any open handles or clients. """ await self.async_metrics_client.close() @@ -41,26 +38,18 @@ async def close(self): def run_sync(self): """The synchronous perf test. - + Try to keep this minimal and focused. Using only a single client API. Avoid putting any ancillary logic (e.g. generating UUIDs), and put this in the setup/init instead so that we're only measuring the client API call. """ - self.metrics_client.query_resource( - self.metrics_uri, - self.names, - aggregations=self.aggregations - ) + self.metrics_client.query_resource(self.metrics_uri, self.names, aggregations=self.aggregations) async def run_async(self): """The asynchronous perf test. - + Try to keep this minimal and focused. Using only a single client API. Avoid putting any ancillary logic (e.g. generating UUIDs), and put this in the setup/init instead so that we're only measuring the client API call. """ - await self.async_metrics_client.query_resource( - self.metrics_uri, - self.names, - aggregations=self.aggregations - ) + await self.async_metrics_client.query_resource(self.metrics_uri, self.names, aggregations=self.aggregations) diff --git a/sdk/monitor/azure-monitor-query/tests/perfstress_tests/single_query.py b/sdk/monitor/azure-monitor-query/tests/perfstress_tests/single_query.py index 2d5b140578d3..e70d18545c37 100644 --- a/sdk/monitor/azure-monitor-query/tests/perfstress_tests/single_query.py +++ b/sdk/monitor/azure-monitor-query/tests/perfstress_tests/single_query.py @@ -1,8 +1,8 @@ -#------------------------------------------------------------------------- +# ------------------------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. See License.txt in the project root for # license information. -#-------------------------------------------------------------------------- +# -------------------------------------------------------------------------- import asyncio from datetime import date, datetime, timezone @@ -14,26 +14,25 @@ from azure.identity import DefaultAzureCredential as SyncDefaultAzureCredential from azure.identity.aio import DefaultAzureCredential as AsyncDefaultAzureCredential + class LogsPerfTest(PerfStressTest): def __init__(self, arguments): super().__init__(arguments) # auth configuration - self.workspace_id = self.get_from_env('LOG_WORKSPACE_ID') + self.workspace_id = self.get_from_env("LOG_WORKSPACE_ID") - self.query = "AppRequests | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId" + self.query = ( + "AppRequests | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId" + ) # Create clients - self.logs_client = SyncLogsQueryClient( - credential=SyncDefaultAzureCredential() - ) - self.async_logs_client = AsyncLogsQueryClient( - credential=AsyncDefaultAzureCredential() - ) + self.logs_client = SyncLogsQueryClient(credential=SyncDefaultAzureCredential()) + self.async_logs_client = AsyncLogsQueryClient(credential=AsyncDefaultAzureCredential()) async def close(self): """This is run after cleanup. - + Use this to close any open handles or clients. """ await self.async_logs_client.close() @@ -41,30 +40,22 @@ async def close(self): def run_sync(self): """The synchronous perf test. - + Try to keep this minimal and focused. Using only a single client API. Avoid putting any ancillary logic (e.g. generating UUIDs), and put this in the setup/init instead so that we're only measuring the client API call. """ - start_time=datetime(2021, 7, 25, 0, 0, 0, tzinfo=timezone.utc) - end_time=datetime(2021, 7, 26, 0, 0, 0, tzinfo=timezone.utc) - self.logs_client.query_workspace( - self.workspace_id, - self.query, - timespan=(start_time, end_time) - ) + start_time = datetime(2021, 7, 25, 0, 0, 0, tzinfo=timezone.utc) + end_time = datetime(2021, 7, 26, 0, 0, 0, tzinfo=timezone.utc) + self.logs_client.query_workspace(self.workspace_id, self.query, timespan=(start_time, end_time)) async def run_async(self): """The asynchronous perf test. - + Try to keep this minimal and focused. Using only a single client API. Avoid putting any ancillary logic (e.g. generating UUIDs), and put this in the setup/init instead so that we're only measuring the client API call. """ - start_time=datetime(2021, 7, 25, 0, 0, 0, tzinfo=timezone.utc) - end_time=datetime(2021, 7, 26, 0, 0, 0, tzinfo=timezone.utc) - await self.async_logs_client.query_workspace( - self.workspace_id, - self.query, - timespan=(start_time, end_time) - ) + start_time = datetime(2021, 7, 25, 0, 0, 0, tzinfo=timezone.utc) + end_time = datetime(2021, 7, 26, 0, 0, 0, tzinfo=timezone.utc) + await self.async_logs_client.query_workspace(self.workspace_id, self.query, timespan=(start_time, end_time)) diff --git a/sdk/monitor/azure-monitor-query/tests/test_exceptions.py b/sdk/monitor/azure-monitor-query/tests/test_exceptions.py index 0b96f95a8984..e0ed547b752e 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_exceptions.py +++ b/sdk/monitor/azure-monitor-query/tests/test_exceptions.py @@ -9,7 +9,14 @@ from azure.identity import ClientSecretCredential from azure.core.exceptions import HttpResponseError -from azure.monitor.query import LogsQueryClient, LogsBatchQuery, LogsQueryError, LogsQueryResult, LogsQueryPartialResult, LogsQueryStatus +from azure.monitor.query import ( + LogsQueryClient, + LogsBatchQuery, + LogsQueryError, + LogsQueryResult, + LogsQueryPartialResult, + LogsQueryStatus, +) from base_testcase import AzureMonitorQueryLogsTestCase @@ -19,66 +26,66 @@ class TestQueryExceptions(AzureMonitorQueryLogsTestCase): def test_logs_single_query_fatal_exception(self, recorded_test): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) with pytest.raises(HttpResponseError): - client.query_workspace('bad_workspace_id', 'AppRequests', timespan=None) + client.query_workspace("bad_workspace_id", "AppRequests", timespan=None) def test_logs_single_query_partial_exception(self, recorded_test, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) query = """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""" - response = client.query_workspace(monitor_info['workspace_id'], query, timespan=timedelta(days=1)) + response = client.query_workspace(monitor_info["workspace_id"], query, timespan=timedelta(days=1)) assert response.__class__ == LogsQueryPartialResult assert response.status == LogsQueryStatus.PARTIAL assert response.partial_error is not None assert response.partial_data is not None assert response.partial_error.details is not None - assert response.partial_error.code == 'PartialError' + assert response.partial_error.code == "PartialError" assert response.partial_error.__class__ == LogsQueryError def test_logs_resource_query_fatal_exception(self, recorded_test): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) with pytest.raises(HttpResponseError): - client.query_resource('/bad/resource/id', 'AzureActivity', timespan=None) + client.query_resource("/bad/resource/id", "AzureActivity", timespan=None) def test_logs_resource_query_partial_exception(self, recorded_test, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) query = """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""" - response = client.query_resource(monitor_info['metrics_resource_id'], query, timespan=timedelta(days=1)) + response = client.query_resource(monitor_info["metrics_resource_id"], query, timespan=timedelta(days=1)) assert response.__class__ == LogsQueryPartialResult assert response.status == LogsQueryStatus.PARTIAL assert response.partial_error is not None assert response.partial_data is not None assert response.partial_error.details is not None - assert response.partial_error.code == 'PartialError' + assert response.partial_error.code == "PartialError" assert response.partial_error.__class__ == LogsQueryError def test_logs_batch_query_fatal_exception(self, recorded_test, monitor_info): - credential = ClientSecretCredential( - client_id = "00000000-0000-0000-0000-000000000000", - client_secret = "bad_secret", - tenant_id = "00000000-0000-0000-0000-000000000000" + credential = ClientSecretCredential( + client_id="00000000-0000-0000-0000-000000000000", + client_secret="bad_secret", + tenant_id="00000000-0000-0000-0000-000000000000", ) client = self.get_client(LogsQueryClient, credential) requests = [ LogsBatchQuery( query="AzureActivity | summarize count()", timespan=timedelta(hours=1), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """AppRequestsss | take 10""", + query="""AppRequestsss | take 10""", timespan=(datetime(2021, 6, 2), timedelta(days=1)), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """let Weight = 92233720368547758; + query="""let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""", - workspace_id=monitor_info['workspace_id'], + workspace_id=monitor_info["workspace_id"], timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)), - include_statistics=True + include_statistics=True, ), ] with pytest.raises(HttpResponseError): @@ -91,20 +98,20 @@ def test_logs_batch_query_partial_exception(self, monitor_info): LogsBatchQuery( query="AzureActivity | summarize count()", timespan=timedelta(hours=1), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """AppRequests | take 10""", + query="""AppRequests | take 10""", timespan=(datetime(2021, 6, 2), timedelta(days=1)), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """let Weight = 92233720368547758; + query="""let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""", - workspace_id=monitor_info['workspace_id'], + workspace_id=monitor_info["workspace_id"], timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)), - include_statistics=True + include_statistics=True, ), ] responses = client.query_batch(requests) @@ -120,18 +127,18 @@ def test_logs_batch_query_non_fatal_exception(self, monitor_info): LogsBatchQuery( query="AzureActivity | summarize count()", timespan=timedelta(hours=1), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """AppRequests | take 10""", + query="""AppRequests | take 10""", timespan=(datetime(2021, 6, 2), timedelta(days=1)), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """Bad Query""", - workspace_id=monitor_info['workspace_id'], + query="""Bad Query""", + workspace_id=monitor_info["workspace_id"], timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)), - include_statistics=True + include_statistics=True, ), ] responses = client.query_batch(requests) diff --git a/sdk/monitor/azure-monitor-query/tests/test_exceptions_async.py b/sdk/monitor/azure-monitor-query/tests/test_exceptions_async.py index 6dabeb585b28..9dd918927d40 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_exceptions_async.py +++ b/sdk/monitor/azure-monitor-query/tests/test_exceptions_async.py @@ -9,7 +9,7 @@ from azure.identity.aio import ClientSecretCredential from azure.core.exceptions import HttpResponseError -from azure.monitor.query import LogsBatchQuery, LogsQueryError,LogsQueryResult, LogsQueryPartialResult, LogsQueryStatus +from azure.monitor.query import LogsBatchQuery, LogsQueryError, LogsQueryResult, LogsQueryPartialResult, LogsQueryStatus from azure.monitor.query.aio import LogsQueryClient from base_testcase import AzureMonitorQueryLogsTestCase @@ -19,55 +19,53 @@ class TestQueryExceptionsAsync(AzureMonitorQueryLogsTestCase): @pytest.mark.asyncio async def test_logs_single_query_fatal_exception(self, recorded_test): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: with pytest.raises(HttpResponseError): - await client.query_workspace('bad_workspace_id', 'AppRequests', timespan=None) + await client.query_workspace("bad_workspace_id", "AppRequests", timespan=None) @pytest.mark.asyncio async def test_logs_single_query_partial_exception_not_allowed(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""" - response = await client.query_workspace(monitor_info['workspace_id'], query, timespan=timedelta(days=1)) + response = await client.query_workspace(monitor_info["workspace_id"], query, timespan=timedelta(days=1)) assert response.__class__ == LogsQueryPartialResult assert response.status == LogsQueryStatus.PARTIAL assert response.partial_error is not None - assert response.partial_error.code == 'PartialError' + assert response.partial_error.code == "PartialError" assert response.partial_error.__class__ == LogsQueryError @pytest.mark.asyncio async def test_logs_resource_query_fatal_exception(self, recorded_test): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: with pytest.raises(HttpResponseError): - await client.query_resource('/bad/resource/id', 'AzureActivity', timespan=None) + await client.query_resource("/bad/resource/id", "AzureActivity", timespan=None) @pytest.mark.asyncio async def test_logs_resource_query_partial_exception(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""" - response = await client.query_resource(monitor_info['metrics_resource_id'], query, timespan=timedelta(days=1)) + response = await client.query_resource( + monitor_info["metrics_resource_id"], query, timespan=timedelta(days=1) + ) assert response.__class__ == LogsQueryPartialResult assert response.partial_error is not None - assert response.partial_error.code == 'PartialError' + assert response.partial_error.code == "PartialError" assert response.partial_error.__class__ == LogsQueryError @pytest.mark.asyncio async def test_logs_batch_query_fatal_exception(self, recorded_test, monitor_info): - credential = ClientSecretCredential( - client_id = "00000000-0000-0000-0000-000000000000", - client_secret = "bad_secret", - tenant_id = "00000000-0000-0000-0000-000000000000" + credential = ClientSecretCredential( + client_id="00000000-0000-0000-0000-000000000000", + client_secret="bad_secret", + tenant_id="00000000-0000-0000-0000-000000000000", ) client = self.get_client(LogsQueryClient, credential) async with client: @@ -75,20 +73,20 @@ async def test_logs_batch_query_fatal_exception(self, recorded_test, monitor_inf LogsBatchQuery( query="AzureActivity | summarize count()", timespan=timedelta(hours=1), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """AppRequestsss | take 10""", + query="""AppRequestsss | take 10""", timespan=(datetime(2021, 6, 2), timedelta(days=1)), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """let Weight = 92233720368547758; + query="""let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""", - workspace_id=monitor_info['workspace_id'], + workspace_id=monitor_info["workspace_id"], timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)), - include_statistics=True + include_statistics=True, ), ] with pytest.raises(HttpResponseError): @@ -97,27 +95,26 @@ async def test_logs_batch_query_fatal_exception(self, recorded_test, monitor_inf @pytest.mark.live_test_only("Issues recording dynamic 'id' values in requests/responses") @pytest.mark.asyncio async def test_logs_batch_query_partial_exception_not_allowed(self, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: requests = [ LogsBatchQuery( query="AzureActivity | summarize count()", timespan=timedelta(hours=1), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """bad query | take 10""", + query="""bad query | take 10""", timespan=(datetime(2021, 6, 2), timedelta(days=1)), - workspace_id=monitor_info['workspace_id'] + workspace_id=monitor_info["workspace_id"], ), LogsBatchQuery( - query= """let Weight = 92233720368547758; + query="""let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""", - workspace_id=monitor_info['workspace_id'], + workspace_id=monitor_info["workspace_id"], timespan=(datetime(2021, 6, 2), datetime(2021, 6, 3)), - include_statistics=True + include_statistics=True, ), ] responses = await client.query_batch(requests) diff --git a/sdk/monitor/azure-monitor-query/tests/test_helpers.py b/sdk/monitor/azure-monitor-query/tests/test_helpers.py index 482e19b52d7e..9930163d9b34 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_helpers.py +++ b/sdk/monitor/azure-monitor-query/tests/test_helpers.py @@ -6,21 +6,24 @@ from datetime import datetime, timedelta import pytest -from azure.monitor.query._helpers import ( - get_subscription_id_from_resource, - get_timespan_iso8601_endpoints -) +from azure.monitor.query._helpers import get_subscription_id_from_resource, get_timespan_iso8601_endpoints def test_get_subscription_id_from_resource(): - assert get_subscription_id_from_resource( - "/subscriptions/00000000-1111-2222-3333-000000000000/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm" - ) == "00000000-1111-2222-3333-000000000000" + assert ( + get_subscription_id_from_resource( + "/subscriptions/00000000-1111-2222-3333-000000000000/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm" + ) + == "00000000-1111-2222-3333-000000000000" + ) # Test witout preceding slash - assert get_subscription_id_from_resource( - "subscriptions/00000000-1111-2222-3333-000000000000/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm" - ) == "00000000-1111-2222-3333-000000000000" + assert ( + get_subscription_id_from_resource( + "subscriptions/00000000-1111-2222-3333-000000000000/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm" + ) + == "00000000-1111-2222-3333-000000000000" + ) with pytest.raises(ValueError): get_subscription_id_from_resource("/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm") diff --git a/sdk/monitor/azure-monitor-query/tests/test_logs_client.py b/sdk/monitor/azure-monitor-query/tests/test_logs_client.py index f18189023517..ca5bf59b7c6b 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_logs_client.py +++ b/sdk/monitor/azure-monitor-query/tests/test_logs_client.py @@ -16,7 +16,7 @@ LogsQueryResult, LogsTableRow, LogsQueryPartialResult, - LogsQueryStatus + LogsQueryStatus, ) from azure.monitor.query._helpers import native_col_type @@ -32,7 +32,7 @@ def test_logs_single_query(self, recorded_test, monitor_info): summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""" # returns LogsQueryResult - response = client.query_workspace(monitor_info['workspace_id'], query, timespan=None) + response = client.query_workspace(monitor_info["workspace_id"], query, timespan=None) assert response is not None assert response.status == LogsQueryStatus.SUCCESS @@ -46,7 +46,7 @@ def test_logs_single_query_raises_no_timespan(self, monitor_info): # returns LogsQueryResult with pytest.raises(TypeError): - client.query_workspace(monitor_info['workspace_id'], query) + client.query_workspace(monitor_info["workspace_id"], query) def test_logs_single_query_with_non_200(self, recorded_test, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) @@ -54,7 +54,7 @@ def test_logs_single_query_with_non_200(self, recorded_test, monitor_info): where TimeGenerated > ago(12h)""" with pytest.raises(HttpResponseError) as e: - client.query_workspace(monitor_info['workspace_id'], query, timespan=None) + client.query_workspace(monitor_info["workspace_id"], query, timespan=None) assert "SemanticError" in e.value.message @@ -63,7 +63,7 @@ def test_logs_single_query_with_partial_success(self, recorded_test, monitor_inf query = """let Weight = 92233720368547758; range x from 1 to 3 step 1 | summarize percentilesw(x, Weight * 100, 50)""" - response = client.query_workspace(monitor_info['workspace_id'], query, timespan=None) + response = client.query_workspace(monitor_info["workspace_id"], query, timespan=None) assert response.partial_error is not None assert response.partial_data is not None @@ -88,39 +88,34 @@ def test_logs_server_timeout(self, recorded_test, monitor_info): assert response.status == LogsQueryStatus.PARTIAL assert "timed out" in str(response.partial_error) - @pytest.mark.live_test_only("Issues recording dynamic 'id' values in requests/responses") def test_logs_query_batch_default(self, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) requests = [ LogsBatchQuery( - workspace_id=monitor_info['workspace_id'], + workspace_id=monitor_info["workspace_id"], query="AzureActivity | summarize count()", - timespan=timedelta(hours=1) + timespan=timedelta(hours=1), ), LogsBatchQuery( - workspace_id=monitor_info['workspace_id'], - query= """AppRequests | take 10 | + workspace_id=monitor_info["workspace_id"], + query="""AppRequests | take 10 | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""", timespan=timedelta(hours=1), ), - LogsBatchQuery( - workspace_id=monitor_info['workspace_id'], - query= "Wrong query | take 2", - timespan=None - ), + LogsBatchQuery(workspace_id=monitor_info["workspace_id"], query="Wrong query | take 2", timespan=None), ] response = client.query_batch(requests) assert len(response) == 3 r0 = response[0] - assert r0.tables[0].columns == ['count_'] + assert r0.tables[0].columns == ["count_"] r1 = response[1] - assert r1.tables[0].columns[0] == 'TimeGenerated' - assert r1.tables[0].columns[1] == '_ResourceId' - assert r1.tables[0].columns[2] == 'avgRequestDuration' + assert r1.tables[0].columns[0] == "TimeGenerated" + assert r1.tables[0].columns[1] == "_ResourceId" + assert r1.tables[0].columns[2] == "avgRequestDuration" r2 = response[2] assert r2.__class__ == LogsQueryError @@ -129,7 +124,7 @@ def test_logs_single_query_with_statistics(self, recorded_test, monitor_info): query = """AppRequests | take 10""" # returns LogsQueryResult - response = client.query_workspace(monitor_info['workspace_id'], query, timespan=None, include_statistics=True) + response = client.query_workspace(monitor_info["workspace_id"], query, timespan=None, include_statistics=True) assert response.statistics is not None @@ -139,7 +134,8 @@ def test_logs_single_query_with_visualization(self, recorded_test, monitor_info) # returns LogsQueryResult response = client.query_workspace( - monitor_info['workspace_id'], query, timespan=None, include_visualization=True) + monitor_info["workspace_id"], query, timespan=None, include_visualization=True + ) assert response.visualization is not None @@ -149,7 +145,8 @@ def test_logs_single_query_with_visualization_and_stats(self, recorded_test, mon # returns LogsQueryResult response = client.query_workspace( - monitor_info['workspace_id'], query, timespan=None, include_visualization=True, include_statistics=True) + monitor_info["workspace_id"], query, timespan=None, include_visualization=True, include_statistics=True + ) assert response.visualization is not None assert response.statistics is not None @@ -160,22 +157,19 @@ def test_logs_query_batch_with_statistics_in_some(self, monitor_info): requests = [ LogsBatchQuery( - workspace_id=monitor_info['workspace_id'], + workspace_id=monitor_info["workspace_id"], query="AzureActivity | summarize count()", timespan=timedelta(hours=1), ), LogsBatchQuery( - workspace_id=monitor_info['workspace_id'], - query= """AppRequests| + workspace_id=monitor_info["workspace_id"], + query="""AppRequests| summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""", timespan=timedelta(hours=1), - include_statistics=True + include_statistics=True, ), LogsBatchQuery( - workspace_id=monitor_info['workspace_id'], - query= "AppRequests", - timespan=None, - include_statistics=True + workspace_id=monitor_info["workspace_id"], query="AppRequests", timespan=None, include_statistics=True ), ] response = client.query_batch(requests) @@ -192,11 +186,11 @@ def test_logs_single_query_additional_workspaces(self, recorded_test, monitor_in ) # returns LogsQueryResult response = client.query_workspace( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, - additional_workspaces=[monitor_info['secondary_workspace_id']], - ) + additional_workspaces=[monitor_info["secondary_workspace_id"]], + ) assert response is not None assert len(response.tables[0].rows) == 2 @@ -211,22 +205,22 @@ def test_logs_query_batch_additional_workspaces(self, monitor_info): ) requests = [ LogsBatchQuery( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, - additional_workspaces=[monitor_info['secondary_workspace_id']] + additional_workspaces=[monitor_info["secondary_workspace_id"]], ), LogsBatchQuery( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, - additional_workspaces=[monitor_info['secondary_workspace_id']] + additional_workspaces=[monitor_info["secondary_workspace_id"]], ), LogsBatchQuery( - monitor_info['workspace_id'], - query, - timespan=None, - additional_workspaces=[monitor_info['secondary_workspace_id']] + monitor_info["workspace_id"], + query, + timespan=None, + additional_workspaces=[monitor_info["secondary_workspace_id"]], ), ] response = client.query_batch(requests) @@ -239,11 +233,7 @@ def test_logs_query_result_iterate_over_tables(self, recorded_test, monitor_info query = "AppRequests | take 10; AppRequests | take 5" response = client.query_workspace( - monitor_info['workspace_id'], - query, - timespan=None, - include_statistics=True, - include_visualization=True + monitor_info["workspace_id"], query, timespan=None, include_statistics=True, include_visualization=True ) ## should iterate over tables @@ -261,7 +251,7 @@ def test_logs_query_result_row_type(self, recorded_test, monitor_info): query = "AppRequests | take 5" response = client.query_workspace( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, ) @@ -274,17 +264,17 @@ def test_logs_query_result_row_type(self, recorded_test, monitor_info): assert row.__class__ == LogsTableRow def test_native_col_type(self): - val = native_col_type('datetime', None) + val = native_col_type("datetime", None) assert val is None - val = native_col_type('datetime', '2020-10-10') + val = native_col_type("datetime", "2020-10-10") assert val is not None def test_logs_resource_query(self, recorded_test, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) query = "requests | summarize count()" - response = client.query_resource(monitor_info['metrics_resource_id'], query, timespan=None) + response = client.query_resource(monitor_info["metrics_resource_id"], query, timespan=None) assert response is not None assert response.tables is not None @@ -295,11 +285,11 @@ def test_logs_resource_query_additional_options(self, recorded_test, monitor_inf query = "requests | summarize count()" response = client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], query, timespan=None, include_statistics=True, - include_visualization=True + include_visualization=True, ) assert response.visualization is not None diff --git a/sdk/monitor/azure-monitor-query/tests/test_logs_client_async.py b/sdk/monitor/azure-monitor-query/tests/test_logs_client_async.py index c021da083d74..35840212a77c 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_logs_client_async.py +++ b/sdk/monitor/azure-monitor-query/tests/test_logs_client_async.py @@ -8,7 +8,14 @@ import pytest from azure.core.exceptions import HttpResponseError -from azure.monitor.query import LogsBatchQuery, LogsQueryError, LogsTable, LogsQueryResult, LogsTableRow, LogsQueryStatus +from azure.monitor.query import ( + LogsBatchQuery, + LogsQueryError, + LogsTable, + LogsQueryResult, + LogsTableRow, + LogsQueryStatus, +) from azure.monitor.query.aio import LogsQueryClient from base_testcase import AzureMonitorQueryLogsTestCase @@ -18,15 +25,14 @@ class TestLogsClientAsync(AzureMonitorQueryLogsTestCase): @pytest.mark.asyncio async def test_logs_auth(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = """AppRequests | where TimeGenerated > ago(12h) | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""" # returns LogsQueryResult - response = await client.query_workspace(monitor_info['workspace_id'], query, timespan=None) + response = await client.query_workspace(monitor_info["workspace_id"], query, timespan=None) assert response is not None assert response.status == LogsQueryStatus.SUCCESS @@ -34,8 +40,7 @@ async def test_logs_auth(self, recorded_test, monitor_info): @pytest.mark.asyncio async def test_logs_auth_no_timespan(self, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = """AppRequests | where TimeGenerated > ago(12h) | @@ -43,7 +48,7 @@ async def test_logs_auth_no_timespan(self, monitor_info): # returns LogsQueryResult with pytest.raises(TypeError): - await client.query_workspace(monitor_info['workspace_id'], query) + await client.query_workspace(monitor_info["workspace_id"], query) @pytest.mark.asyncio async def test_logs_server_timeout(self, recorded_test, monitor_info): @@ -69,50 +74,42 @@ async def test_logs_server_timeout(self, recorded_test, monitor_info): async def test_logs_query_batch_raises_on_no_timespan(self, monitor_info): with pytest.raises(TypeError): LogsBatchQuery( - workspace_id=monitor_info['workspace_id'], + workspace_id=monitor_info["workspace_id"], query="AzureActivity | summarize count()", ) @pytest.mark.live_test_only("Issues recording dynamic 'id' values in requests/responses") @pytest.mark.asyncio async def test_logs_query_batch_default(self, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: requests = [ LogsBatchQuery( - monitor_info['workspace_id'], - query="AzureActivity | summarize count()", - timespan=timedelta(hours=1) + monitor_info["workspace_id"], query="AzureActivity | summarize count()", timespan=timedelta(hours=1) ), LogsBatchQuery( - monitor_info['workspace_id'], - query= """AppRequests | take 10 | + monitor_info["workspace_id"], + query="""AppRequests | take 10 | summarize avgRequestDuration=avg(DurationMs) by bin(TimeGenerated, 10m), _ResourceId""", timespan=timedelta(hours=1), ), - LogsBatchQuery( - monitor_info['workspace_id'], - query= "Wrong query | take 2", - timespan=None - ), + LogsBatchQuery(monitor_info["workspace_id"], query="Wrong query | take 2", timespan=None), ] response = await client.query_batch(requests) assert len(response) == 3 r0 = response[0] - assert r0.tables[0].columns == ['count_'] + assert r0.tables[0].columns == ["count_"] r1 = response[1] - assert r1.tables[0].columns[0] == 'TimeGenerated' - assert r1.tables[0].columns[1] == '_ResourceId' - assert r1.tables[0].columns[2] == 'avgRequestDuration' + assert r1.tables[0].columns[0] == "TimeGenerated" + assert r1.tables[0].columns[1] == "_ResourceId" + assert r1.tables[0].columns[2] == "avgRequestDuration" r2 = response[2] assert r2.__class__ == LogsQueryError @pytest.mark.asyncio async def test_logs_single_query_additional_workspaces_async(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = ( f"{monitor_info['table_name']} | where TimeGenerated > ago(100d)" @@ -121,11 +118,11 @@ async def test_logs_single_query_additional_workspaces_async(self, recorded_test # returns LogsQueryResult response = await client.query_workspace( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, - additional_workspaces=[monitor_info['secondary_workspace_id']], - ) + additional_workspaces=[monitor_info["secondary_workspace_id"]], + ) assert response assert len(response.tables[0].rows) == 2 @@ -134,8 +131,7 @@ async def test_logs_single_query_additional_workspaces_async(self, recorded_test @pytest.mark.live_test_only("Issues recording dynamic 'id' values in requests/responses") @pytest.mark.asyncio async def test_logs_query_batch_additional_workspaces(self, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = ( f"{monitor_info['table_name']} | where TimeGenerated > ago(100d)" @@ -143,22 +139,22 @@ async def test_logs_query_batch_additional_workspaces(self, monitor_info): ) requests = [ LogsBatchQuery( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, - additional_workspaces=[monitor_info['secondary_workspace_id']] + additional_workspaces=[monitor_info["secondary_workspace_id"]], ), LogsBatchQuery( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, - additional_workspaces=[monitor_info['secondary_workspace_id']] + additional_workspaces=[monitor_info["secondary_workspace_id"]], ), LogsBatchQuery( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, - additional_workspaces=[monitor_info['secondary_workspace_id']] + additional_workspaces=[monitor_info["secondary_workspace_id"]], ), ] response = await client.query_batch(requests) @@ -169,42 +165,37 @@ async def test_logs_query_batch_additional_workspaces(self, monitor_info): @pytest.mark.asyncio async def test_logs_single_query_with_visualization(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = """AppRequests | take 10""" # returns LogsQueryResult response = await client.query_workspace( - monitor_info['workspace_id'], query, timespan=None, include_visualization=True) + monitor_info["workspace_id"], query, timespan=None, include_visualization=True + ) assert response.visualization is not None @pytest.mark.asyncio async def test_logs_single_query_with_visualization_and_stats(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = """AppRequests | take 10""" # returns LogsQueryResult response = await client.query_workspace( - monitor_info['workspace_id'], query, timespan=None, include_visualization=True, include_statistics=True) + monitor_info["workspace_id"], query, timespan=None, include_visualization=True, include_statistics=True + ) assert response.visualization is not None assert response.statistics is not None @pytest.mark.asyncio async def test_logs_query_result_iterate_over_tables(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = "AppRequests | take 10; AppRequests | take 5" response = await client.query_workspace( - monitor_info['workspace_id'], - query, - timespan=None, - include_statistics=True, - include_visualization=True + monitor_info["workspace_id"], query, timespan=None, include_statistics=True, include_visualization=True ) ## should iterate over tables @@ -218,12 +209,11 @@ async def test_logs_query_result_iterate_over_tables(self, recorded_test, monito @pytest.mark.asyncio async def test_logs_query_result_row_type(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = "AppRequests | take 5" response = await client.query_workspace( - monitor_info['workspace_id'], + monitor_info["workspace_id"], query, timespan=None, ) @@ -237,12 +227,11 @@ async def test_logs_query_result_row_type(self, recorded_test, monitor_info): @pytest.mark.asyncio async def test_logs_resource_query(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = "requests | summarize count()" - response = await client.query_resource(monitor_info['metrics_resource_id'], query, timespan=None) + response = await client.query_resource(monitor_info["metrics_resource_id"], query, timespan=None) assert response is not None assert response.tables is not None @@ -250,17 +239,16 @@ async def test_logs_resource_query(self, recorded_test, monitor_info): @pytest.mark.asyncio async def test_logs_resource_query_additional_options(self, recorded_test, monitor_info): - client = self.get_client( - LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) + client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient, is_async=True)) async with client: query = "requests | summarize count()" response = await client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], query, timespan=None, include_statistics=True, - include_visualization=True + include_visualization=True, ) assert response.visualization is not None diff --git a/sdk/monitor/azure-monitor-query/tests/test_logs_response.py b/sdk/monitor/azure-monitor-query/tests/test_logs_response.py index 6cd581b25e1c..544d432ef8cd 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_logs_response.py +++ b/sdk/monitor/azure-monitor-query/tests/test_logs_response.py @@ -23,14 +23,15 @@ def test_query_response_data(self, recorded_test, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) query = ( f"{monitor_info['table_name']} | project TimeGenerated, Type, ExtendedColumn, AdditionalContext" - f"| order by TimeGenerated desc | take 5") + f"| order by TimeGenerated desc | take 5" + ) # returns LogsQueryResult - result = client.query_workspace(monitor_info['workspace_id'], query, timespan=None) + result = client.query_workspace(monitor_info["workspace_id"], query, timespan=None) assert isinstance(result.tables[0].rows[0][0], datetime) assert isinstance(result.tables[0].rows[0][1], str) - assert result.tables[0].rows[0][1] == monitor_info['table_name'] + assert result.tables[0].rows[0][1] == monitor_info["table_name"] assert isinstance(result.tables[0].rows[0][2], str) # Check if DCR transformation correctly populated the ExtendedColumn field. @@ -50,7 +51,7 @@ def test_query_response_types(self, recorded_test, monitor_info): longcolumn=print_4, realcolumn=print_5 """ - result = client.query_workspace(monitor_info['workspace_id'], query, timespan=None) + result = client.query_workspace(monitor_info["workspace_id"], query, timespan=None) assert isinstance(result.tables[0].rows[0][0], str) assert result.tables[0].rows[0][0] == "hello" diff --git a/sdk/monitor/azure-monitor-query/tests/test_logs_timespans.py b/sdk/monitor/azure-monitor-query/tests/test_logs_timespans.py index 4877cd727a12..4a00fbc9d07e 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_logs_timespans.py +++ b/sdk/monitor/azure-monitor-query/tests/test_logs_timespans.py @@ -25,9 +25,10 @@ def test_query_no_duration(self, recorded_test, monitor_info): def callback(request): dic = json.loads(request.http_request.body) - assert dic.get('timespan') is None + assert dic.get("timespan") is None + # returns LogsQueryResult - client.query_workspace(monitor_info['workspace_id'], query, timespan=None) + client.query_workspace(monitor_info["workspace_id"], query, timespan=None) def test_query_start_and_end_time(self, recorded_test, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) @@ -38,9 +39,11 @@ def test_query_start_and_end_time(self, recorded_test, monitor_info): def callback(request): dic = json.loads(request.http_request.body) - assert dic.get('timespan') is not None + assert dic.get("timespan") is not None - client.query_workspace(monitor_info['workspace_id'], query, timespan=(start_time, end_time), raw_request_hook=callback) + client.query_workspace( + monitor_info["workspace_id"], query, timespan=(start_time, end_time), raw_request_hook=callback + ) def test_query_duration_and_start_time(self, recorded_test, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) @@ -52,9 +55,11 @@ def test_query_duration_and_start_time(self, recorded_test, monitor_info): def callback(request): dic = json.loads(request.http_request.body) - assert '/PT259200.0S' in dic.get('timespan') + assert "/PT259200.0S" in dic.get("timespan") - client.query_workspace(monitor_info['workspace_id'], query, timespan=(start_time,duration), raw_request_hook=callback) + client.query_workspace( + monitor_info["workspace_id"], query, timespan=(start_time, duration), raw_request_hook=callback + ) def test_query_duration_only(self, recorded_test, monitor_info): client = self.get_client(LogsQueryClient, self.get_credential(LogsQueryClient)) @@ -64,9 +69,9 @@ def test_query_duration_only(self, recorded_test, monitor_info): def callback(request): dic = json.loads(request.http_request.body) - assert 'PT259200.0S' in dic.get('timespan') + assert "PT259200.0S" in dic.get("timespan") - client.query_workspace(monitor_info['workspace_id'], query, timespan=duration, raw_request_hook=callback) + client.query_workspace(monitor_info["workspace_id"], query, timespan=duration, raw_request_hook=callback) def test_duration_to_iso8601(self): d1 = timedelta(days=1) @@ -77,13 +82,13 @@ def test_duration_to_iso8601(self): d6 = timedelta(milliseconds=100000) d7 = timedelta(hours=24, days=1) - assert construct_iso8601(timespan=d1) == 'PT86400.0S' - assert construct_iso8601(timespan=d2) == 'PT604800.0S' - assert construct_iso8601(timespan=d3) == 'PT2160000.0S' - assert construct_iso8601(timespan=d4) == 'PT10.0S' - assert construct_iso8601(timespan=d5) == 'PT0.001S' - assert construct_iso8601(timespan=d5) == 'PT0.001S' - assert construct_iso8601(timespan=d7) == 'PT172800.0S' + assert construct_iso8601(timespan=d1) == "PT86400.0S" + assert construct_iso8601(timespan=d2) == "PT604800.0S" + assert construct_iso8601(timespan=d3) == "PT2160000.0S" + assert construct_iso8601(timespan=d4) == "PT10.0S" + assert construct_iso8601(timespan=d5) == "PT0.001S" + assert construct_iso8601(timespan=d5) == "PT0.001S" + assert construct_iso8601(timespan=d7) == "PT172800.0S" with pytest.raises(ValueError, match="timespan must be a timedelta or a tuple."): construct_iso8601(timespan=(datetime.now(UTC()))) 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 6cfb047119a3..2e4371946685 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py +++ b/sdk/monitor/azure-monitor-query/tests/test_metrics_client.py @@ -19,7 +19,7 @@ class TestMetricsClient(MetricsClientTestCase): def test_batch_metrics_auth(self, recorded_test, monitor_info): client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient)) responses = client.query_resources( - resource_ids=[monitor_info['metrics_resource_id']], + resource_ids=[monitor_info["metrics_resource_id"]], metric_namespace=METRIC_RESOURCE_PROVIDER, metric_names=[METRIC_NAME], aggregations=[MetricAggregationType.COUNT], @@ -30,7 +30,7 @@ def test_batch_metrics_auth(self, recorded_test, monitor_info): def test_batch_metrics_granularity(self, recorded_test, monitor_info): client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient)) responses = client.query_resources( - resource_ids=[monitor_info['metrics_resource_id']], + resource_ids=[monitor_info["metrics_resource_id"]], metric_namespace=METRIC_RESOURCE_PROVIDER, metric_names=[METRIC_NAME], granularity=timedelta(minutes=5), diff --git a/sdk/monitor/azure-monitor-query/tests/test_metrics_client_async.py b/sdk/monitor/azure-monitor-query/tests/test_metrics_client_async.py index 574527967528..8e3c6e2bc1ff 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_metrics_client_async.py +++ b/sdk/monitor/azure-monitor-query/tests/test_metrics_client_async.py @@ -21,11 +21,10 @@ class TestMetricsClientAsync(MetricsClientTestCase): @pytest.mark.asyncio async def test_batch_metrics_auth(self, recorded_test, monitor_info): - client: MetricsClient = self.get_client( - MetricsClient, self.get_credential(MetricsClient, is_async=True)) + client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient, is_async=True)) async with client: responses = await client.query_resources( - resource_ids=[monitor_info['metrics_resource_id']], + resource_ids=[monitor_info["metrics_resource_id"]], metric_namespace=METRIC_RESOURCE_PROVIDER, metric_names=[METRIC_NAME], aggregations=[MetricAggregationType.COUNT], @@ -35,11 +34,10 @@ async def test_batch_metrics_auth(self, recorded_test, monitor_info): @pytest.mark.asyncio async def test_batch_metrics_granularity(self, recorded_test, monitor_info): - client: MetricsClient = self.get_client( - MetricsClient, self.get_credential(MetricsClient, is_async=True)) + client: MetricsClient = self.get_client(MetricsClient, self.get_credential(MetricsClient, is_async=True)) async with client: responses = await client.query_resources( - resource_ids=[monitor_info['metrics_resource_id']], + resource_ids=[monitor_info["metrics_resource_id"]], metric_namespace=METRIC_RESOURCE_PROVIDER, metric_names=[METRIC_NAME], granularity=timedelta(minutes=5), diff --git a/sdk/monitor/azure-monitor-query/tests/test_metrics_query_client.py b/sdk/monitor/azure-monitor-query/tests/test_metrics_query_client.py index bab59b0610bd..55c772828456 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_metrics_query_client.py +++ b/sdk/monitor/azure-monitor-query/tests/test_metrics_query_client.py @@ -20,23 +20,23 @@ class TestMetricsQueryClient(MetricsQueryClientTestCase): def test_metrics_auth(self, recorded_test, monitor_info): client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient)) response = client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], metric_names=[METRIC_NAME], timespan=timedelta(days=1), - aggregations=[MetricAggregationType.COUNT] - ) + aggregations=[MetricAggregationType.COUNT], + ) assert response assert response.metrics def test_metrics_granularity(self, recorded_test, monitor_info): client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient)) response = client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], metric_names=[METRIC_NAME], timespan=timedelta(days=1), granularity=timedelta(minutes=5), - aggregations=[MetricAggregationType.COUNT] - ) + aggregations=[MetricAggregationType.COUNT], + ) assert response assert response.granularity == timedelta(minutes=5) metric = response.metrics[METRIC_NAME] @@ -47,13 +47,13 @@ def test_metrics_granularity(self, recorded_test, monitor_info): def test_metrics_filter(self, recorded_test, monitor_info): client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient)) response = client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], metric_names=[METRIC_NAME], timespan=timedelta(days=1), granularity=timedelta(minutes=5), filter="request/success eq '0'", - aggregations=[MetricAggregationType.COUNT] - ) + aggregations=[MetricAggregationType.COUNT], + ) assert response metric = response.metrics[METRIC_NAME] for t in metric.timeseries: @@ -62,12 +62,12 @@ def test_metrics_filter(self, recorded_test, monitor_info): def test_metrics_list(self, recorded_test, monitor_info): client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient)) response = client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], metric_names=[METRIC_NAME], timespan=timedelta(days=1), granularity=timedelta(minutes=5), - aggregations=[MetricAggregationType.COUNT] - ) + aggregations=[MetricAggregationType.COUNT], + ) assert response metrics = response.metrics assert len(metrics) == 1 @@ -86,17 +86,16 @@ def test_metrics_list_with_commas(self): metric_names=["metric1,metric2", "foo,test,test"], timespan=timedelta(days=1), granularity=timedelta(minutes=5), - aggregations=[MetricAggregationType.COUNT] + aggregations=[MetricAggregationType.COUNT], ) assert "metricnames" in mock_list.call_args[1] - assert mock_list.call_args[1]['metricnames'] == "metric1%2metric2,foo%2test%2test" - + assert mock_list.call_args[1]["metricnames"] == "metric1%2metric2,foo%2test%2test" def test_metrics_namespaces(self, recorded_test, monitor_info): client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient)) - response = client.list_metric_namespaces(monitor_info['metrics_resource_id']) + response = client.list_metric_namespaces(monitor_info["metrics_resource_id"]) assert response is not None for item in response: @@ -105,7 +104,8 @@ def test_metrics_namespaces(self, recorded_test, monitor_info): def test_metrics_definitions(self, recorded_test, monitor_info): client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient)) response = client.list_metric_definitions( - monitor_info['metrics_resource_id'], namespace=METRIC_RESOURCE_PROVIDER) + monitor_info["metrics_resource_id"], namespace=METRIC_RESOURCE_PROVIDER + ) assert response is not None for item in response: diff --git a/sdk/monitor/azure-monitor-query/tests/test_metrics_query_client_async.py b/sdk/monitor/azure-monitor-query/tests/test_metrics_query_client_async.py index 6ddab41e322c..a4272a6e6043 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_metrics_query_client_async.py +++ b/sdk/monitor/azure-monitor-query/tests/test_metrics_query_client_async.py @@ -23,30 +23,28 @@ class TestMetricsQueryClientAsync(MetricsQueryClientTestCase): @pytest.mark.asyncio async def test_metrics_auth(self, recorded_test, monitor_info): - client = self.get_client( - MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) + client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) async with client: response = await client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], metric_names=[METRIC_NAME], timespan=timedelta(days=1), - aggregations=[MetricAggregationType.COUNT] - ) + aggregations=[MetricAggregationType.COUNT], + ) assert response assert response.metrics @pytest.mark.asyncio async def test_metrics_granularity(self, recorded_test, monitor_info): - client = self.get_client( - MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) + client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) async with client: response = await client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], metric_names=[METRIC_NAME], timespan=timedelta(days=1), granularity=timedelta(minutes=5), - aggregations=[MetricAggregationType.COUNT] - ) + aggregations=[MetricAggregationType.COUNT], + ) assert response assert response.granularity == timedelta(minutes=5) metric = response.metrics[METRIC_NAME] @@ -56,17 +54,16 @@ async def test_metrics_granularity(self, recorded_test, monitor_info): @pytest.mark.asyncio async def test_metrics_filter(self, recorded_test, monitor_info): - client = self.get_client( - MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) + client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) async with client: response = await client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], metric_names=[METRIC_NAME], timespan=timedelta(days=1), granularity=timedelta(minutes=5), filter="request/success eq '0'", - aggregations=[MetricAggregationType.COUNT] - ) + aggregations=[MetricAggregationType.COUNT], + ) assert response metric = response.metrics[METRIC_NAME] for t in metric.timeseries: @@ -74,16 +71,15 @@ async def test_metrics_filter(self, recorded_test, monitor_info): @pytest.mark.asyncio async def test_metrics_list(self, recorded_test, monitor_info): - client = self.get_client( - MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) + client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) async with client: response = await client.query_resource( - monitor_info['metrics_resource_id'], + monitor_info["metrics_resource_id"], metric_names=[METRIC_NAME], timespan=timedelta(days=1), granularity=timedelta(minutes=5), - aggregations=[MetricAggregationType.COUNT] - ) + aggregations=[MetricAggregationType.COUNT], + ) assert response metrics = response.metrics assert len(metrics) == 1 @@ -98,27 +94,25 @@ async def test_metrics_list_with_commas(self): with mock.patch("azure.monitor.query._generated.metrics.aio.operations.MetricsOperations.list") as mock_list: mock_list.return_value = {"foo": "bar"} - client = self.get_client( - MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) + client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) async with client: await client.query_resource( "resource", metric_names=["metric1,metric2", "foo,test,test"], timespan=timedelta(days=1), granularity=timedelta(minutes=5), - aggregations=[MetricAggregationType.COUNT] + aggregations=[MetricAggregationType.COUNT], ) assert "metricnames" in mock_list.call_args[1] - assert mock_list.call_args[1]['metricnames'] == "metric1%2metric2,foo%2test%2test" + assert mock_list.call_args[1]["metricnames"] == "metric1%2metric2,foo%2test%2test" @pytest.mark.asyncio async def test_metrics_namespaces(self, recorded_test, monitor_info): - client = self.get_client( - MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) + client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) async with client: - response = client.list_metric_namespaces(monitor_info['metrics_resource_id']) + response = client.list_metric_namespaces(monitor_info["metrics_resource_id"]) assert response is not None async for item in response: @@ -126,12 +120,12 @@ async def test_metrics_namespaces(self, recorded_test, monitor_info): @pytest.mark.asyncio async def test_metrics_definitions(self, recorded_test, monitor_info): - client = self.get_client( - MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) + client = self.get_client(MetricsQueryClient, self.get_credential(MetricsQueryClient, is_async=True)) async with client: response = client.list_metric_definitions( - monitor_info['metrics_resource_id'], namespace=METRIC_RESOURCE_PROVIDER) + monitor_info["metrics_resource_id"], namespace=METRIC_RESOURCE_PROVIDER + ) assert response is not None async for item in response: From dfac1320d24954f086ad8d094afbff8682d7d151 Mon Sep 17 00:00:00 2001 From: Paul Van Eck Date: Tue, 29 Oct 2024 19:06:34 +0000 Subject: [PATCH 2/2] Fix spelling Signed-off-by: Paul Van Eck --- sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py | 3 ++- sdk/monitor/azure-monitor-query/tests/test_logs_response.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py b/sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py index 778f5dabde72..89e0d3cd9313 100644 --- a/sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py +++ b/sdk/monitor/azure-monitor-ingestion/tests/test_helpers.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See LICENSE.txt in the project root for # license information. # ------------------------------------------------------------------------- +# cspell:ignore wbits import json import random import string @@ -20,7 +21,7 @@ ALPHANUMERIC_CHARACTERS = string.ascii_letters + string.digits -random.seed(42) # For repeatibility +random.seed(42) # For repeatability def _get_random_string(length: int): diff --git a/sdk/monitor/azure-monitor-query/tests/test_logs_response.py b/sdk/monitor/azure-monitor-query/tests/test_logs_response.py index 544d432ef8cd..7090bdcbe89c 100644 --- a/sdk/monitor/azure-monitor-query/tests/test_logs_response.py +++ b/sdk/monitor/azure-monitor-query/tests/test_logs_response.py @@ -3,6 +3,7 @@ # Licensed under the MIT License. See LICENSE.txt in the project root for # license information. # ------------------------------------------------------------------------- +# cspell:ignore toint from datetime import datetime, timezone from azure.monitor.query import LogsQueryClient