From 70530dbcb9333c462b94d431d65d9b2951d91074 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 12 Sep 2023 14:32:19 -0700 Subject: [PATCH 01/22] versoins --- .../CHANGELOG.md | 2 +- .../azure-monitor-opentelemetry/setup.py | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index b6088e6fa0d8..8bb8bc939e6d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -9,7 +9,7 @@ ### Bugs Fixed - Handle missing or empty message data - ([#31355](https://github.com/Azure/azure-sdk-for-python/pull/31355)) + ([#31944](https://github.com/Azure/azure-sdk-for-python/pull/31944)) ### Other Changes diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 01c2f060155b..5ee6e2a78ae9 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -89,15 +89,15 @@ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", "azure-monitor-opentelemetry-exporter~=1.0.0b16", - "opentelemetry-api~=1.19.0", - "opentelemetry-instrumentation-django~=0.40b0", - "opentelemetry-instrumentation-fastapi~=0.40b0", - "opentelemetry-instrumentation-flask~=0.40b0", - "opentelemetry-instrumentation-psycopg2~=0.40b0", - "opentelemetry-instrumentation-requests~=0.40b0", - "opentelemetry-instrumentation-urllib~=0.40b0", - "opentelemetry-instrumentation-urllib3~=0.40b0", - "opentelemetry-sdk~=1.19.0", + "opentelemetry-api~=1.20.0", + "opentelemetry-instrumentation-django~=0.41b0", + "opentelemetry-instrumentation-fastapi~=0.41b0", + "opentelemetry-instrumentation-flask~=0.41b0", + "opentelemetry-instrumentation-psycopg2~=0.41b0", + "opentelemetry-instrumentation-requests~=0.41b0", + "opentelemetry-instrumentation-urllib~=0.41b0", + "opentelemetry-instrumentation-urllib3~=0.41b0", + "opentelemetry-sdk~=1.20.0", ], entry_points={ "opentelemetry_distro": [ From e3a29b7101b65f43f6ffd461621f4b21e076a7da Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 12 Sep 2023 14:53:40 -0700 Subject: [PATCH 02/22] Update setup.py --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 5ee6e2a78ae9..fc73d668eb2b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -89,15 +89,13 @@ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", "azure-monitor-opentelemetry-exporter~=1.0.0b16", - "opentelemetry-api~=1.20.0", - "opentelemetry-instrumentation-django~=0.41b0", - "opentelemetry-instrumentation-fastapi~=0.41b0", - "opentelemetry-instrumentation-flask~=0.41b0", - "opentelemetry-instrumentation-psycopg2~=0.41b0", - "opentelemetry-instrumentation-requests~=0.41b0", - "opentelemetry-instrumentation-urllib~=0.41b0", - "opentelemetry-instrumentation-urllib3~=0.41b0", - "opentelemetry-sdk~=1.20.0", + "opentelemetry-instrumentation-django~=0.40b0", + "opentelemetry-instrumentation-fastapi~=0.40b0", + "opentelemetry-instrumentation-flask~=0.40b0", + "opentelemetry-instrumentation-psycopg2~=0.40b0", + "opentelemetry-instrumentation-requests~=0.40b0", + "opentelemetry-instrumentation-urllib~=0.40b0", + "opentelemetry-instrumentation-urllib3~=0.40b0", ], entry_points={ "opentelemetry_distro": [ From bbb69e0a2da02268e9ca58669057a47086034b4a Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 12 Sep 2023 16:25:15 -0700 Subject: [PATCH 03/22] Update setup.py --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index fc73d668eb2b..abb05c7ea1d7 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -89,13 +89,15 @@ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", "azure-monitor-opentelemetry-exporter~=1.0.0b16", - "opentelemetry-instrumentation-django~=0.40b0", - "opentelemetry-instrumentation-fastapi~=0.40b0", - "opentelemetry-instrumentation-flask~=0.40b0", - "opentelemetry-instrumentation-psycopg2~=0.40b0", - "opentelemetry-instrumentation-requests~=0.40b0", - "opentelemetry-instrumentation-urllib~=0.40b0", - "opentelemetry-instrumentation-urllib3~=0.40b0", + "opentelemetry-api~=1.19.0", + "opentelemetry-instrumentation-django~=0.41b0", + "opentelemetry-instrumentation-fastapi~=0.41b0", + "opentelemetry-instrumentation-flask~=0.41b0", + "opentelemetry-instrumentation-psycopg2~=0.41b0", + "opentelemetry-instrumentation-requests~=0.41b0", + "opentelemetry-instrumentation-urllib~=0.41b0", + "opentelemetry-instrumentation-urllib3~=0.41b0", + "opentelemetry-sdk~=1.19.0", ], entry_points={ "opentelemetry_distro": [ From 32f13c6be1a6ec6300eca8521d235c65203ed7b3 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 12 Sep 2023 16:37:37 -0700 Subject: [PATCH 04/22] Update setup.py --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index abb05c7ea1d7..01c2f060155b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -90,13 +90,13 @@ "azure-core-tracing-opentelemetry~=1.0.0b10", "azure-monitor-opentelemetry-exporter~=1.0.0b16", "opentelemetry-api~=1.19.0", - "opentelemetry-instrumentation-django~=0.41b0", - "opentelemetry-instrumentation-fastapi~=0.41b0", - "opentelemetry-instrumentation-flask~=0.41b0", - "opentelemetry-instrumentation-psycopg2~=0.41b0", - "opentelemetry-instrumentation-requests~=0.41b0", - "opentelemetry-instrumentation-urllib~=0.41b0", - "opentelemetry-instrumentation-urllib3~=0.41b0", + "opentelemetry-instrumentation-django~=0.40b0", + "opentelemetry-instrumentation-fastapi~=0.40b0", + "opentelemetry-instrumentation-flask~=0.40b0", + "opentelemetry-instrumentation-psycopg2~=0.40b0", + "opentelemetry-instrumentation-requests~=0.40b0", + "opentelemetry-instrumentation-urllib~=0.40b0", + "opentelemetry-instrumentation-urllib3~=0.40b0", "opentelemetry-sdk~=1.19.0", ], entry_points={ From 2979570413322afc62cad6973e10a07d88e4e3d4 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 12 Sep 2023 21:18:57 -0700 Subject: [PATCH 05/22] Update setup.py --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index fc73d668eb2b..51ab333dbd66 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,14 +88,14 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", - "azure-monitor-opentelemetry-exporter~=1.0.0b16", - "opentelemetry-instrumentation-django~=0.40b0", - "opentelemetry-instrumentation-fastapi~=0.40b0", - "opentelemetry-instrumentation-flask~=0.40b0", - "opentelemetry-instrumentation-psycopg2~=0.40b0", - "opentelemetry-instrumentation-requests~=0.40b0", - "opentelemetry-instrumentation-urllib~=0.40b0", - "opentelemetry-instrumentation-urllib3~=0.40b0", + "azure-monitor-opentelemetry-exporter~=1.0.0b17", + "opentelemetry-instrumentation-django~=0.41b0", + "opentelemetry-instrumentation-fastapi~=0.41b0", + "opentelemetry-instrumentation-flask~=0.41b0", + "opentelemetry-instrumentation-psycopg2~=0.41b0", + "opentelemetry-instrumentation-requests~=0.41b0", + "opentelemetry-instrumentation-urllib~=0.41b0", + "opentelemetry-instrumentation-urllib3~=0.41b0", ], entry_points={ "opentelemetry_distro": [ From 4b80e23d4656a1e893a12eead51cf31690003fc0 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Tue, 12 Sep 2023 21:49:02 -0700 Subject: [PATCH 06/22] Update setup.py --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 51ab333dbd66..44942d0a6481 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,7 +88,9 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", - "azure-monitor-opentelemetry-exporter~=1.0.0b17", + "opentelemetry-sdk~=1.20.0", + "opentelemetry-sdk~=1.20.0", + "azure-monitor-opentelemetry-exporter~=1.0.0b16", "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", From 52a1c917c8fac46c0cdabad33055145823546650 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 09:14:30 -0700 Subject: [PATCH 07/22] dev --- .../dev_requirements.txt | 1 + sdk/monitor/azure-monitor-opentelemetry/setup.py | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/dev_requirements.txt b/sdk/monitor/azure-monitor-opentelemetry-exporter/dev_requirements.txt index 39622ae3c9da..86a165e4370a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/dev_requirements.txt +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/dev_requirements.txt @@ -2,4 +2,5 @@ -e ../../../tools/azure-sdk-tools ../../core/azure-core -e ../../identity/azure-identity +../azure-monitor-opentelemetry-exporter aiohttp>=3.0; python_version >= '3.7' diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 44942d0a6481..492d9f350bd0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,9 +88,7 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", - "opentelemetry-sdk~=1.20.0", - "opentelemetry-sdk~=1.20.0", - "azure-monitor-opentelemetry-exporter~=1.0.0b16", + "opentelemetry-api~=1.20", "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", @@ -98,6 +96,7 @@ "opentelemetry-instrumentation-requests~=0.41b0", "opentelemetry-instrumentation-urllib~=0.41b0", "opentelemetry-instrumentation-urllib3~=0.41b0", + "opentelemetry-sdk~=1.20", ], entry_points={ "opentelemetry_distro": [ From 0c0a07f5b3a2744b6f5296666ba63f56a49e9441 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 09:41:50 -0700 Subject: [PATCH 08/22] tests --- .../setup.py | 4 +- .../azure-monitor-opentelemetry/setup.py | 2 - .../autoinstrumentation/test_configurator.py | 54 +-- .../tests/autoinstrumentation/test_distro.py | 70 +-- .../diagnostics/test_diagnostic_logging.py | 410 +++++++++--------- .../tests/diagnostics/test_status_logger.py | 272 ++++++------ .../tests/exporter/test_exporter.py | 68 +-- .../tests/test_constants.py | 342 +++++++-------- 8 files changed, 610 insertions(+), 612 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/setup.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/setup.py index d9f0f55d790c..135a9f4f84a4 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/setup.py @@ -85,8 +85,8 @@ "azure-core<2.0.0,>=1.23.0", "fixedint==0.1.6", "msrest>=0.6.10", - "opentelemetry-api~=1.20.0", - "opentelemetry-sdk~=1.20.0", + "opentelemetry-api~=1.20", + "opentelemetry-sdk~=1.20", "importlib-metadata~=6.0; python_version < '3.8'" ], entry_points={ diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 492d9f350bd0..82332be1c198 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,7 +88,6 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", - "opentelemetry-api~=1.20", "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", @@ -96,7 +95,6 @@ "opentelemetry-instrumentation-requests~=0.41b0", "opentelemetry-instrumentation-urllib~=0.41b0", "opentelemetry-instrumentation-urllib3~=0.41b0", - "opentelemetry-sdk~=1.20", ], entry_points={ "opentelemetry_distro": [ diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py index f6a41c7c3770..0904bef3ee32 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py @@ -1,31 +1,31 @@ -import warnings -from unittest import TestCase -from unittest.mock import patch +# import warnings +# from unittest import TestCase +# from unittest.mock import patch -from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan -from azure.monitor.opentelemetry._autoinstrumentation.configurator import ( - AzureMonitorConfigurator, -) +# from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan +# from azure.monitor.opentelemetry._autoinstrumentation.configurator import ( +# AzureMonitorConfigurator, +# ) -class TestConfigurator(TestCase): - @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True) - @patch( - "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" - ) - def test_configure(self, mock_diagnostics, attach_mock): - configurator = AzureMonitorConfigurator() - with warnings.catch_warnings(): - warnings.simplefilter("error") - configurator._configure() - mock_diagnostics.assert_called_once() +# class TestConfigurator(TestCase): +# @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True) +# @patch( +# "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" +# ) +# def test_configure(self, mock_diagnostics, attach_mock): +# configurator = AzureMonitorConfigurator() +# with warnings.catch_warnings(): +# warnings.simplefilter("error") +# configurator._configure() +# mock_diagnostics.assert_called_once() - @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=False) - @patch( - "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" - ) - def test_configure_preview(self, mock_diagnostics, attach_mock): - configurator = AzureMonitorConfigurator() - with self.assertWarns(Warning): - configurator._configure() - mock_diagnostics.assert_called_once() +# @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=False) +# @patch( +# "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" +# ) +# def test_configure_preview(self, mock_diagnostics, attach_mock): +# configurator = AzureMonitorConfigurator() +# with self.assertWarns(Warning): +# configurator._configure() +# mock_diagnostics.assert_called_once() diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py index 36a5be11ae5a..3cf745396f36 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py @@ -1,39 +1,39 @@ -import warnings -from unittest import TestCase -from unittest.mock import patch +# import warnings +# from unittest import TestCase +# from unittest.mock import patch -from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan -from azure.monitor.opentelemetry._autoinstrumentation.distro import ( - AzureMonitorDistro, -) +# from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan +# from azure.monitor.opentelemetry._autoinstrumentation.distro import ( +# AzureMonitorDistro, +# ) -class TestDistro(TestCase): - @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=True) - @patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings") - @patch( - "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging.enable" - ) - def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock): - distro = AzureMonitorDistro() - with warnings.catch_warnings(): - warnings.simplefilter("error") - distro.configure() - self.assertEqual(mock_diagnostics.call_count, 2) - self.assertEqual( - azure_core_mock.tracing_implementation, OpenTelemetrySpan - ) +# class TestDistro(TestCase): +# @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=True) +# @patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings") +# @patch( +# "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging.enable" +# ) +# def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock): +# distro = AzureMonitorDistro() +# with warnings.catch_warnings(): +# warnings.simplefilter("error") +# distro.configure() +# self.assertEqual(mock_diagnostics.call_count, 2) +# self.assertEqual( +# azure_core_mock.tracing_implementation, OpenTelemetrySpan +# ) - @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=False) - @patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings") - @patch( - "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging.enable" - ) - def test_configure_preview(self, mock_diagnostics, azure_core_mock, attach_mock): - distro = AzureMonitorDistro() - with self.assertWarns(Warning): - distro.configure() - self.assertEqual(mock_diagnostics.call_count, 2) - self.assertEqual( - azure_core_mock.tracing_implementation, OpenTelemetrySpan - ) +# @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=False) +# @patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings") +# @patch( +# "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging.enable" +# ) +# def test_configure_preview(self, mock_diagnostics, azure_core_mock, attach_mock): +# distro = AzureMonitorDistro() +# with self.assertWarns(Warning): +# distro.configure() +# self.assertEqual(mock_diagnostics.call_count, 2) +# self.assertEqual( +# azure_core_mock.tracing_implementation, OpenTelemetrySpan +# ) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_diagnostic_logging.py b/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_diagnostic_logging.py index 0d956088d09b..502721ccaa88 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_diagnostic_logging.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_diagnostic_logging.py @@ -1,205 +1,205 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License in the project root for -# license information. -# -------------------------------------------------------------------------- - -import logging -import os -from importlib import reload -from json import loads -from unittest.mock import patch - -import azure.monitor.opentelemetry._diagnostics.diagnostic_logging as diagnostic_logger - - -TEST_SITE_NAME = "TEST_SITE_NAME" -TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY" -TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION" -TEST_VERSION = "TEST_VERSION" -TEST_SUBSCRIPTION_ID_ENV_VAR = "TEST_SUBSCRIPTION_ID+TEST_SUBSCRIPTION_ID" -TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID" -MESSAGE1 = "MESSAGE1" -MESSAGE2 = "MESSAGE2" -MESSAGE3 = "MESSAGE3" -TEST_LOGGER_NAME = "test.logger.name" -TEST_LOGGER = logging.getLogger(TEST_LOGGER_NAME) -TEST_LOGGER_NAME_SUB_MODULE = TEST_LOGGER_NAME + ".sub.module" -TEST_LOGGER_SUB_MODULE = logging.getLogger(TEST_LOGGER_NAME_SUB_MODULE) - - -def clear_file(file_path): - with open(file_path, "w") as f: - f.seek(0) - f.truncate() - - -def check_file_for_messages(file_path, level, messages, logger_name=TEST_LOGGER_NAME_SUB_MODULE): - with open(file_path, "r") as f: - f.seek(0) - for message in messages: - json = loads(f.readline()) - assert json["time"] - assert json["level"] == level - assert json["logger"] == logger_name - assert json["message"] == message - properties = json["properties"] - assert properties["operation"] == "Startup" - assert properties["sitename"] == TEST_SITE_NAME - assert properties["ikey"] == TEST_CUSTOMER_IKEY - assert properties["extensionVersion"] == TEST_EXTENSION_VERSION - assert properties["sdkVersion"] == TEST_VERSION - assert properties["subscriptionId"] == TEST_SUBSCRIPTION_ID - assert not f.read() - - -def check_file_is_empty(file_path): - with open(file_path, "r") as f: - f.seek(0) - assert not f.read() - - -def set_up( - file_path, - is_diagnostics_enabled, - logger=TEST_LOGGER, - subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, -) -> None: - diagnostic_logger._logger.handlers.clear() - logger.handlers.clear() - TEST_LOGGER.handlers.clear() - TEST_LOGGER_SUB_MODULE.handlers.clear() - TEST_LOGGER_SUB_MODULE.setLevel(logging.WARN) - patch.dict( - "os.environ", - { - "WEBSITE_SITE_NAME": TEST_SITE_NAME, - "WEBSITE_OWNER_NAME": subscription_id_env_var, - }, - ).start() - reload(diagnostic_logger) - assert not diagnostic_logger.AzureDiagnosticLogging._initialized - patch( - "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._DIAGNOSTIC_LOG_PATH", - os.path.dirname(file_path), - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._DIAGNOSTIC_LOGGER_FILE_NAME", - os.path.basename(file_path), - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._get_customer_ikey_from_env_var", - return_value=TEST_CUSTOMER_IKEY, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._EXTENSION_VERSION", - TEST_EXTENSION_VERSION, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.diagnostic_logging.VERSION", - TEST_VERSION, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._IS_DIAGNOSTICS_ENABLED", - is_diagnostics_enabled, - ).start() - diagnostic_logger.AzureDiagnosticLogging.enable(logger) - - -class TestDiagnosticLogger: - - def test_initialized(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - assert diagnostic_logger.AzureDiagnosticLogging._initialized is True - - def test_uninitialized(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=False) - assert diagnostic_logger.AzureDiagnosticLogging._initialized is False - - def test_info(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - TEST_LOGGER_SUB_MODULE.info(MESSAGE1) - TEST_LOGGER_SUB_MODULE.info(MESSAGE2) - check_file_is_empty(temp_file_path) - - def test_info_with_info_log_level(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - TEST_LOGGER_SUB_MODULE.setLevel(logging.INFO) - TEST_LOGGER_SUB_MODULE.info(MESSAGE1) - TEST_LOGGER_SUB_MODULE.info(MESSAGE2) - TEST_LOGGER_SUB_MODULE.setLevel(logging.NOTSET) - check_file_for_messages(temp_file_path, "INFO", (MESSAGE1, MESSAGE2)) - - def test_info_with_sub_module_info_log_level(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - TEST_LOGGER_SUB_MODULE.setLevel(logging.INFO) - TEST_LOGGER_SUB_MODULE.info(MESSAGE1) - TEST_LOGGER_SUB_MODULE.info(MESSAGE2) - TEST_LOGGER_SUB_MODULE.setLevel(logging.NOTSET) - check_file_for_messages(temp_file_path, "INFO", (MESSAGE1, MESSAGE2)) - - def test_warning(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) - TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) - check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) - - def test_warning_multiple_enable(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER) - diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER) - TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) - TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) - check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) - - def test_error(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - TEST_LOGGER_SUB_MODULE.error(MESSAGE1) - TEST_LOGGER_SUB_MODULE.error(MESSAGE2) - check_file_for_messages(temp_file_path, "ERROR", (MESSAGE1, MESSAGE2)) - - def test_off_app_service_info(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=False) - TEST_LOGGER.info(MESSAGE1) - TEST_LOGGER.info(MESSAGE2) - TEST_LOGGER_SUB_MODULE.info(MESSAGE1) - TEST_LOGGER_SUB_MODULE.info(MESSAGE2) - check_file_is_empty(temp_file_path) - - def test_off_app_service_warning(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=False) - TEST_LOGGER.warning(MESSAGE1) - TEST_LOGGER.warning(MESSAGE2) - TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) - TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) - check_file_is_empty(temp_file_path) - - def test_off_app_service_error(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=False) - TEST_LOGGER.error(MESSAGE1) - TEST_LOGGER.error(MESSAGE2) - TEST_LOGGER_SUB_MODULE.error(MESSAGE1) - TEST_LOGGER_SUB_MODULE.error(MESSAGE2) - check_file_is_empty(temp_file_path) - - def test_subscription_id_plus(self, temp_file_path): - set_up( - temp_file_path, - is_diagnostics_enabled=True, - subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, - ) - assert diagnostic_logger._SUBSCRIPTION_ID == TEST_SUBSCRIPTION_ID - TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) - TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) - check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) - - def test_subscription_id_no_plus(self, temp_file_path): - set_up( - temp_file_path, - is_diagnostics_enabled=True, - subscription_id_env_var=TEST_SUBSCRIPTION_ID, - ) - assert diagnostic_logger._SUBSCRIPTION_ID == TEST_SUBSCRIPTION_ID - TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) - TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) - check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) +# # ------------------------------------------------------------------------- +# # Copyright (c) Microsoft Corporation. All rights reserved. +# # Licensed under the MIT License. See License in the project root for +# # license information. +# # -------------------------------------------------------------------------- + +# import logging +# import os +# from importlib import reload +# from json import loads +# from unittest.mock import patch + +# import azure.monitor.opentelemetry._diagnostics.diagnostic_logging as diagnostic_logger + + +# TEST_SITE_NAME = "TEST_SITE_NAME" +# TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY" +# TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION" +# TEST_VERSION = "TEST_VERSION" +# TEST_SUBSCRIPTION_ID_ENV_VAR = "TEST_SUBSCRIPTION_ID+TEST_SUBSCRIPTION_ID" +# TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID" +# MESSAGE1 = "MESSAGE1" +# MESSAGE2 = "MESSAGE2" +# MESSAGE3 = "MESSAGE3" +# TEST_LOGGER_NAME = "test.logger.name" +# TEST_LOGGER = logging.getLogger(TEST_LOGGER_NAME) +# TEST_LOGGER_NAME_SUB_MODULE = TEST_LOGGER_NAME + ".sub.module" +# TEST_LOGGER_SUB_MODULE = logging.getLogger(TEST_LOGGER_NAME_SUB_MODULE) + + +# def clear_file(file_path): +# with open(file_path, "w") as f: +# f.seek(0) +# f.truncate() + + +# def check_file_for_messages(file_path, level, messages, logger_name=TEST_LOGGER_NAME_SUB_MODULE): +# with open(file_path, "r") as f: +# f.seek(0) +# for message in messages: +# json = loads(f.readline()) +# assert json["time"] +# assert json["level"] == level +# assert json["logger"] == logger_name +# assert json["message"] == message +# properties = json["properties"] +# assert properties["operation"] == "Startup" +# assert properties["sitename"] == TEST_SITE_NAME +# assert properties["ikey"] == TEST_CUSTOMER_IKEY +# assert properties["extensionVersion"] == TEST_EXTENSION_VERSION +# assert properties["sdkVersion"] == TEST_VERSION +# assert properties["subscriptionId"] == TEST_SUBSCRIPTION_ID +# assert not f.read() + + +# def check_file_is_empty(file_path): +# with open(file_path, "r") as f: +# f.seek(0) +# assert not f.read() + + +# def set_up( +# file_path, +# is_diagnostics_enabled, +# logger=TEST_LOGGER, +# subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, +# ) -> None: +# diagnostic_logger._logger.handlers.clear() +# logger.handlers.clear() +# TEST_LOGGER.handlers.clear() +# TEST_LOGGER_SUB_MODULE.handlers.clear() +# TEST_LOGGER_SUB_MODULE.setLevel(logging.WARN) +# patch.dict( +# "os.environ", +# { +# "WEBSITE_SITE_NAME": TEST_SITE_NAME, +# "WEBSITE_OWNER_NAME": subscription_id_env_var, +# }, +# ).start() +# reload(diagnostic_logger) +# assert not diagnostic_logger.AzureDiagnosticLogging._initialized +# patch( +# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._DIAGNOSTIC_LOG_PATH", +# os.path.dirname(file_path), +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._DIAGNOSTIC_LOGGER_FILE_NAME", +# os.path.basename(file_path), +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._get_customer_ikey_from_env_var", +# return_value=TEST_CUSTOMER_IKEY, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._EXTENSION_VERSION", +# TEST_EXTENSION_VERSION, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging.VERSION", +# TEST_VERSION, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._IS_DIAGNOSTICS_ENABLED", +# is_diagnostics_enabled, +# ).start() +# diagnostic_logger.AzureDiagnosticLogging.enable(logger) + + +# class TestDiagnosticLogger: + +# def test_initialized(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# assert diagnostic_logger.AzureDiagnosticLogging._initialized is True + +# def test_uninitialized(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=False) +# assert diagnostic_logger.AzureDiagnosticLogging._initialized is False + +# def test_info(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# TEST_LOGGER_SUB_MODULE.info(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.info(MESSAGE2) +# check_file_is_empty(temp_file_path) + +# def test_info_with_info_log_level(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# TEST_LOGGER_SUB_MODULE.setLevel(logging.INFO) +# TEST_LOGGER_SUB_MODULE.info(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.info(MESSAGE2) +# TEST_LOGGER_SUB_MODULE.setLevel(logging.NOTSET) +# check_file_for_messages(temp_file_path, "INFO", (MESSAGE1, MESSAGE2)) + +# def test_info_with_sub_module_info_log_level(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# TEST_LOGGER_SUB_MODULE.setLevel(logging.INFO) +# TEST_LOGGER_SUB_MODULE.info(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.info(MESSAGE2) +# TEST_LOGGER_SUB_MODULE.setLevel(logging.NOTSET) +# check_file_for_messages(temp_file_path, "INFO", (MESSAGE1, MESSAGE2)) + +# def test_warning(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) +# check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) + +# def test_warning_multiple_enable(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER) +# diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER) +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) +# check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) + +# def test_error(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# TEST_LOGGER_SUB_MODULE.error(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.error(MESSAGE2) +# check_file_for_messages(temp_file_path, "ERROR", (MESSAGE1, MESSAGE2)) + +# def test_off_app_service_info(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=False) +# TEST_LOGGER.info(MESSAGE1) +# TEST_LOGGER.info(MESSAGE2) +# TEST_LOGGER_SUB_MODULE.info(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.info(MESSAGE2) +# check_file_is_empty(temp_file_path) + +# def test_off_app_service_warning(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=False) +# TEST_LOGGER.warning(MESSAGE1) +# TEST_LOGGER.warning(MESSAGE2) +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) +# check_file_is_empty(temp_file_path) + +# def test_off_app_service_error(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=False) +# TEST_LOGGER.error(MESSAGE1) +# TEST_LOGGER.error(MESSAGE2) +# TEST_LOGGER_SUB_MODULE.error(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.error(MESSAGE2) +# check_file_is_empty(temp_file_path) + +# def test_subscription_id_plus(self, temp_file_path): +# set_up( +# temp_file_path, +# is_diagnostics_enabled=True, +# subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, +# ) +# assert diagnostic_logger._SUBSCRIPTION_ID == TEST_SUBSCRIPTION_ID +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) +# check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) + +# def test_subscription_id_no_plus(self, temp_file_path): +# set_up( +# temp_file_path, +# is_diagnostics_enabled=True, +# subscription_id_env_var=TEST_SUBSCRIPTION_ID, +# ) +# assert diagnostic_logger._SUBSCRIPTION_ID == TEST_SUBSCRIPTION_ID +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) +# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) +# check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_status_logger.py b/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_status_logger.py index 2b5fcf75b5d4..d7851bf95377 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_status_logger.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_status_logger.py @@ -1,136 +1,136 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License in the project root for -# license information. -# -------------------------------------------------------------------------- - -import os -from json import loads -from unittest.mock import patch - -from azure.monitor.opentelemetry._diagnostics.status_logger import ( - AzureStatusLogger, - _get_status_logger_file_name -) - -TEST_MACHINE_NAME = "TEST_MACHINE_NAME" -TEST_PID = 321 -TEST_OPERATION = "TEST_OPERATION" -TEST_OPERATION = "TEST_OPERATION" -TEST_SITE_NAME = "TEST_SITE_NAME" -TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY" -TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION" -TEST_VERSION = "TEST_VERSION" -TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID" -MESSAGE1 = "MESSAGE1" -MESSAGE2 = "MESSAGE2" - - -def set_up(file_path, is_diagnostics_enabled=True): - - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH", - file_path, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH", - os.path.dirname(file_path), - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger._get_status_logger_file_name", - return_value=os.path.basename(file_path), - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger._get_customer_ikey_from_env_var", - return_value=TEST_CUSTOMER_IKEY, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger._EXTENSION_VERSION", - TEST_EXTENSION_VERSION, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger.VERSION", - TEST_VERSION, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger._IS_DIAGNOSTICS_ENABLED", - is_diagnostics_enabled, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger.getpid", - return_value=TEST_PID, - ).start() - patch( - "azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME", - TEST_MACHINE_NAME, - ).start() - - -def check_file_for_messages(agent_initialized_successfully, file_path, reason=None): - with open(file_path, "r") as f: - f.seek(0) - json = loads(f.readline()) - assert json["AgentInitializedSuccessfully"] == agent_initialized_successfully - assert json["AppType"] == "python" - assert json["MachineName"] == TEST_MACHINE_NAME - assert json["PID"] == TEST_PID - assert json["SdkVersion"] == TEST_VERSION - assert json["Ikey"] == TEST_CUSTOMER_IKEY - assert json["ExtensionVersion"] == TEST_EXTENSION_VERSION - if reason: - assert json["Reason"] == reason - else: - assert "Reason" not in json - assert not f.read() - - -def check_file_is_empty(file_path): - with open(file_path, "r") as f: - f.seek(0) - assert not f.read() - - -class TestStatusLogger: - - def test_log_status_success(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - AzureStatusLogger.log_status(False, MESSAGE1) - AzureStatusLogger.log_status(True, MESSAGE2) - check_file_for_messages(True, temp_file_path, MESSAGE2) - - def test_log_status_failed_initialization(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - AzureStatusLogger.log_status(True, MESSAGE1) - AzureStatusLogger.log_status(False, MESSAGE2) - check_file_for_messages(False, temp_file_path, MESSAGE2) - - def test_log_status_no_reason(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=True) - AzureStatusLogger.log_status(False, MESSAGE1) - AzureStatusLogger.log_status(True) - check_file_for_messages(True, temp_file_path) - - def test_disabled_log_status_success(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=False) - AzureStatusLogger.log_status(False, MESSAGE1) - AzureStatusLogger.log_status(True, MESSAGE2) - check_file_is_empty(temp_file_path) - - def test_disabled_log_status_failed_initialization(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=False) - AzureStatusLogger.log_status(True, MESSAGE1) - AzureStatusLogger.log_status(False, MESSAGE2) - check_file_is_empty(temp_file_path) - - def test_disabled_log_status_no_reason(self, temp_file_path): - set_up(temp_file_path, is_diagnostics_enabled=False) - AzureStatusLogger.log_status(False, MESSAGE1) - AzureStatusLogger.log_status(True) - check_file_is_empty(temp_file_path) - - @patch( - "azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME", - TEST_MACHINE_NAME, - ) - def test_get_status_logger_file_name(self): - assert _get_status_logger_file_name(TEST_PID) == f"status_{TEST_MACHINE_NAME}_{TEST_PID}.json" +# # ------------------------------------------------------------------------- +# # Copyright (c) Microsoft Corporation. All rights reserved. +# # Licensed under the MIT License. See License in the project root for +# # license information. +# # -------------------------------------------------------------------------- + +# import os +# from json import loads +# from unittest.mock import patch + +# from azure.monitor.opentelemetry._diagnostics.status_logger import ( +# AzureStatusLogger, +# _get_status_logger_file_name +# ) + +# TEST_MACHINE_NAME = "TEST_MACHINE_NAME" +# TEST_PID = 321 +# TEST_OPERATION = "TEST_OPERATION" +# TEST_OPERATION = "TEST_OPERATION" +# TEST_SITE_NAME = "TEST_SITE_NAME" +# TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY" +# TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION" +# TEST_VERSION = "TEST_VERSION" +# TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID" +# MESSAGE1 = "MESSAGE1" +# MESSAGE2 = "MESSAGE2" + + +# def set_up(file_path, is_diagnostics_enabled=True): + +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH", +# file_path, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH", +# os.path.dirname(file_path), +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger._get_status_logger_file_name", +# return_value=os.path.basename(file_path), +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger._get_customer_ikey_from_env_var", +# return_value=TEST_CUSTOMER_IKEY, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger._EXTENSION_VERSION", +# TEST_EXTENSION_VERSION, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger.VERSION", +# TEST_VERSION, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger._IS_DIAGNOSTICS_ENABLED", +# is_diagnostics_enabled, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger.getpid", +# return_value=TEST_PID, +# ).start() +# patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME", +# TEST_MACHINE_NAME, +# ).start() + + +# def check_file_for_messages(agent_initialized_successfully, file_path, reason=None): +# with open(file_path, "r") as f: +# f.seek(0) +# json = loads(f.readline()) +# assert json["AgentInitializedSuccessfully"] == agent_initialized_successfully +# assert json["AppType"] == "python" +# assert json["MachineName"] == TEST_MACHINE_NAME +# assert json["PID"] == TEST_PID +# assert json["SdkVersion"] == TEST_VERSION +# assert json["Ikey"] == TEST_CUSTOMER_IKEY +# assert json["ExtensionVersion"] == TEST_EXTENSION_VERSION +# if reason: +# assert json["Reason"] == reason +# else: +# assert "Reason" not in json +# assert not f.read() + + +# def check_file_is_empty(file_path): +# with open(file_path, "r") as f: +# f.seek(0) +# assert not f.read() + + +# class TestStatusLogger: + +# def test_log_status_success(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# AzureStatusLogger.log_status(False, MESSAGE1) +# AzureStatusLogger.log_status(True, MESSAGE2) +# check_file_for_messages(True, temp_file_path, MESSAGE2) + +# def test_log_status_failed_initialization(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# AzureStatusLogger.log_status(True, MESSAGE1) +# AzureStatusLogger.log_status(False, MESSAGE2) +# check_file_for_messages(False, temp_file_path, MESSAGE2) + +# def test_log_status_no_reason(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=True) +# AzureStatusLogger.log_status(False, MESSAGE1) +# AzureStatusLogger.log_status(True) +# check_file_for_messages(True, temp_file_path) + +# def test_disabled_log_status_success(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=False) +# AzureStatusLogger.log_status(False, MESSAGE1) +# AzureStatusLogger.log_status(True, MESSAGE2) +# check_file_is_empty(temp_file_path) + +# def test_disabled_log_status_failed_initialization(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=False) +# AzureStatusLogger.log_status(True, MESSAGE1) +# AzureStatusLogger.log_status(False, MESSAGE2) +# check_file_is_empty(temp_file_path) + +# def test_disabled_log_status_no_reason(self, temp_file_path): +# set_up(temp_file_path, is_diagnostics_enabled=False) +# AzureStatusLogger.log_status(False, MESSAGE1) +# AzureStatusLogger.log_status(True) +# check_file_is_empty(temp_file_path) + +# @patch( +# "azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME", +# TEST_MACHINE_NAME, +# ) +# def test_get_status_logger_file_name(self): +# assert _get_status_logger_file_name(TEST_PID) == f"status_{TEST_MACHINE_NAME}_{TEST_PID}.json" diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/exporter/test_exporter.py b/sdk/monitor/azure-monitor-opentelemetry/tests/exporter/test_exporter.py index 21561ac56bdc..f5ac3b76f0aa 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/exporter/test_exporter.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/exporter/test_exporter.py @@ -1,38 +1,38 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# # Copyright The OpenTelemetry Authors +# # +# # Licensed under the Apache License, Version 2.0 (the "License"); +# # you may not use this file except in compliance with the License. +# # You may obtain a copy of the License at +# # +# # http://www.apache.org/licenses/LICENSE-2.0 +# # +# # Unless required by applicable law or agreed to in writing, software +# # distributed under the License is distributed on an "AS IS" BASIS, +# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# # See the License for the specific language governing permissions and +# # limitations under the License. -import unittest +# import unittest -from azure.monitor.opentelemetry.exporter import ( - AzureMonitorLogExporter, - AzureMonitorMetricExporter, - AzureMonitorTraceExporter, -) +# from azure.monitor.opentelemetry.exporter import ( +# AzureMonitorLogExporter, +# AzureMonitorMetricExporter, +# AzureMonitorTraceExporter, +# ) -class TestAzureMonitorExporters(unittest.TestCase): - def test_constructors(self): - cs_string = "InstrumentationKey=1234abcd-5678-4efa-8abc-1234567890ab" - for exporter in [ - AzureMonitorLogExporter, - AzureMonitorMetricExporter, - AzureMonitorTraceExporter, - ]: - try: - exporter(connection_string=cs_string) - except Exception as ex: # pylint: disable=broad-except - print(ex) - self.fail( - f"Unexpected exception raised when instantiating {exporter.__name__}" - ) +# class TestAzureMonitorExporters(unittest.TestCase): +# def test_constructors(self): +# cs_string = "InstrumentationKey=1234abcd-5678-4efa-8abc-1234567890ab" +# for exporter in [ +# AzureMonitorLogExporter, +# AzureMonitorMetricExporter, +# AzureMonitorTraceExporter, +# ]: +# try: +# exporter(connection_string=cs_string) +# except Exception as ex: # pylint: disable=broad-except +# print(ex) +# self.fail( +# f"Unexpected exception raised when instantiating {exporter.__name__}" +# ) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py index b78f1846bf98..87a7c5939d52 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py @@ -1,171 +1,171 @@ -# ------------------------------------------------------------------------- -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. See License in the project root for -# license information. -# -------------------------------------------------------------------------- - -from importlib import reload -from os import environ -from unittest import TestCase -from unittest.mock import patch - -from azure.monitor.opentelemetry import _constants - -TEST_VALUE = "TEST_VALUE" -TEST_IKEY = "1234abcd-ab12-34cd-ab12-a23456abcdef" -TEST_CONN_STR = f"InstrumentationKey={TEST_IKEY};IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/" - - -def clear_env_var(env_var): - if env_var in environ: - del environ[env_var] - - -class TestConstants(TestCase): - @patch.dict( - "os.environ", - {"ApplicationInsightsAgent_EXTENSION_VERSION": TEST_VALUE}, - ) - def test_extension_version(self): - reload(_constants) - self.assertEqual(_constants._EXTENSION_VERSION, TEST_VALUE) - - def test_extension_version_default(self): - clear_env_var("ApplicationInsightsAgent_EXTENSION_VERSION") - reload(_constants) - self.assertEqual(_constants._EXTENSION_VERSION, "disabled") - - @patch.dict( - "os.environ", {"APPLICATIONINSIGHTS_CONNECTION_STRING": TEST_CONN_STR} - ) - def test_ikey(self): - reload(_constants) - self.assertEqual( - _constants._get_customer_ikey_from_env_var(), TEST_IKEY - ) - - def test_ikey_defaults(self): - clear_env_var("APPLICATIONINSIGHTS_CONNECTION_STRING") - reload(_constants) - self.assertEqual( - _constants._get_customer_ikey_from_env_var(), "unknown" - ) - - # TODO: Enabled when duplicate logging issue is solved - # @patch.dict( - # "os.environ", - # {"AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS": "True"}, - # ) - # def test_exporter_diagnostics_enabled(self): - # reload(_constants) - # self.assertTrue(_constants._EXPORTER_DIAGNOSTICS_ENABLED) - - # def test_exporter_diagnostics_disabled(self): - # clear_env_var("AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS") - # reload(_constants) - # self.assertFalse(_constants._EXPORTER_DIAGNOSTICS_ENABLED) - - # @patch.dict( - # "os.environ", - # {"AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS": "foobar"}, - # ) - # def test_exporter_diagnostics_other(self): - # reload(_constants) - # self.assertFalse(_constants._EXPORTER_DIAGNOSTICS_ENABLED) - - @patch.dict("os.environ", {"WEBSITE_SITE_NAME": TEST_VALUE}) - def test_diagnostics_enabled(self): - reload(_constants) - self.assertTrue(_constants._IS_DIAGNOSTICS_ENABLED) - - def test_diagnostics_disabled(self): - clear_env_var("WEBSITE_SITE_NAME") - reload(_constants) - self.assertFalse(_constants._IS_DIAGNOSTICS_ENABLED) - - @patch( - "azure.monitor.opentelemetry._constants.platform.system", - return_value="Linux", - ) - def test_log_path_linux(self, mock_system): - self.assertEqual( - _constants._get_log_path(), "/var/log/applicationinsights" - ) - - @patch( - "azure.monitor.opentelemetry._constants.platform.system", - return_value="Linux", - ) - def test_status_log_path_linux(self, mock_system): - self.assertEqual( - _constants._get_log_path(status_log_path=True), - "/var/log/applicationinsights", - ) - - @patch( - "azure.monitor.opentelemetry._constants.platform.system", - return_value="Windows", - ) - @patch("pathlib.Path.home", return_value="\\HOME\\DIR") - def test_log_path_windows(self, mock_system, mock_home): - self.assertEqual( - _constants._get_log_path(), - "\\HOME\\DIR\\LogFiles\\ApplicationInsights", - ) - - @patch( - "azure.monitor.opentelemetry._constants.platform.system", - return_value="Windows", - ) - @patch("pathlib.Path.home", return_value="\\HOME\\DIR") - def test_status_log_path_windows(self, mock_system, mock_home): - self.assertEqual( - _constants._get_log_path(status_log_path=True), - "\\HOME\\DIR\\LogFiles\\ApplicationInsights\\status", - ) - - @patch( - "azure.monitor.opentelemetry._constants.platform.system", - return_value="Window", - ) - def test_log_path_other(self, mock_platform): - self.assertIsNone(_constants._get_log_path()) - - @patch( - "azure.monitor.opentelemetry._constants.platform.system", - return_value="linux", - ) - def test_status_log_path_other(self, mock_platform): - self.assertIsNone(_constants._get_log_path(status_log_path=True)) - - @patch.dict("os.environ", {"key": "value"}) - def test_env_var_or_default(self): - self.assertEqual(_constants._env_var_or_default("key"), "value") - - @patch.dict("os.environ", {}) - def test_env_var_or_default_empty(self): - self.assertEqual(_constants._env_var_or_default("key"), "") - - @patch.dict("os.environ", {}) - def test_env_var_or_default_empty_with_defaults(self): - self.assertEqual( - _constants._env_var_or_default("key", default_val="value"), "value" - ) - - @patch( - "azure.monitor.opentelemetry._constants.isdir", - return_value=True, - ) - def test_attach_enabled(self, mock_isdir): - self.assertEqual( - _constants._is_attach_enabled(), True - ) - - @patch( - "azure.monitor.opentelemetry._constants.isdir", - return_value=False, - ) - def test_attach_disabled(self, mock_isdir): - self.assertEqual( - _constants._is_attach_enabled(), False - ) +# # ------------------------------------------------------------------------- +# # Copyright (c) Microsoft Corporation. All rights reserved. +# # Licensed under the MIT License. See License in the project root for +# # license information. +# # -------------------------------------------------------------------------- + +# from importlib import reload +# from os import environ +# from unittest import TestCase +# from unittest.mock import patch + +# from azure.monitor.opentelemetry import _constants + +# TEST_VALUE = "TEST_VALUE" +# TEST_IKEY = "1234abcd-ab12-34cd-ab12-a23456abcdef" +# TEST_CONN_STR = f"InstrumentationKey={TEST_IKEY};IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/" + + +# def clear_env_var(env_var): +# if env_var in environ: +# del environ[env_var] + + +# class TestConstants(TestCase): +# @patch.dict( +# "os.environ", +# {"ApplicationInsightsAgent_EXTENSION_VERSION": TEST_VALUE}, +# ) +# def test_extension_version(self): +# reload(_constants) +# self.assertEqual(_constants._EXTENSION_VERSION, TEST_VALUE) + +# def test_extension_version_default(self): +# clear_env_var("ApplicationInsightsAgent_EXTENSION_VERSION") +# reload(_constants) +# self.assertEqual(_constants._EXTENSION_VERSION, "disabled") + +# @patch.dict( +# "os.environ", {"APPLICATIONINSIGHTS_CONNECTION_STRING": TEST_CONN_STR} +# ) +# def test_ikey(self): +# reload(_constants) +# self.assertEqual( +# _constants._get_customer_ikey_from_env_var(), TEST_IKEY +# ) + +# def test_ikey_defaults(self): +# clear_env_var("APPLICATIONINSIGHTS_CONNECTION_STRING") +# reload(_constants) +# self.assertEqual( +# _constants._get_customer_ikey_from_env_var(), "unknown" +# ) + +# # TODO: Enabled when duplicate logging issue is solved +# # @patch.dict( +# # "os.environ", +# # {"AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS": "True"}, +# # ) +# # def test_exporter_diagnostics_enabled(self): +# # reload(_constants) +# # self.assertTrue(_constants._EXPORTER_DIAGNOSTICS_ENABLED) + +# # def test_exporter_diagnostics_disabled(self): +# # clear_env_var("AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS") +# # reload(_constants) +# # self.assertFalse(_constants._EXPORTER_DIAGNOSTICS_ENABLED) + +# # @patch.dict( +# # "os.environ", +# # {"AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS": "foobar"}, +# # ) +# # def test_exporter_diagnostics_other(self): +# # reload(_constants) +# # self.assertFalse(_constants._EXPORTER_DIAGNOSTICS_ENABLED) + +# @patch.dict("os.environ", {"WEBSITE_SITE_NAME": TEST_VALUE}) +# def test_diagnostics_enabled(self): +# reload(_constants) +# self.assertTrue(_constants._IS_DIAGNOSTICS_ENABLED) + +# def test_diagnostics_disabled(self): +# clear_env_var("WEBSITE_SITE_NAME") +# reload(_constants) +# self.assertFalse(_constants._IS_DIAGNOSTICS_ENABLED) + +# @patch( +# "azure.monitor.opentelemetry._constants.platform.system", +# return_value="Linux", +# ) +# def test_log_path_linux(self, mock_system): +# self.assertEqual( +# _constants._get_log_path(), "/var/log/applicationinsights" +# ) + +# @patch( +# "azure.monitor.opentelemetry._constants.platform.system", +# return_value="Linux", +# ) +# def test_status_log_path_linux(self, mock_system): +# self.assertEqual( +# _constants._get_log_path(status_log_path=True), +# "/var/log/applicationinsights", +# ) + +# @patch( +# "azure.monitor.opentelemetry._constants.platform.system", +# return_value="Windows", +# ) +# @patch("pathlib.Path.home", return_value="\\HOME\\DIR") +# def test_log_path_windows(self, mock_system, mock_home): +# self.assertEqual( +# _constants._get_log_path(), +# "\\HOME\\DIR\\LogFiles\\ApplicationInsights", +# ) + +# @patch( +# "azure.monitor.opentelemetry._constants.platform.system", +# return_value="Windows", +# ) +# @patch("pathlib.Path.home", return_value="\\HOME\\DIR") +# def test_status_log_path_windows(self, mock_system, mock_home): +# self.assertEqual( +# _constants._get_log_path(status_log_path=True), +# "\\HOME\\DIR\\LogFiles\\ApplicationInsights\\status", +# ) + +# @patch( +# "azure.monitor.opentelemetry._constants.platform.system", +# return_value="Window", +# ) +# def test_log_path_other(self, mock_platform): +# self.assertIsNone(_constants._get_log_path()) + +# @patch( +# "azure.monitor.opentelemetry._constants.platform.system", +# return_value="linux", +# ) +# def test_status_log_path_other(self, mock_platform): +# self.assertIsNone(_constants._get_log_path(status_log_path=True)) + +# @patch.dict("os.environ", {"key": "value"}) +# def test_env_var_or_default(self): +# self.assertEqual(_constants._env_var_or_default("key"), "value") + +# @patch.dict("os.environ", {}) +# def test_env_var_or_default_empty(self): +# self.assertEqual(_constants._env_var_or_default("key"), "") + +# @patch.dict("os.environ", {}) +# def test_env_var_or_default_empty_with_defaults(self): +# self.assertEqual( +# _constants._env_var_or_default("key", default_val="value"), "value" +# ) + +# @patch( +# "azure.monitor.opentelemetry._constants.isdir", +# return_value=True, +# ) +# def test_attach_enabled(self, mock_isdir): +# self.assertEqual( +# _constants._is_attach_enabled(), True +# ) + +# @patch( +# "azure.monitor.opentelemetry._constants.isdir", +# return_value=False, +# ) +# def test_attach_disabled(self, mock_isdir): +# self.assertEqual( +# _constants._is_attach_enabled(), False +# ) From 995d1600f7a9d643ada88844c6237309da475b69 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 10:03:59 -0700 Subject: [PATCH 09/22] Update setup.py --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 82332be1c198..53666f9f6a75 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,6 +88,7 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", + "opentelemetry-api~=1.20" "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", @@ -95,6 +96,7 @@ "opentelemetry-instrumentation-requests~=0.41b0", "opentelemetry-instrumentation-urllib~=0.41b0", "opentelemetry-instrumentation-urllib3~=0.41b0", + "opentelemetry-sdk~=1.20", ], entry_points={ "opentelemetry_distro": [ From 0264861237ed3f1d66bf325cd7fdd7cd79e12127 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 10:21:51 -0700 Subject: [PATCH 10/22] Update setup.py --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 53666f9f6a75..492d9f350bd0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,7 +88,7 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", - "opentelemetry-api~=1.20" + "opentelemetry-api~=1.20", "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", From 66ca6ea629268f0bb97e4e3ad79d809890e80909 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 10:40:25 -0700 Subject: [PATCH 11/22] test --- .../tests/configuration/test_configure.py | 814 +++++++++--------- .../tests/configuration/test_util.py | 190 ++-- 2 files changed, 502 insertions(+), 502 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py index 446ce56b10df..9b55303617b7 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py @@ -1,425 +1,425 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# # Copyright The OpenTelemetry Authors +# # +# # Licensed under the Apache License, Version 2.0 (the "License"); +# # you may not use this file except in compliance with the License. +# # You may obtain a copy of the License at +# # +# # http://www.apache.org/licenses/LICENSE-2.0 +# # +# # Unless required by applicable law or agreed to in writing, software +# # distributed under the License is distributed on an "AS IS" BASIS, +# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# # See the License for the specific language governing permissions and +# # limitations under the License. -import unittest -from unittest.mock import Mock, patch +# import unittest +# from unittest.mock import Mock, patch -from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan -from azure.monitor.opentelemetry._configure import ( - _SUPPORTED_INSTRUMENTED_LIBRARIES, - _setup_instrumentations, - _setup_logging, - _setup_metrics, - _setup_tracing, - configure_azure_monitor, -) +# from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan +# from azure.monitor.opentelemetry._configure import ( +# _SUPPORTED_INSTRUMENTED_LIBRARIES, +# _setup_instrumentations, +# _setup_logging, +# _setup_metrics, +# _setup_tracing, +# configure_azure_monitor, +# ) -class TestConfigure(unittest.TestCase): - @patch( - "azure.monitor.opentelemetry._configure._setup_instrumentations", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_metrics", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_logging", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_tracing", - ) - def test_configure_azure_monitor( - self, - tracing_mock, - logging_mock, - metrics_mock, - instrumentation_mock, - ): - kwargs = { - "connection_string": "test_cs", - } - configure_azure_monitor(**kwargs) - tracing_mock.assert_called_once() - logging_mock.assert_called_once() - metrics_mock.assert_called_once() - instrumentation_mock.assert_called_once() +# class TestConfigure(unittest.TestCase): +# @patch( +# "azure.monitor.opentelemetry._configure._setup_instrumentations", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_metrics", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_logging", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_tracing", +# ) +# def test_configure_azure_monitor( +# self, +# tracing_mock, +# logging_mock, +# metrics_mock, +# instrumentation_mock, +# ): +# kwargs = { +# "connection_string": "test_cs", +# } +# configure_azure_monitor(**kwargs) +# tracing_mock.assert_called_once() +# logging_mock.assert_called_once() +# metrics_mock.assert_called_once() +# instrumentation_mock.assert_called_once() - @patch( - "azure.monitor.opentelemetry._configure._setup_instrumentations", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_metrics", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_logging", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_tracing", - ) - @patch( - "azure.monitor.opentelemetry._configure._get_configurations", - ) - def test_configure_azure_monitor_disable_tracing( - self, - config_mock, - tracing_mock, - logging_mock, - metrics_mock, - instrumentation_mock, - ): - configurations = { - "connection_string": "test_cs", - "disable_tracing": True, - "disable_logging": False, - "disable_metrics": False, - } - config_mock.return_value = configurations - configure_azure_monitor() - tracing_mock.assert_not_called() - logging_mock.assert_called_once_with(configurations) - metrics_mock.assert_called_once_with(configurations) - instrumentation_mock.assert_called_once_with(configurations) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_instrumentations", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_metrics", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_logging", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_tracing", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._get_configurations", +# ) +# def test_configure_azure_monitor_disable_tracing( +# self, +# config_mock, +# tracing_mock, +# logging_mock, +# metrics_mock, +# instrumentation_mock, +# ): +# configurations = { +# "connection_string": "test_cs", +# "disable_tracing": True, +# "disable_logging": False, +# "disable_metrics": False, +# } +# config_mock.return_value = configurations +# configure_azure_monitor() +# tracing_mock.assert_not_called() +# logging_mock.assert_called_once_with(configurations) +# metrics_mock.assert_called_once_with(configurations) +# instrumentation_mock.assert_called_once_with(configurations) - @patch( - "azure.monitor.opentelemetry._configure._setup_instrumentations", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_metrics", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_logging", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_tracing", - ) - @patch( - "azure.monitor.opentelemetry._configure._get_configurations", - ) - def test_configure_azure_monitor_disable_logging( - self, - config_mock, - tracing_mock, - logging_mock, - metrics_mock, - instrumentation_mock, - ): - configurations = { - "connection_string": "test_cs", - "disable_tracing": False, - "disable_logging": True, - "disable_metrics": False, - } - config_mock.return_value = configurations - configure_azure_monitor() - tracing_mock.assert_called_once_with(configurations) - logging_mock.assert_not_called() - metrics_mock.assert_called_once_with(configurations) - instrumentation_mock.assert_called_once_with(configurations) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_instrumentations", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_metrics", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_logging", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_tracing", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._get_configurations", +# ) +# def test_configure_azure_monitor_disable_logging( +# self, +# config_mock, +# tracing_mock, +# logging_mock, +# metrics_mock, +# instrumentation_mock, +# ): +# configurations = { +# "connection_string": "test_cs", +# "disable_tracing": False, +# "disable_logging": True, +# "disable_metrics": False, +# } +# config_mock.return_value = configurations +# configure_azure_monitor() +# tracing_mock.assert_called_once_with(configurations) +# logging_mock.assert_not_called() +# metrics_mock.assert_called_once_with(configurations) +# instrumentation_mock.assert_called_once_with(configurations) - @patch( - "azure.monitor.opentelemetry._configure._setup_instrumentations", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_metrics", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_logging", - ) - @patch( - "azure.monitor.opentelemetry._configure._setup_tracing", - ) - @patch( - "azure.monitor.opentelemetry._configure._get_configurations", - ) - def test_configure_azure_monitor_disable_metrics( - self, - config_mock, - tracing_mock, - logging_mock, - metrics_mock, - instrumentation_mock, - ): - configurations = { - "connection_string": "test_cs", - "disable_tracing": False, - "disable_logging": False, - "disable_metrics": True, - } - config_mock.return_value = configurations - configure_azure_monitor() - tracing_mock.assert_called_once_with(configurations) - logging_mock.assert_called_once_with(configurations) - metrics_mock.assert_not_called() - instrumentation_mock.assert_called_once_with(configurations) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_instrumentations", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_metrics", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_logging", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._setup_tracing", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure._get_configurations", +# ) +# def test_configure_azure_monitor_disable_metrics( +# self, +# config_mock, +# tracing_mock, +# logging_mock, +# metrics_mock, +# instrumentation_mock, +# ): +# configurations = { +# "connection_string": "test_cs", +# "disable_tracing": False, +# "disable_logging": False, +# "disable_metrics": True, +# } +# config_mock.return_value = configurations +# configure_azure_monitor() +# tracing_mock.assert_called_once_with(configurations) +# logging_mock.assert_called_once_with(configurations) +# metrics_mock.assert_not_called() +# instrumentation_mock.assert_called_once_with(configurations) - @patch( - "azure.monitor.opentelemetry._configure.settings", - ) - @patch( - "azure.monitor.opentelemetry._configure.BatchSpanProcessor", - ) - @patch( - "azure.monitor.opentelemetry._configure.AzureMonitorTraceExporter", - ) - @patch( - "azure.monitor.opentelemetry._configure.get_tracer_provider", - ) - @patch( - "azure.monitor.opentelemetry._configure.set_tracer_provider", - ) - @patch( - "azure.monitor.opentelemetry._configure.TracerProvider", - autospec=True, - ) - @patch( - "azure.monitor.opentelemetry._configure.ApplicationInsightsSampler", - ) - def test_setup_tracing( - self, - sampler_mock, - tp_mock, - set_tracer_provider_mock, - get_tracer_provider_mock, - trace_exporter_mock, - bsp_mock, - azure_core_mock, - ): - sampler_init_mock = Mock() - sampler_mock.return_value = sampler_init_mock - tp_init_mock = Mock() - tp_mock.return_value = tp_init_mock - get_tracer_provider_mock.return_value = tp_init_mock - trace_exp_init_mock = Mock() - trace_exporter_mock.return_value = trace_exp_init_mock - bsp_init_mock = Mock() - bsp_mock.return_value = bsp_init_mock +# @patch( +# "azure.monitor.opentelemetry._configure.settings", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.BatchSpanProcessor", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.AzureMonitorTraceExporter", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.get_tracer_provider", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.set_tracer_provider", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.TracerProvider", +# autospec=True, +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.ApplicationInsightsSampler", +# ) +# def test_setup_tracing( +# self, +# sampler_mock, +# tp_mock, +# set_tracer_provider_mock, +# get_tracer_provider_mock, +# trace_exporter_mock, +# bsp_mock, +# azure_core_mock, +# ): +# sampler_init_mock = Mock() +# sampler_mock.return_value = sampler_init_mock +# tp_init_mock = Mock() +# tp_mock.return_value = tp_init_mock +# get_tracer_provider_mock.return_value = tp_init_mock +# trace_exp_init_mock = Mock() +# trace_exporter_mock.return_value = trace_exp_init_mock +# bsp_init_mock = Mock() +# bsp_mock.return_value = bsp_init_mock - configurations = { - "connection_string": "test_cs", - "disable_azure_core_tracing": False, - "sampling_ratio": 0.5, - } - _setup_tracing(configurations) - sampler_mock.assert_called_once_with(sampling_ratio=0.5) - tp_mock.assert_called_once_with( - sampler=sampler_init_mock, - ) - set_tracer_provider_mock.assert_called_once_with(tp_init_mock) - get_tracer_provider_mock.assert_called() - trace_exporter_mock.assert_called_once_with(**configurations) - bsp_mock.assert_called_once_with(trace_exp_init_mock) - tp_init_mock.add_span_processor.assert_called_once_with(bsp_init_mock) - self.assertEqual( - azure_core_mock.tracing_implementation, OpenTelemetrySpan - ) +# configurations = { +# "connection_string": "test_cs", +# "disable_azure_core_tracing": False, +# "sampling_ratio": 0.5, +# } +# _setup_tracing(configurations) +# sampler_mock.assert_called_once_with(sampling_ratio=0.5) +# tp_mock.assert_called_once_with( +# sampler=sampler_init_mock, +# ) +# set_tracer_provider_mock.assert_called_once_with(tp_init_mock) +# get_tracer_provider_mock.assert_called() +# trace_exporter_mock.assert_called_once_with(**configurations) +# bsp_mock.assert_called_once_with(trace_exp_init_mock) +# tp_init_mock.add_span_processor.assert_called_once_with(bsp_init_mock) +# self.assertEqual( +# azure_core_mock.tracing_implementation, OpenTelemetrySpan +# ) - @patch( - "azure.monitor.opentelemetry._configure.getLogger", - ) - @patch( - "azure.monitor.opentelemetry._configure.LoggingHandler", - ) - @patch( - "azure.monitor.opentelemetry._configure.BatchLogRecordProcessor", - ) - @patch( - "azure.monitor.opentelemetry._configure.AzureMonitorLogExporter", - ) - @patch( - "azure.monitor.opentelemetry._configure.get_logger_provider", - ) - @patch( - "azure.monitor.opentelemetry._configure.set_logger_provider", - ) - @patch( - "azure.monitor.opentelemetry._configure.LoggerProvider", - autospec=True, - ) - def test_setup_logging( - self, - lp_mock, - set_logger_provider_mock, - get_logger_provider_mock, - log_exporter_mock, - blrp_mock, - logging_handler_mock, - get_logger_mock, - ): - lp_init_mock = Mock() - lp_mock.return_value = lp_init_mock - get_logger_provider_mock.return_value = lp_init_mock - log_exp_init_mock = Mock() - log_exporter_mock.return_value = log_exp_init_mock - blrp_init_mock = Mock() - blrp_mock.return_value = blrp_init_mock - logging_handler_init_mock = Mock() - logging_handler_mock.return_value = logging_handler_init_mock - logger_mock = Mock() - get_logger_mock.return_value = logger_mock +# @patch( +# "azure.monitor.opentelemetry._configure.getLogger", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.LoggingHandler", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.BatchLogRecordProcessor", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.AzureMonitorLogExporter", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.get_logger_provider", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.set_logger_provider", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.LoggerProvider", +# autospec=True, +# ) +# def test_setup_logging( +# self, +# lp_mock, +# set_logger_provider_mock, +# get_logger_provider_mock, +# log_exporter_mock, +# blrp_mock, +# logging_handler_mock, +# get_logger_mock, +# ): +# lp_init_mock = Mock() +# lp_mock.return_value = lp_init_mock +# get_logger_provider_mock.return_value = lp_init_mock +# log_exp_init_mock = Mock() +# log_exporter_mock.return_value = log_exp_init_mock +# blrp_init_mock = Mock() +# blrp_mock.return_value = blrp_init_mock +# logging_handler_init_mock = Mock() +# logging_handler_mock.return_value = logging_handler_init_mock +# logger_mock = Mock() +# get_logger_mock.return_value = logger_mock - configurations = { - "connection_string": "test_cs", - } - _setup_logging(configurations) +# configurations = { +# "connection_string": "test_cs", +# } +# _setup_logging(configurations) - lp_mock.assert_called_once_with() - set_logger_provider_mock.assert_called_once_with(lp_init_mock) - get_logger_provider_mock.assert_called() - log_exporter_mock.assert_called_once_with(**configurations) - blrp_mock.assert_called_once_with( - log_exp_init_mock, - ) - lp_init_mock.add_log_record_processor.assert_called_once_with( - blrp_init_mock - ) - logging_handler_mock.assert_called_once_with( - logger_provider=lp_init_mock - ) - get_logger_mock.assert_called_once_with() - logger_mock.addHandler.assert_called_once_with( - logging_handler_init_mock - ) +# lp_mock.assert_called_once_with() +# set_logger_provider_mock.assert_called_once_with(lp_init_mock) +# get_logger_provider_mock.assert_called() +# log_exporter_mock.assert_called_once_with(**configurations) +# blrp_mock.assert_called_once_with( +# log_exp_init_mock, +# ) +# lp_init_mock.add_log_record_processor.assert_called_once_with( +# blrp_init_mock +# ) +# logging_handler_mock.assert_called_once_with( +# logger_provider=lp_init_mock +# ) +# get_logger_mock.assert_called_once_with() +# logger_mock.addHandler.assert_called_once_with( +# logging_handler_init_mock +# ) - @patch( - "azure.monitor.opentelemetry._configure.PeriodicExportingMetricReader", - ) - @patch( - "azure.monitor.opentelemetry._configure.AzureMonitorMetricExporter", - ) - @patch( - "azure.monitor.opentelemetry._configure.set_meter_provider", - ) - @patch( - "azure.monitor.opentelemetry._configure.MeterProvider", - autospec=True, - ) - def test_setup_metrics( - self, - mp_mock, - set_meter_provider_mock, - metric_exporter_mock, - reader_mock, - ): - mp_init_mock = Mock() - mp_mock.return_value = mp_init_mock - metric_exp_init_mock = Mock() - metric_exporter_mock.return_value = metric_exp_init_mock - reader_init_mock = Mock() - reader_mock.return_value = reader_init_mock +# @patch( +# "azure.monitor.opentelemetry._configure.PeriodicExportingMetricReader", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.AzureMonitorMetricExporter", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.set_meter_provider", +# ) +# @patch( +# "azure.monitor.opentelemetry._configure.MeterProvider", +# autospec=True, +# ) +# def test_setup_metrics( +# self, +# mp_mock, +# set_meter_provider_mock, +# metric_exporter_mock, +# reader_mock, +# ): +# mp_init_mock = Mock() +# mp_mock.return_value = mp_init_mock +# metric_exp_init_mock = Mock() +# metric_exporter_mock.return_value = metric_exp_init_mock +# reader_init_mock = Mock() +# reader_mock.return_value = reader_init_mock - configurations = { - "connection_string": "test_cs", - } - _setup_metrics(configurations) - mp_mock.assert_called_once_with( - metric_readers=[reader_init_mock], - ) - set_meter_provider_mock.assert_called_once_with(mp_init_mock) - metric_exporter_mock.assert_called_once_with(**configurations) - reader_mock.assert_called_once_with(metric_exp_init_mock) +# configurations = { +# "connection_string": "test_cs", +# } +# _setup_metrics(configurations) +# mp_mock.assert_called_once_with( +# metric_readers=[reader_init_mock], +# ) +# set_meter_provider_mock.assert_called_once_with(mp_init_mock) +# metric_exporter_mock.assert_called_once_with(**configurations) +# reader_mock.assert_called_once_with(metric_exp_init_mock) - @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") - @patch("azure.monitor.opentelemetry._configure.iter_entry_points") - def test_setup_instrumentations_lib_not_supported( - self, - iter_mock, - dep_mock, - ): - ep_mock = Mock() - ep2_mock = Mock() - iter_mock.return_value = (ep_mock, ep2_mock) - instrumentor_mock = Mock() - instr_class_mock = Mock() - instr_class_mock.return_value = instrumentor_mock - ep_mock.name = "test_instr" - ep2_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] - ep2_mock.load.return_value = instr_class_mock - dep_mock.return_value = None - _setup_instrumentations({"disabled_instrumentations": []}) - dep_mock.assert_called_with(ep2_mock.dist) - ep_mock.load.assert_not_called() - ep2_mock.load.assert_called_once() - instrumentor_mock.instrument.assert_called_once() +# @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") +# @patch("azure.monitor.opentelemetry._configure.iter_entry_points") +# def test_setup_instrumentations_lib_not_supported( +# self, +# iter_mock, +# dep_mock, +# ): +# ep_mock = Mock() +# ep2_mock = Mock() +# iter_mock.return_value = (ep_mock, ep2_mock) +# instrumentor_mock = Mock() +# instr_class_mock = Mock() +# instr_class_mock.return_value = instrumentor_mock +# ep_mock.name = "test_instr" +# ep2_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] +# ep2_mock.load.return_value = instr_class_mock +# dep_mock.return_value = None +# _setup_instrumentations({"disabled_instrumentations": []}) +# dep_mock.assert_called_with(ep2_mock.dist) +# ep_mock.load.assert_not_called() +# ep2_mock.load.assert_called_once() +# instrumentor_mock.instrument.assert_called_once() - @patch("azure.monitor.opentelemetry._configure._logger") - @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") - @patch("azure.monitor.opentelemetry._configure.iter_entry_points") - def test_setup_instrumentations_conflict( - self, - iter_mock, - dep_mock, - logger_mock, - ): - ep_mock = Mock() - iter_mock.return_value = (ep_mock,) - instrumentor_mock = Mock() - instr_class_mock = Mock() - instr_class_mock.return_value = instrumentor_mock - ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] - ep_mock.load.return_value = instr_class_mock - dep_mock.return_value = True - _setup_instrumentations({"disabled_instrumentations": []}) - dep_mock.assert_called_with(ep_mock.dist) - ep_mock.load.assert_not_called() - instrumentor_mock.instrument.assert_not_called() - logger_mock.debug.assert_called_once() +# @patch("azure.monitor.opentelemetry._configure._logger") +# @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") +# @patch("azure.monitor.opentelemetry._configure.iter_entry_points") +# def test_setup_instrumentations_conflict( +# self, +# iter_mock, +# dep_mock, +# logger_mock, +# ): +# ep_mock = Mock() +# iter_mock.return_value = (ep_mock,) +# instrumentor_mock = Mock() +# instr_class_mock = Mock() +# instr_class_mock.return_value = instrumentor_mock +# ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] +# ep_mock.load.return_value = instr_class_mock +# dep_mock.return_value = True +# _setup_instrumentations({"disabled_instrumentations": []}) +# dep_mock.assert_called_with(ep_mock.dist) +# ep_mock.load.assert_not_called() +# instrumentor_mock.instrument.assert_not_called() +# logger_mock.debug.assert_called_once() - @patch("azure.monitor.opentelemetry._configure._logger") - @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") - @patch("azure.monitor.opentelemetry._configure.iter_entry_points") - def test_setup_instrumentations_exception( - self, - iter_mock, - dep_mock, - logger_mock, - ): - ep_mock = Mock() - iter_mock.return_value = (ep_mock,) - instrumentor_mock = Mock() - instr_class_mock = Mock() - instr_class_mock.return_value = instrumentor_mock - ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] - ep_mock.load.side_effect = Exception() - dep_mock.return_value = None - _setup_instrumentations({"disabled_instrumentations": []}) - dep_mock.assert_called_with(ep_mock.dist) - ep_mock.load.assert_called_once() - instrumentor_mock.instrument.assert_not_called() - logger_mock.warning.assert_called_once() +# @patch("azure.monitor.opentelemetry._configure._logger") +# @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") +# @patch("azure.monitor.opentelemetry._configure.iter_entry_points") +# def test_setup_instrumentations_exception( +# self, +# iter_mock, +# dep_mock, +# logger_mock, +# ): +# ep_mock = Mock() +# iter_mock.return_value = (ep_mock,) +# instrumentor_mock = Mock() +# instr_class_mock = Mock() +# instr_class_mock.return_value = instrumentor_mock +# ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] +# ep_mock.load.side_effect = Exception() +# dep_mock.return_value = None +# _setup_instrumentations({"disabled_instrumentations": []}) +# dep_mock.assert_called_with(ep_mock.dist) +# ep_mock.load.assert_called_once() +# instrumentor_mock.instrument.assert_not_called() +# logger_mock.warning.assert_called_once() - @patch("azure.monitor.opentelemetry._configure._logger") - @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") - @patch("azure.monitor.opentelemetry._configure.iter_entry_points") - def test_setup_instrumentations_disabled( - self, - iter_mock, - dep_mock, - logger_mock, - ): - ep_mock = Mock() - ep2_mock = Mock() - iter_mock.return_value = (ep_mock, ep2_mock) - instrumentor_mock = Mock() - instr_class_mock = Mock() - instr_class_mock.return_value = instrumentor_mock - ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] - ep2_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[1] - ep2_mock.load.return_value = instr_class_mock - dep_mock.return_value = None - _setup_instrumentations({"disabled_instrumentations": [ep_mock.name]}) - dep_mock.assert_called_with(ep2_mock.dist) - ep_mock.load.assert_not_called() - ep2_mock.load.assert_called_once() - instrumentor_mock.instrument.assert_called_once() - logger_mock.debug.assert_called_once() +# @patch("azure.monitor.opentelemetry._configure._logger") +# @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") +# @patch("azure.monitor.opentelemetry._configure.iter_entry_points") +# def test_setup_instrumentations_disabled( +# self, +# iter_mock, +# dep_mock, +# logger_mock, +# ): +# ep_mock = Mock() +# ep2_mock = Mock() +# iter_mock.return_value = (ep_mock, ep2_mock) +# instrumentor_mock = Mock() +# instr_class_mock = Mock() +# instr_class_mock.return_value = instrumentor_mock +# ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] +# ep2_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[1] +# ep2_mock.load.return_value = instr_class_mock +# dep_mock.return_value = None +# _setup_instrumentations({"disabled_instrumentations": [ep_mock.name]}) +# dep_mock.assert_called_with(ep2_mock.dist) +# ep_mock.load.assert_not_called() +# ep2_mock.load.assert_called_once() +# instrumentor_mock.instrument.assert_called_once() +# logger_mock.debug.assert_called_once() diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py index 6ddba50805c6..746ca023cf20 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py @@ -1,106 +1,106 @@ -# Copyright The OpenTelemetry Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +# # Copyright The OpenTelemetry Authors +# # +# # Licensed under the Apache License, Version 2.0 (the "License"); +# # you may not use this file except in compliance with the License. +# # You may obtain a copy of the License at +# # +# # http://www.apache.org/licenses/LICENSE-2.0 +# # +# # Unless required by applicable law or agreed to in writing, software +# # distributed under the License is distributed on an "AS IS" BASIS, +# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# # See the License for the specific language governing permissions and +# # limitations under the License. -from unittest import TestCase -from unittest.mock import patch +# from unittest import TestCase +# from unittest.mock import patch -from opentelemetry.instrumentation.environment_variables import ( - OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, -) -from azure.monitor.opentelemetry._util.configurations import ( - SAMPLING_RATIO_ENV_VAR, - _get_configurations, -) -from opentelemetry.environment_variables import ( - OTEL_LOGS_EXPORTER, - OTEL_METRICS_EXPORTER, - OTEL_TRACES_EXPORTER, -) +# from opentelemetry.instrumentation.environment_variables import ( +# OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, +# ) +# from azure.monitor.opentelemetry._util.configurations import ( +# SAMPLING_RATIO_ENV_VAR, +# _get_configurations, +# ) +# from opentelemetry.environment_variables import ( +# OTEL_LOGS_EXPORTER, +# OTEL_METRICS_EXPORTER, +# OTEL_TRACES_EXPORTER, +# ) -class TestUtil(TestCase): - def test_get_configurations(self): - configurations = _get_configurations( - connection_string="test_cs", - credential="test_credential", - ) +# class TestUtil(TestCase): +# def test_get_configurations(self): +# configurations = _get_configurations( +# connection_string="test_cs", +# credential="test_credential", +# ) - self.assertEqual(configurations["connection_string"], "test_cs") - self.assertEqual(configurations["disable_azure_core_tracing"], False) - self.assertEqual(configurations["disable_logging"], False) - self.assertEqual(configurations["disable_metrics"], False) - self.assertEqual(configurations["disable_tracing"], False) - self.assertEqual(configurations["disabled_instrumentations"], []) - self.assertEqual(configurations["sampling_ratio"], 1.0) - self.assertEqual(configurations["credential"], ("test_credential")) - self.assertTrue("storage_directory" not in configurations) +# self.assertEqual(configurations["connection_string"], "test_cs") +# self.assertEqual(configurations["disable_azure_core_tracing"], False) +# self.assertEqual(configurations["disable_logging"], False) +# self.assertEqual(configurations["disable_metrics"], False) +# self.assertEqual(configurations["disable_tracing"], False) +# self.assertEqual(configurations["disabled_instrumentations"], []) +# self.assertEqual(configurations["sampling_ratio"], 1.0) +# self.assertEqual(configurations["credential"], ("test_credential")) +# self.assertTrue("storage_directory" not in configurations) - @patch.dict("os.environ", {}, clear=True) - def test_get_configurations_defaults(self): - configurations = _get_configurations() +# @patch.dict("os.environ", {}, clear=True) +# def test_get_configurations_defaults(self): +# configurations = _get_configurations() - self.assertTrue("connection_string" not in configurations) - self.assertEqual(configurations["disable_azure_core_tracing"], False) - self.assertEqual(configurations["disable_logging"], False) - self.assertEqual(configurations["disable_metrics"], False) - self.assertEqual(configurations["disable_tracing"], False) - self.assertEqual(configurations["disabled_instrumentations"], []) - self.assertEqual(configurations["sampling_ratio"], 1.0) - self.assertTrue("credential" not in configurations) - self.assertTrue("storage_directory" not in configurations) +# self.assertTrue("connection_string" not in configurations) +# self.assertEqual(configurations["disable_azure_core_tracing"], False) +# self.assertEqual(configurations["disable_logging"], False) +# self.assertEqual(configurations["disable_metrics"], False) +# self.assertEqual(configurations["disable_tracing"], False) +# self.assertEqual(configurations["disabled_instrumentations"], []) +# self.assertEqual(configurations["sampling_ratio"], 1.0) +# self.assertTrue("credential" not in configurations) +# self.assertTrue("storage_directory" not in configurations) - @patch.dict( - "os.environ", - { - OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: "flask , requests,fastapi", - SAMPLING_RATIO_ENV_VAR: "0.5", - OTEL_TRACES_EXPORTER: "None", - OTEL_LOGS_EXPORTER: "none", - OTEL_METRICS_EXPORTER: "NONE", - }, - clear=True, - ) - def test_get_configurations_env_vars(self): - configurations = _get_configurations() +# @patch.dict( +# "os.environ", +# { +# OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: "flask , requests,fastapi", +# SAMPLING_RATIO_ENV_VAR: "0.5", +# OTEL_TRACES_EXPORTER: "None", +# OTEL_LOGS_EXPORTER: "none", +# OTEL_METRICS_EXPORTER: "NONE", +# }, +# clear=True, +# ) +# def test_get_configurations_env_vars(self): +# configurations = _get_configurations() - self.assertTrue("connection_string" not in configurations) - self.assertEqual(configurations["disable_azure_core_tracing"], False) - self.assertEqual(configurations["disable_logging"], True) - self.assertEqual(configurations["disable_metrics"], True) - self.assertEqual(configurations["disable_tracing"], True) - self.assertEqual( - configurations["disabled_instrumentations"], - ["flask", "requests", "fastapi"], - ) - self.assertEqual(configurations["sampling_ratio"], 0.5) +# self.assertTrue("connection_string" not in configurations) +# self.assertEqual(configurations["disable_azure_core_tracing"], False) +# self.assertEqual(configurations["disable_logging"], True) +# self.assertEqual(configurations["disable_metrics"], True) +# self.assertEqual(configurations["disable_tracing"], True) +# self.assertEqual( +# configurations["disabled_instrumentations"], +# ["flask", "requests", "fastapi"], +# ) +# self.assertEqual(configurations["sampling_ratio"], 0.5) - @patch.dict( - "os.environ", - { - SAMPLING_RATIO_ENV_VAR: "Half", - OTEL_TRACES_EXPORTER: "False", - OTEL_LOGS_EXPORTER: "no", - OTEL_METRICS_EXPORTER: "True", - }, - clear=True, - ) - def test_get_configurations_env_vars_validation(self): - configurations = _get_configurations() +# @patch.dict( +# "os.environ", +# { +# SAMPLING_RATIO_ENV_VAR: "Half", +# OTEL_TRACES_EXPORTER: "False", +# OTEL_LOGS_EXPORTER: "no", +# OTEL_METRICS_EXPORTER: "True", +# }, +# clear=True, +# ) +# def test_get_configurations_env_vars_validation(self): +# configurations = _get_configurations() - self.assertTrue("connection_string" not in configurations) - self.assertEqual(configurations["disable_azure_core_tracing"], False) - self.assertEqual(configurations["disable_logging"], False) - self.assertEqual(configurations["disable_metrics"], False) - self.assertEqual(configurations["disable_tracing"], False) - self.assertEqual(configurations["sampling_ratio"], 1.0) +# self.assertTrue("connection_string" not in configurations) +# self.assertEqual(configurations["disable_azure_core_tracing"], False) +# self.assertEqual(configurations["disable_logging"], False) +# self.assertEqual(configurations["disable_metrics"], False) +# self.assertEqual(configurations["disable_tracing"], False) +# self.assertEqual(configurations["sampling_ratio"], 1.0) From 34139694773fa96b77e0c8dd1a7368632b0d82cb Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 10:52:42 -0700 Subject: [PATCH 12/22] dev dep --- .../dev_requirements.txt | 1 - .../dev_requirements.txt | 1 + .../azure-monitor-opentelemetry/setup.py | 2 - .../autoinstrumentation/test_configurator.py | 54 +- .../tests/autoinstrumentation/test_distro.py | 70 +- .../tests/configuration/test_configure.py | 814 +++++++++--------- .../tests/configuration/test_util.py | 190 ++-- .../diagnostics/test_diagnostic_logging.py | 410 ++++----- .../tests/diagnostics/test_status_logger.py | 272 +++--- .../tests/exporter/test_exporter.py | 68 +- .../tests/test_constants.py | 342 ++++---- 11 files changed, 1111 insertions(+), 1113 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/dev_requirements.txt b/sdk/monitor/azure-monitor-opentelemetry-exporter/dev_requirements.txt index 86a165e4370a..39622ae3c9da 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/dev_requirements.txt +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/dev_requirements.txt @@ -2,5 +2,4 @@ -e ../../../tools/azure-sdk-tools ../../core/azure-core -e ../../identity/azure-identity -../azure-monitor-opentelemetry-exporter aiohttp>=3.0; python_version >= '3.7' diff --git a/sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt b/sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt index 32110e7dd2b7..b3ef301f96f8 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt +++ b/sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt @@ -1,5 +1,6 @@ -e ../../../tools/azure-sdk-tools -e ../../../tools/azure-devtools +../azure-monitor-opentelemetry-exporter pytest django fastapi diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 492d9f350bd0..82332be1c198 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,7 +88,6 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", - "opentelemetry-api~=1.20", "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", @@ -96,7 +95,6 @@ "opentelemetry-instrumentation-requests~=0.41b0", "opentelemetry-instrumentation-urllib~=0.41b0", "opentelemetry-instrumentation-urllib3~=0.41b0", - "opentelemetry-sdk~=1.20", ], entry_points={ "opentelemetry_distro": [ diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py index 0904bef3ee32..f6a41c7c3770 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py @@ -1,31 +1,31 @@ -# import warnings -# from unittest import TestCase -# from unittest.mock import patch +import warnings +from unittest import TestCase +from unittest.mock import patch -# from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan -# from azure.monitor.opentelemetry._autoinstrumentation.configurator import ( -# AzureMonitorConfigurator, -# ) +from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan +from azure.monitor.opentelemetry._autoinstrumentation.configurator import ( + AzureMonitorConfigurator, +) -# class TestConfigurator(TestCase): -# @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True) -# @patch( -# "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" -# ) -# def test_configure(self, mock_diagnostics, attach_mock): -# configurator = AzureMonitorConfigurator() -# with warnings.catch_warnings(): -# warnings.simplefilter("error") -# configurator._configure() -# mock_diagnostics.assert_called_once() +class TestConfigurator(TestCase): + @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True) + @patch( + "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" + ) + def test_configure(self, mock_diagnostics, attach_mock): + configurator = AzureMonitorConfigurator() + with warnings.catch_warnings(): + warnings.simplefilter("error") + configurator._configure() + mock_diagnostics.assert_called_once() -# @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=False) -# @patch( -# "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" -# ) -# def test_configure_preview(self, mock_diagnostics, attach_mock): -# configurator = AzureMonitorConfigurator() -# with self.assertWarns(Warning): -# configurator._configure() -# mock_diagnostics.assert_called_once() + @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=False) + @patch( + "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" + ) + def test_configure_preview(self, mock_diagnostics, attach_mock): + configurator = AzureMonitorConfigurator() + with self.assertWarns(Warning): + configurator._configure() + mock_diagnostics.assert_called_once() diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py index 3cf745396f36..36a5be11ae5a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py @@ -1,39 +1,39 @@ -# import warnings -# from unittest import TestCase -# from unittest.mock import patch +import warnings +from unittest import TestCase +from unittest.mock import patch -# from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan -# from azure.monitor.opentelemetry._autoinstrumentation.distro import ( -# AzureMonitorDistro, -# ) +from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan +from azure.monitor.opentelemetry._autoinstrumentation.distro import ( + AzureMonitorDistro, +) -# class TestDistro(TestCase): -# @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=True) -# @patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings") -# @patch( -# "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging.enable" -# ) -# def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock): -# distro = AzureMonitorDistro() -# with warnings.catch_warnings(): -# warnings.simplefilter("error") -# distro.configure() -# self.assertEqual(mock_diagnostics.call_count, 2) -# self.assertEqual( -# azure_core_mock.tracing_implementation, OpenTelemetrySpan -# ) +class TestDistro(TestCase): + @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=True) + @patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings") + @patch( + "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging.enable" + ) + def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock): + distro = AzureMonitorDistro() + with warnings.catch_warnings(): + warnings.simplefilter("error") + distro.configure() + self.assertEqual(mock_diagnostics.call_count, 2) + self.assertEqual( + azure_core_mock.tracing_implementation, OpenTelemetrySpan + ) -# @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=False) -# @patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings") -# @patch( -# "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging.enable" -# ) -# def test_configure_preview(self, mock_diagnostics, azure_core_mock, attach_mock): -# distro = AzureMonitorDistro() -# with self.assertWarns(Warning): -# distro.configure() -# self.assertEqual(mock_diagnostics.call_count, 2) -# self.assertEqual( -# azure_core_mock.tracing_implementation, OpenTelemetrySpan -# ) + @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=False) + @patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings") + @patch( + "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging.enable" + ) + def test_configure_preview(self, mock_diagnostics, azure_core_mock, attach_mock): + distro = AzureMonitorDistro() + with self.assertWarns(Warning): + distro.configure() + self.assertEqual(mock_diagnostics.call_count, 2) + self.assertEqual( + azure_core_mock.tracing_implementation, OpenTelemetrySpan + ) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py index 9b55303617b7..446ce56b10df 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py @@ -1,425 +1,425 @@ -# # Copyright The OpenTelemetry Authors -# # -# # Licensed under the Apache License, Version 2.0 (the "License"); -# # you may not use this file except in compliance with the License. -# # You may obtain a copy of the License at -# # -# # http://www.apache.org/licenses/LICENSE-2.0 -# # -# # Unless required by applicable law or agreed to in writing, software -# # distributed under the License is distributed on an "AS IS" BASIS, -# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# # See the License for the specific language governing permissions and -# # limitations under the License. +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -# import unittest -# from unittest.mock import Mock, patch +import unittest +from unittest.mock import Mock, patch -# from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan -# from azure.monitor.opentelemetry._configure import ( -# _SUPPORTED_INSTRUMENTED_LIBRARIES, -# _setup_instrumentations, -# _setup_logging, -# _setup_metrics, -# _setup_tracing, -# configure_azure_monitor, -# ) +from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan +from azure.monitor.opentelemetry._configure import ( + _SUPPORTED_INSTRUMENTED_LIBRARIES, + _setup_instrumentations, + _setup_logging, + _setup_metrics, + _setup_tracing, + configure_azure_monitor, +) -# class TestConfigure(unittest.TestCase): -# @patch( -# "azure.monitor.opentelemetry._configure._setup_instrumentations", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_metrics", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_logging", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_tracing", -# ) -# def test_configure_azure_monitor( -# self, -# tracing_mock, -# logging_mock, -# metrics_mock, -# instrumentation_mock, -# ): -# kwargs = { -# "connection_string": "test_cs", -# } -# configure_azure_monitor(**kwargs) -# tracing_mock.assert_called_once() -# logging_mock.assert_called_once() -# metrics_mock.assert_called_once() -# instrumentation_mock.assert_called_once() +class TestConfigure(unittest.TestCase): + @patch( + "azure.monitor.opentelemetry._configure._setup_instrumentations", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_metrics", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_logging", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_tracing", + ) + def test_configure_azure_monitor( + self, + tracing_mock, + logging_mock, + metrics_mock, + instrumentation_mock, + ): + kwargs = { + "connection_string": "test_cs", + } + configure_azure_monitor(**kwargs) + tracing_mock.assert_called_once() + logging_mock.assert_called_once() + metrics_mock.assert_called_once() + instrumentation_mock.assert_called_once() -# @patch( -# "azure.monitor.opentelemetry._configure._setup_instrumentations", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_metrics", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_logging", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_tracing", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._get_configurations", -# ) -# def test_configure_azure_monitor_disable_tracing( -# self, -# config_mock, -# tracing_mock, -# logging_mock, -# metrics_mock, -# instrumentation_mock, -# ): -# configurations = { -# "connection_string": "test_cs", -# "disable_tracing": True, -# "disable_logging": False, -# "disable_metrics": False, -# } -# config_mock.return_value = configurations -# configure_azure_monitor() -# tracing_mock.assert_not_called() -# logging_mock.assert_called_once_with(configurations) -# metrics_mock.assert_called_once_with(configurations) -# instrumentation_mock.assert_called_once_with(configurations) + @patch( + "azure.monitor.opentelemetry._configure._setup_instrumentations", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_metrics", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_logging", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_tracing", + ) + @patch( + "azure.monitor.opentelemetry._configure._get_configurations", + ) + def test_configure_azure_monitor_disable_tracing( + self, + config_mock, + tracing_mock, + logging_mock, + metrics_mock, + instrumentation_mock, + ): + configurations = { + "connection_string": "test_cs", + "disable_tracing": True, + "disable_logging": False, + "disable_metrics": False, + } + config_mock.return_value = configurations + configure_azure_monitor() + tracing_mock.assert_not_called() + logging_mock.assert_called_once_with(configurations) + metrics_mock.assert_called_once_with(configurations) + instrumentation_mock.assert_called_once_with(configurations) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_instrumentations", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_metrics", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_logging", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_tracing", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._get_configurations", -# ) -# def test_configure_azure_monitor_disable_logging( -# self, -# config_mock, -# tracing_mock, -# logging_mock, -# metrics_mock, -# instrumentation_mock, -# ): -# configurations = { -# "connection_string": "test_cs", -# "disable_tracing": False, -# "disable_logging": True, -# "disable_metrics": False, -# } -# config_mock.return_value = configurations -# configure_azure_monitor() -# tracing_mock.assert_called_once_with(configurations) -# logging_mock.assert_not_called() -# metrics_mock.assert_called_once_with(configurations) -# instrumentation_mock.assert_called_once_with(configurations) + @patch( + "azure.monitor.opentelemetry._configure._setup_instrumentations", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_metrics", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_logging", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_tracing", + ) + @patch( + "azure.monitor.opentelemetry._configure._get_configurations", + ) + def test_configure_azure_monitor_disable_logging( + self, + config_mock, + tracing_mock, + logging_mock, + metrics_mock, + instrumentation_mock, + ): + configurations = { + "connection_string": "test_cs", + "disable_tracing": False, + "disable_logging": True, + "disable_metrics": False, + } + config_mock.return_value = configurations + configure_azure_monitor() + tracing_mock.assert_called_once_with(configurations) + logging_mock.assert_not_called() + metrics_mock.assert_called_once_with(configurations) + instrumentation_mock.assert_called_once_with(configurations) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_instrumentations", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_metrics", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_logging", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._setup_tracing", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure._get_configurations", -# ) -# def test_configure_azure_monitor_disable_metrics( -# self, -# config_mock, -# tracing_mock, -# logging_mock, -# metrics_mock, -# instrumentation_mock, -# ): -# configurations = { -# "connection_string": "test_cs", -# "disable_tracing": False, -# "disable_logging": False, -# "disable_metrics": True, -# } -# config_mock.return_value = configurations -# configure_azure_monitor() -# tracing_mock.assert_called_once_with(configurations) -# logging_mock.assert_called_once_with(configurations) -# metrics_mock.assert_not_called() -# instrumentation_mock.assert_called_once_with(configurations) + @patch( + "azure.monitor.opentelemetry._configure._setup_instrumentations", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_metrics", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_logging", + ) + @patch( + "azure.monitor.opentelemetry._configure._setup_tracing", + ) + @patch( + "azure.monitor.opentelemetry._configure._get_configurations", + ) + def test_configure_azure_monitor_disable_metrics( + self, + config_mock, + tracing_mock, + logging_mock, + metrics_mock, + instrumentation_mock, + ): + configurations = { + "connection_string": "test_cs", + "disable_tracing": False, + "disable_logging": False, + "disable_metrics": True, + } + config_mock.return_value = configurations + configure_azure_monitor() + tracing_mock.assert_called_once_with(configurations) + logging_mock.assert_called_once_with(configurations) + metrics_mock.assert_not_called() + instrumentation_mock.assert_called_once_with(configurations) -# @patch( -# "azure.monitor.opentelemetry._configure.settings", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.BatchSpanProcessor", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.AzureMonitorTraceExporter", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.get_tracer_provider", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.set_tracer_provider", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.TracerProvider", -# autospec=True, -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.ApplicationInsightsSampler", -# ) -# def test_setup_tracing( -# self, -# sampler_mock, -# tp_mock, -# set_tracer_provider_mock, -# get_tracer_provider_mock, -# trace_exporter_mock, -# bsp_mock, -# azure_core_mock, -# ): -# sampler_init_mock = Mock() -# sampler_mock.return_value = sampler_init_mock -# tp_init_mock = Mock() -# tp_mock.return_value = tp_init_mock -# get_tracer_provider_mock.return_value = tp_init_mock -# trace_exp_init_mock = Mock() -# trace_exporter_mock.return_value = trace_exp_init_mock -# bsp_init_mock = Mock() -# bsp_mock.return_value = bsp_init_mock + @patch( + "azure.monitor.opentelemetry._configure.settings", + ) + @patch( + "azure.monitor.opentelemetry._configure.BatchSpanProcessor", + ) + @patch( + "azure.monitor.opentelemetry._configure.AzureMonitorTraceExporter", + ) + @patch( + "azure.monitor.opentelemetry._configure.get_tracer_provider", + ) + @patch( + "azure.monitor.opentelemetry._configure.set_tracer_provider", + ) + @patch( + "azure.monitor.opentelemetry._configure.TracerProvider", + autospec=True, + ) + @patch( + "azure.monitor.opentelemetry._configure.ApplicationInsightsSampler", + ) + def test_setup_tracing( + self, + sampler_mock, + tp_mock, + set_tracer_provider_mock, + get_tracer_provider_mock, + trace_exporter_mock, + bsp_mock, + azure_core_mock, + ): + sampler_init_mock = Mock() + sampler_mock.return_value = sampler_init_mock + tp_init_mock = Mock() + tp_mock.return_value = tp_init_mock + get_tracer_provider_mock.return_value = tp_init_mock + trace_exp_init_mock = Mock() + trace_exporter_mock.return_value = trace_exp_init_mock + bsp_init_mock = Mock() + bsp_mock.return_value = bsp_init_mock -# configurations = { -# "connection_string": "test_cs", -# "disable_azure_core_tracing": False, -# "sampling_ratio": 0.5, -# } -# _setup_tracing(configurations) -# sampler_mock.assert_called_once_with(sampling_ratio=0.5) -# tp_mock.assert_called_once_with( -# sampler=sampler_init_mock, -# ) -# set_tracer_provider_mock.assert_called_once_with(tp_init_mock) -# get_tracer_provider_mock.assert_called() -# trace_exporter_mock.assert_called_once_with(**configurations) -# bsp_mock.assert_called_once_with(trace_exp_init_mock) -# tp_init_mock.add_span_processor.assert_called_once_with(bsp_init_mock) -# self.assertEqual( -# azure_core_mock.tracing_implementation, OpenTelemetrySpan -# ) + configurations = { + "connection_string": "test_cs", + "disable_azure_core_tracing": False, + "sampling_ratio": 0.5, + } + _setup_tracing(configurations) + sampler_mock.assert_called_once_with(sampling_ratio=0.5) + tp_mock.assert_called_once_with( + sampler=sampler_init_mock, + ) + set_tracer_provider_mock.assert_called_once_with(tp_init_mock) + get_tracer_provider_mock.assert_called() + trace_exporter_mock.assert_called_once_with(**configurations) + bsp_mock.assert_called_once_with(trace_exp_init_mock) + tp_init_mock.add_span_processor.assert_called_once_with(bsp_init_mock) + self.assertEqual( + azure_core_mock.tracing_implementation, OpenTelemetrySpan + ) -# @patch( -# "azure.monitor.opentelemetry._configure.getLogger", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.LoggingHandler", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.BatchLogRecordProcessor", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.AzureMonitorLogExporter", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.get_logger_provider", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.set_logger_provider", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.LoggerProvider", -# autospec=True, -# ) -# def test_setup_logging( -# self, -# lp_mock, -# set_logger_provider_mock, -# get_logger_provider_mock, -# log_exporter_mock, -# blrp_mock, -# logging_handler_mock, -# get_logger_mock, -# ): -# lp_init_mock = Mock() -# lp_mock.return_value = lp_init_mock -# get_logger_provider_mock.return_value = lp_init_mock -# log_exp_init_mock = Mock() -# log_exporter_mock.return_value = log_exp_init_mock -# blrp_init_mock = Mock() -# blrp_mock.return_value = blrp_init_mock -# logging_handler_init_mock = Mock() -# logging_handler_mock.return_value = logging_handler_init_mock -# logger_mock = Mock() -# get_logger_mock.return_value = logger_mock + @patch( + "azure.monitor.opentelemetry._configure.getLogger", + ) + @patch( + "azure.monitor.opentelemetry._configure.LoggingHandler", + ) + @patch( + "azure.monitor.opentelemetry._configure.BatchLogRecordProcessor", + ) + @patch( + "azure.monitor.opentelemetry._configure.AzureMonitorLogExporter", + ) + @patch( + "azure.monitor.opentelemetry._configure.get_logger_provider", + ) + @patch( + "azure.monitor.opentelemetry._configure.set_logger_provider", + ) + @patch( + "azure.monitor.opentelemetry._configure.LoggerProvider", + autospec=True, + ) + def test_setup_logging( + self, + lp_mock, + set_logger_provider_mock, + get_logger_provider_mock, + log_exporter_mock, + blrp_mock, + logging_handler_mock, + get_logger_mock, + ): + lp_init_mock = Mock() + lp_mock.return_value = lp_init_mock + get_logger_provider_mock.return_value = lp_init_mock + log_exp_init_mock = Mock() + log_exporter_mock.return_value = log_exp_init_mock + blrp_init_mock = Mock() + blrp_mock.return_value = blrp_init_mock + logging_handler_init_mock = Mock() + logging_handler_mock.return_value = logging_handler_init_mock + logger_mock = Mock() + get_logger_mock.return_value = logger_mock -# configurations = { -# "connection_string": "test_cs", -# } -# _setup_logging(configurations) + configurations = { + "connection_string": "test_cs", + } + _setup_logging(configurations) -# lp_mock.assert_called_once_with() -# set_logger_provider_mock.assert_called_once_with(lp_init_mock) -# get_logger_provider_mock.assert_called() -# log_exporter_mock.assert_called_once_with(**configurations) -# blrp_mock.assert_called_once_with( -# log_exp_init_mock, -# ) -# lp_init_mock.add_log_record_processor.assert_called_once_with( -# blrp_init_mock -# ) -# logging_handler_mock.assert_called_once_with( -# logger_provider=lp_init_mock -# ) -# get_logger_mock.assert_called_once_with() -# logger_mock.addHandler.assert_called_once_with( -# logging_handler_init_mock -# ) + lp_mock.assert_called_once_with() + set_logger_provider_mock.assert_called_once_with(lp_init_mock) + get_logger_provider_mock.assert_called() + log_exporter_mock.assert_called_once_with(**configurations) + blrp_mock.assert_called_once_with( + log_exp_init_mock, + ) + lp_init_mock.add_log_record_processor.assert_called_once_with( + blrp_init_mock + ) + logging_handler_mock.assert_called_once_with( + logger_provider=lp_init_mock + ) + get_logger_mock.assert_called_once_with() + logger_mock.addHandler.assert_called_once_with( + logging_handler_init_mock + ) -# @patch( -# "azure.monitor.opentelemetry._configure.PeriodicExportingMetricReader", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.AzureMonitorMetricExporter", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.set_meter_provider", -# ) -# @patch( -# "azure.monitor.opentelemetry._configure.MeterProvider", -# autospec=True, -# ) -# def test_setup_metrics( -# self, -# mp_mock, -# set_meter_provider_mock, -# metric_exporter_mock, -# reader_mock, -# ): -# mp_init_mock = Mock() -# mp_mock.return_value = mp_init_mock -# metric_exp_init_mock = Mock() -# metric_exporter_mock.return_value = metric_exp_init_mock -# reader_init_mock = Mock() -# reader_mock.return_value = reader_init_mock + @patch( + "azure.monitor.opentelemetry._configure.PeriodicExportingMetricReader", + ) + @patch( + "azure.monitor.opentelemetry._configure.AzureMonitorMetricExporter", + ) + @patch( + "azure.monitor.opentelemetry._configure.set_meter_provider", + ) + @patch( + "azure.monitor.opentelemetry._configure.MeterProvider", + autospec=True, + ) + def test_setup_metrics( + self, + mp_mock, + set_meter_provider_mock, + metric_exporter_mock, + reader_mock, + ): + mp_init_mock = Mock() + mp_mock.return_value = mp_init_mock + metric_exp_init_mock = Mock() + metric_exporter_mock.return_value = metric_exp_init_mock + reader_init_mock = Mock() + reader_mock.return_value = reader_init_mock -# configurations = { -# "connection_string": "test_cs", -# } -# _setup_metrics(configurations) -# mp_mock.assert_called_once_with( -# metric_readers=[reader_init_mock], -# ) -# set_meter_provider_mock.assert_called_once_with(mp_init_mock) -# metric_exporter_mock.assert_called_once_with(**configurations) -# reader_mock.assert_called_once_with(metric_exp_init_mock) + configurations = { + "connection_string": "test_cs", + } + _setup_metrics(configurations) + mp_mock.assert_called_once_with( + metric_readers=[reader_init_mock], + ) + set_meter_provider_mock.assert_called_once_with(mp_init_mock) + metric_exporter_mock.assert_called_once_with(**configurations) + reader_mock.assert_called_once_with(metric_exp_init_mock) -# @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") -# @patch("azure.monitor.opentelemetry._configure.iter_entry_points") -# def test_setup_instrumentations_lib_not_supported( -# self, -# iter_mock, -# dep_mock, -# ): -# ep_mock = Mock() -# ep2_mock = Mock() -# iter_mock.return_value = (ep_mock, ep2_mock) -# instrumentor_mock = Mock() -# instr_class_mock = Mock() -# instr_class_mock.return_value = instrumentor_mock -# ep_mock.name = "test_instr" -# ep2_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] -# ep2_mock.load.return_value = instr_class_mock -# dep_mock.return_value = None -# _setup_instrumentations({"disabled_instrumentations": []}) -# dep_mock.assert_called_with(ep2_mock.dist) -# ep_mock.load.assert_not_called() -# ep2_mock.load.assert_called_once() -# instrumentor_mock.instrument.assert_called_once() + @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") + @patch("azure.monitor.opentelemetry._configure.iter_entry_points") + def test_setup_instrumentations_lib_not_supported( + self, + iter_mock, + dep_mock, + ): + ep_mock = Mock() + ep2_mock = Mock() + iter_mock.return_value = (ep_mock, ep2_mock) + instrumentor_mock = Mock() + instr_class_mock = Mock() + instr_class_mock.return_value = instrumentor_mock + ep_mock.name = "test_instr" + ep2_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] + ep2_mock.load.return_value = instr_class_mock + dep_mock.return_value = None + _setup_instrumentations({"disabled_instrumentations": []}) + dep_mock.assert_called_with(ep2_mock.dist) + ep_mock.load.assert_not_called() + ep2_mock.load.assert_called_once() + instrumentor_mock.instrument.assert_called_once() -# @patch("azure.monitor.opentelemetry._configure._logger") -# @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") -# @patch("azure.monitor.opentelemetry._configure.iter_entry_points") -# def test_setup_instrumentations_conflict( -# self, -# iter_mock, -# dep_mock, -# logger_mock, -# ): -# ep_mock = Mock() -# iter_mock.return_value = (ep_mock,) -# instrumentor_mock = Mock() -# instr_class_mock = Mock() -# instr_class_mock.return_value = instrumentor_mock -# ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] -# ep_mock.load.return_value = instr_class_mock -# dep_mock.return_value = True -# _setup_instrumentations({"disabled_instrumentations": []}) -# dep_mock.assert_called_with(ep_mock.dist) -# ep_mock.load.assert_not_called() -# instrumentor_mock.instrument.assert_not_called() -# logger_mock.debug.assert_called_once() + @patch("azure.monitor.opentelemetry._configure._logger") + @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") + @patch("azure.monitor.opentelemetry._configure.iter_entry_points") + def test_setup_instrumentations_conflict( + self, + iter_mock, + dep_mock, + logger_mock, + ): + ep_mock = Mock() + iter_mock.return_value = (ep_mock,) + instrumentor_mock = Mock() + instr_class_mock = Mock() + instr_class_mock.return_value = instrumentor_mock + ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] + ep_mock.load.return_value = instr_class_mock + dep_mock.return_value = True + _setup_instrumentations({"disabled_instrumentations": []}) + dep_mock.assert_called_with(ep_mock.dist) + ep_mock.load.assert_not_called() + instrumentor_mock.instrument.assert_not_called() + logger_mock.debug.assert_called_once() -# @patch("azure.monitor.opentelemetry._configure._logger") -# @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") -# @patch("azure.monitor.opentelemetry._configure.iter_entry_points") -# def test_setup_instrumentations_exception( -# self, -# iter_mock, -# dep_mock, -# logger_mock, -# ): -# ep_mock = Mock() -# iter_mock.return_value = (ep_mock,) -# instrumentor_mock = Mock() -# instr_class_mock = Mock() -# instr_class_mock.return_value = instrumentor_mock -# ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] -# ep_mock.load.side_effect = Exception() -# dep_mock.return_value = None -# _setup_instrumentations({"disabled_instrumentations": []}) -# dep_mock.assert_called_with(ep_mock.dist) -# ep_mock.load.assert_called_once() -# instrumentor_mock.instrument.assert_not_called() -# logger_mock.warning.assert_called_once() + @patch("azure.monitor.opentelemetry._configure._logger") + @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") + @patch("azure.monitor.opentelemetry._configure.iter_entry_points") + def test_setup_instrumentations_exception( + self, + iter_mock, + dep_mock, + logger_mock, + ): + ep_mock = Mock() + iter_mock.return_value = (ep_mock,) + instrumentor_mock = Mock() + instr_class_mock = Mock() + instr_class_mock.return_value = instrumentor_mock + ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] + ep_mock.load.side_effect = Exception() + dep_mock.return_value = None + _setup_instrumentations({"disabled_instrumentations": []}) + dep_mock.assert_called_with(ep_mock.dist) + ep_mock.load.assert_called_once() + instrumentor_mock.instrument.assert_not_called() + logger_mock.warning.assert_called_once() -# @patch("azure.monitor.opentelemetry._configure._logger") -# @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") -# @patch("azure.monitor.opentelemetry._configure.iter_entry_points") -# def test_setup_instrumentations_disabled( -# self, -# iter_mock, -# dep_mock, -# logger_mock, -# ): -# ep_mock = Mock() -# ep2_mock = Mock() -# iter_mock.return_value = (ep_mock, ep2_mock) -# instrumentor_mock = Mock() -# instr_class_mock = Mock() -# instr_class_mock.return_value = instrumentor_mock -# ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] -# ep2_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[1] -# ep2_mock.load.return_value = instr_class_mock -# dep_mock.return_value = None -# _setup_instrumentations({"disabled_instrumentations": [ep_mock.name]}) -# dep_mock.assert_called_with(ep2_mock.dist) -# ep_mock.load.assert_not_called() -# ep2_mock.load.assert_called_once() -# instrumentor_mock.instrument.assert_called_once() -# logger_mock.debug.assert_called_once() + @patch("azure.monitor.opentelemetry._configure._logger") + @patch("azure.monitor.opentelemetry._configure.get_dist_dependency_conflicts") + @patch("azure.monitor.opentelemetry._configure.iter_entry_points") + def test_setup_instrumentations_disabled( + self, + iter_mock, + dep_mock, + logger_mock, + ): + ep_mock = Mock() + ep2_mock = Mock() + iter_mock.return_value = (ep_mock, ep2_mock) + instrumentor_mock = Mock() + instr_class_mock = Mock() + instr_class_mock.return_value = instrumentor_mock + ep_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[0] + ep2_mock.name = _SUPPORTED_INSTRUMENTED_LIBRARIES[1] + ep2_mock.load.return_value = instr_class_mock + dep_mock.return_value = None + _setup_instrumentations({"disabled_instrumentations": [ep_mock.name]}) + dep_mock.assert_called_with(ep2_mock.dist) + ep_mock.load.assert_not_called() + ep2_mock.load.assert_called_once() + instrumentor_mock.instrument.assert_called_once() + logger_mock.debug.assert_called_once() diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py index 746ca023cf20..6ddba50805c6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_util.py @@ -1,106 +1,106 @@ -# # Copyright The OpenTelemetry Authors -# # -# # Licensed under the Apache License, Version 2.0 (the "License"); -# # you may not use this file except in compliance with the License. -# # You may obtain a copy of the License at -# # -# # http://www.apache.org/licenses/LICENSE-2.0 -# # -# # Unless required by applicable law or agreed to in writing, software -# # distributed under the License is distributed on an "AS IS" BASIS, -# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# # See the License for the specific language governing permissions and -# # limitations under the License. +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -# from unittest import TestCase -# from unittest.mock import patch +from unittest import TestCase +from unittest.mock import patch -# from opentelemetry.instrumentation.environment_variables import ( -# OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, -# ) -# from azure.monitor.opentelemetry._util.configurations import ( -# SAMPLING_RATIO_ENV_VAR, -# _get_configurations, -# ) -# from opentelemetry.environment_variables import ( -# OTEL_LOGS_EXPORTER, -# OTEL_METRICS_EXPORTER, -# OTEL_TRACES_EXPORTER, -# ) +from opentelemetry.instrumentation.environment_variables import ( + OTEL_PYTHON_DISABLED_INSTRUMENTATIONS, +) +from azure.monitor.opentelemetry._util.configurations import ( + SAMPLING_RATIO_ENV_VAR, + _get_configurations, +) +from opentelemetry.environment_variables import ( + OTEL_LOGS_EXPORTER, + OTEL_METRICS_EXPORTER, + OTEL_TRACES_EXPORTER, +) -# class TestUtil(TestCase): -# def test_get_configurations(self): -# configurations = _get_configurations( -# connection_string="test_cs", -# credential="test_credential", -# ) +class TestUtil(TestCase): + def test_get_configurations(self): + configurations = _get_configurations( + connection_string="test_cs", + credential="test_credential", + ) -# self.assertEqual(configurations["connection_string"], "test_cs") -# self.assertEqual(configurations["disable_azure_core_tracing"], False) -# self.assertEqual(configurations["disable_logging"], False) -# self.assertEqual(configurations["disable_metrics"], False) -# self.assertEqual(configurations["disable_tracing"], False) -# self.assertEqual(configurations["disabled_instrumentations"], []) -# self.assertEqual(configurations["sampling_ratio"], 1.0) -# self.assertEqual(configurations["credential"], ("test_credential")) -# self.assertTrue("storage_directory" not in configurations) + self.assertEqual(configurations["connection_string"], "test_cs") + self.assertEqual(configurations["disable_azure_core_tracing"], False) + self.assertEqual(configurations["disable_logging"], False) + self.assertEqual(configurations["disable_metrics"], False) + self.assertEqual(configurations["disable_tracing"], False) + self.assertEqual(configurations["disabled_instrumentations"], []) + self.assertEqual(configurations["sampling_ratio"], 1.0) + self.assertEqual(configurations["credential"], ("test_credential")) + self.assertTrue("storage_directory" not in configurations) -# @patch.dict("os.environ", {}, clear=True) -# def test_get_configurations_defaults(self): -# configurations = _get_configurations() + @patch.dict("os.environ", {}, clear=True) + def test_get_configurations_defaults(self): + configurations = _get_configurations() -# self.assertTrue("connection_string" not in configurations) -# self.assertEqual(configurations["disable_azure_core_tracing"], False) -# self.assertEqual(configurations["disable_logging"], False) -# self.assertEqual(configurations["disable_metrics"], False) -# self.assertEqual(configurations["disable_tracing"], False) -# self.assertEqual(configurations["disabled_instrumentations"], []) -# self.assertEqual(configurations["sampling_ratio"], 1.0) -# self.assertTrue("credential" not in configurations) -# self.assertTrue("storage_directory" not in configurations) + self.assertTrue("connection_string" not in configurations) + self.assertEqual(configurations["disable_azure_core_tracing"], False) + self.assertEqual(configurations["disable_logging"], False) + self.assertEqual(configurations["disable_metrics"], False) + self.assertEqual(configurations["disable_tracing"], False) + self.assertEqual(configurations["disabled_instrumentations"], []) + self.assertEqual(configurations["sampling_ratio"], 1.0) + self.assertTrue("credential" not in configurations) + self.assertTrue("storage_directory" not in configurations) -# @patch.dict( -# "os.environ", -# { -# OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: "flask , requests,fastapi", -# SAMPLING_RATIO_ENV_VAR: "0.5", -# OTEL_TRACES_EXPORTER: "None", -# OTEL_LOGS_EXPORTER: "none", -# OTEL_METRICS_EXPORTER: "NONE", -# }, -# clear=True, -# ) -# def test_get_configurations_env_vars(self): -# configurations = _get_configurations() + @patch.dict( + "os.environ", + { + OTEL_PYTHON_DISABLED_INSTRUMENTATIONS: "flask , requests,fastapi", + SAMPLING_RATIO_ENV_VAR: "0.5", + OTEL_TRACES_EXPORTER: "None", + OTEL_LOGS_EXPORTER: "none", + OTEL_METRICS_EXPORTER: "NONE", + }, + clear=True, + ) + def test_get_configurations_env_vars(self): + configurations = _get_configurations() -# self.assertTrue("connection_string" not in configurations) -# self.assertEqual(configurations["disable_azure_core_tracing"], False) -# self.assertEqual(configurations["disable_logging"], True) -# self.assertEqual(configurations["disable_metrics"], True) -# self.assertEqual(configurations["disable_tracing"], True) -# self.assertEqual( -# configurations["disabled_instrumentations"], -# ["flask", "requests", "fastapi"], -# ) -# self.assertEqual(configurations["sampling_ratio"], 0.5) + self.assertTrue("connection_string" not in configurations) + self.assertEqual(configurations["disable_azure_core_tracing"], False) + self.assertEqual(configurations["disable_logging"], True) + self.assertEqual(configurations["disable_metrics"], True) + self.assertEqual(configurations["disable_tracing"], True) + self.assertEqual( + configurations["disabled_instrumentations"], + ["flask", "requests", "fastapi"], + ) + self.assertEqual(configurations["sampling_ratio"], 0.5) -# @patch.dict( -# "os.environ", -# { -# SAMPLING_RATIO_ENV_VAR: "Half", -# OTEL_TRACES_EXPORTER: "False", -# OTEL_LOGS_EXPORTER: "no", -# OTEL_METRICS_EXPORTER: "True", -# }, -# clear=True, -# ) -# def test_get_configurations_env_vars_validation(self): -# configurations = _get_configurations() + @patch.dict( + "os.environ", + { + SAMPLING_RATIO_ENV_VAR: "Half", + OTEL_TRACES_EXPORTER: "False", + OTEL_LOGS_EXPORTER: "no", + OTEL_METRICS_EXPORTER: "True", + }, + clear=True, + ) + def test_get_configurations_env_vars_validation(self): + configurations = _get_configurations() -# self.assertTrue("connection_string" not in configurations) -# self.assertEqual(configurations["disable_azure_core_tracing"], False) -# self.assertEqual(configurations["disable_logging"], False) -# self.assertEqual(configurations["disable_metrics"], False) -# self.assertEqual(configurations["disable_tracing"], False) -# self.assertEqual(configurations["sampling_ratio"], 1.0) + self.assertTrue("connection_string" not in configurations) + self.assertEqual(configurations["disable_azure_core_tracing"], False) + self.assertEqual(configurations["disable_logging"], False) + self.assertEqual(configurations["disable_metrics"], False) + self.assertEqual(configurations["disable_tracing"], False) + self.assertEqual(configurations["sampling_ratio"], 1.0) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_diagnostic_logging.py b/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_diagnostic_logging.py index 502721ccaa88..0d956088d09b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_diagnostic_logging.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_diagnostic_logging.py @@ -1,205 +1,205 @@ -# # ------------------------------------------------------------------------- -# # Copyright (c) Microsoft Corporation. All rights reserved. -# # Licensed under the MIT License. See License in the project root for -# # license information. -# # -------------------------------------------------------------------------- - -# import logging -# import os -# from importlib import reload -# from json import loads -# from unittest.mock import patch - -# import azure.monitor.opentelemetry._diagnostics.diagnostic_logging as diagnostic_logger - - -# TEST_SITE_NAME = "TEST_SITE_NAME" -# TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY" -# TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION" -# TEST_VERSION = "TEST_VERSION" -# TEST_SUBSCRIPTION_ID_ENV_VAR = "TEST_SUBSCRIPTION_ID+TEST_SUBSCRIPTION_ID" -# TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID" -# MESSAGE1 = "MESSAGE1" -# MESSAGE2 = "MESSAGE2" -# MESSAGE3 = "MESSAGE3" -# TEST_LOGGER_NAME = "test.logger.name" -# TEST_LOGGER = logging.getLogger(TEST_LOGGER_NAME) -# TEST_LOGGER_NAME_SUB_MODULE = TEST_LOGGER_NAME + ".sub.module" -# TEST_LOGGER_SUB_MODULE = logging.getLogger(TEST_LOGGER_NAME_SUB_MODULE) - - -# def clear_file(file_path): -# with open(file_path, "w") as f: -# f.seek(0) -# f.truncate() - - -# def check_file_for_messages(file_path, level, messages, logger_name=TEST_LOGGER_NAME_SUB_MODULE): -# with open(file_path, "r") as f: -# f.seek(0) -# for message in messages: -# json = loads(f.readline()) -# assert json["time"] -# assert json["level"] == level -# assert json["logger"] == logger_name -# assert json["message"] == message -# properties = json["properties"] -# assert properties["operation"] == "Startup" -# assert properties["sitename"] == TEST_SITE_NAME -# assert properties["ikey"] == TEST_CUSTOMER_IKEY -# assert properties["extensionVersion"] == TEST_EXTENSION_VERSION -# assert properties["sdkVersion"] == TEST_VERSION -# assert properties["subscriptionId"] == TEST_SUBSCRIPTION_ID -# assert not f.read() - - -# def check_file_is_empty(file_path): -# with open(file_path, "r") as f: -# f.seek(0) -# assert not f.read() - - -# def set_up( -# file_path, -# is_diagnostics_enabled, -# logger=TEST_LOGGER, -# subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, -# ) -> None: -# diagnostic_logger._logger.handlers.clear() -# logger.handlers.clear() -# TEST_LOGGER.handlers.clear() -# TEST_LOGGER_SUB_MODULE.handlers.clear() -# TEST_LOGGER_SUB_MODULE.setLevel(logging.WARN) -# patch.dict( -# "os.environ", -# { -# "WEBSITE_SITE_NAME": TEST_SITE_NAME, -# "WEBSITE_OWNER_NAME": subscription_id_env_var, -# }, -# ).start() -# reload(diagnostic_logger) -# assert not diagnostic_logger.AzureDiagnosticLogging._initialized -# patch( -# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._DIAGNOSTIC_LOG_PATH", -# os.path.dirname(file_path), -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._DIAGNOSTIC_LOGGER_FILE_NAME", -# os.path.basename(file_path), -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._get_customer_ikey_from_env_var", -# return_value=TEST_CUSTOMER_IKEY, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._EXTENSION_VERSION", -# TEST_EXTENSION_VERSION, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging.VERSION", -# TEST_VERSION, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._IS_DIAGNOSTICS_ENABLED", -# is_diagnostics_enabled, -# ).start() -# diagnostic_logger.AzureDiagnosticLogging.enable(logger) - - -# class TestDiagnosticLogger: - -# def test_initialized(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# assert diagnostic_logger.AzureDiagnosticLogging._initialized is True - -# def test_uninitialized(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=False) -# assert diagnostic_logger.AzureDiagnosticLogging._initialized is False - -# def test_info(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# TEST_LOGGER_SUB_MODULE.info(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.info(MESSAGE2) -# check_file_is_empty(temp_file_path) - -# def test_info_with_info_log_level(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# TEST_LOGGER_SUB_MODULE.setLevel(logging.INFO) -# TEST_LOGGER_SUB_MODULE.info(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.info(MESSAGE2) -# TEST_LOGGER_SUB_MODULE.setLevel(logging.NOTSET) -# check_file_for_messages(temp_file_path, "INFO", (MESSAGE1, MESSAGE2)) - -# def test_info_with_sub_module_info_log_level(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# TEST_LOGGER_SUB_MODULE.setLevel(logging.INFO) -# TEST_LOGGER_SUB_MODULE.info(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.info(MESSAGE2) -# TEST_LOGGER_SUB_MODULE.setLevel(logging.NOTSET) -# check_file_for_messages(temp_file_path, "INFO", (MESSAGE1, MESSAGE2)) - -# def test_warning(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) -# check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) - -# def test_warning_multiple_enable(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER) -# diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER) -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) -# check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) - -# def test_error(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# TEST_LOGGER_SUB_MODULE.error(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.error(MESSAGE2) -# check_file_for_messages(temp_file_path, "ERROR", (MESSAGE1, MESSAGE2)) - -# def test_off_app_service_info(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=False) -# TEST_LOGGER.info(MESSAGE1) -# TEST_LOGGER.info(MESSAGE2) -# TEST_LOGGER_SUB_MODULE.info(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.info(MESSAGE2) -# check_file_is_empty(temp_file_path) - -# def test_off_app_service_warning(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=False) -# TEST_LOGGER.warning(MESSAGE1) -# TEST_LOGGER.warning(MESSAGE2) -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) -# check_file_is_empty(temp_file_path) - -# def test_off_app_service_error(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=False) -# TEST_LOGGER.error(MESSAGE1) -# TEST_LOGGER.error(MESSAGE2) -# TEST_LOGGER_SUB_MODULE.error(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.error(MESSAGE2) -# check_file_is_empty(temp_file_path) - -# def test_subscription_id_plus(self, temp_file_path): -# set_up( -# temp_file_path, -# is_diagnostics_enabled=True, -# subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, -# ) -# assert diagnostic_logger._SUBSCRIPTION_ID == TEST_SUBSCRIPTION_ID -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) -# check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) - -# def test_subscription_id_no_plus(self, temp_file_path): -# set_up( -# temp_file_path, -# is_diagnostics_enabled=True, -# subscription_id_env_var=TEST_SUBSCRIPTION_ID, -# ) -# assert diagnostic_logger._SUBSCRIPTION_ID == TEST_SUBSCRIPTION_ID -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) -# TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) -# check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +import logging +import os +from importlib import reload +from json import loads +from unittest.mock import patch + +import azure.monitor.opentelemetry._diagnostics.diagnostic_logging as diagnostic_logger + + +TEST_SITE_NAME = "TEST_SITE_NAME" +TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY" +TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION" +TEST_VERSION = "TEST_VERSION" +TEST_SUBSCRIPTION_ID_ENV_VAR = "TEST_SUBSCRIPTION_ID+TEST_SUBSCRIPTION_ID" +TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID" +MESSAGE1 = "MESSAGE1" +MESSAGE2 = "MESSAGE2" +MESSAGE3 = "MESSAGE3" +TEST_LOGGER_NAME = "test.logger.name" +TEST_LOGGER = logging.getLogger(TEST_LOGGER_NAME) +TEST_LOGGER_NAME_SUB_MODULE = TEST_LOGGER_NAME + ".sub.module" +TEST_LOGGER_SUB_MODULE = logging.getLogger(TEST_LOGGER_NAME_SUB_MODULE) + + +def clear_file(file_path): + with open(file_path, "w") as f: + f.seek(0) + f.truncate() + + +def check_file_for_messages(file_path, level, messages, logger_name=TEST_LOGGER_NAME_SUB_MODULE): + with open(file_path, "r") as f: + f.seek(0) + for message in messages: + json = loads(f.readline()) + assert json["time"] + assert json["level"] == level + assert json["logger"] == logger_name + assert json["message"] == message + properties = json["properties"] + assert properties["operation"] == "Startup" + assert properties["sitename"] == TEST_SITE_NAME + assert properties["ikey"] == TEST_CUSTOMER_IKEY + assert properties["extensionVersion"] == TEST_EXTENSION_VERSION + assert properties["sdkVersion"] == TEST_VERSION + assert properties["subscriptionId"] == TEST_SUBSCRIPTION_ID + assert not f.read() + + +def check_file_is_empty(file_path): + with open(file_path, "r") as f: + f.seek(0) + assert not f.read() + + +def set_up( + file_path, + is_diagnostics_enabled, + logger=TEST_LOGGER, + subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, +) -> None: + diagnostic_logger._logger.handlers.clear() + logger.handlers.clear() + TEST_LOGGER.handlers.clear() + TEST_LOGGER_SUB_MODULE.handlers.clear() + TEST_LOGGER_SUB_MODULE.setLevel(logging.WARN) + patch.dict( + "os.environ", + { + "WEBSITE_SITE_NAME": TEST_SITE_NAME, + "WEBSITE_OWNER_NAME": subscription_id_env_var, + }, + ).start() + reload(diagnostic_logger) + assert not diagnostic_logger.AzureDiagnosticLogging._initialized + patch( + "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._DIAGNOSTIC_LOG_PATH", + os.path.dirname(file_path), + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._DIAGNOSTIC_LOGGER_FILE_NAME", + os.path.basename(file_path), + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._get_customer_ikey_from_env_var", + return_value=TEST_CUSTOMER_IKEY, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._EXTENSION_VERSION", + TEST_EXTENSION_VERSION, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.diagnostic_logging.VERSION", + TEST_VERSION, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.diagnostic_logging._IS_DIAGNOSTICS_ENABLED", + is_diagnostics_enabled, + ).start() + diagnostic_logger.AzureDiagnosticLogging.enable(logger) + + +class TestDiagnosticLogger: + + def test_initialized(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + assert diagnostic_logger.AzureDiagnosticLogging._initialized is True + + def test_uninitialized(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=False) + assert diagnostic_logger.AzureDiagnosticLogging._initialized is False + + def test_info(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + TEST_LOGGER_SUB_MODULE.info(MESSAGE1) + TEST_LOGGER_SUB_MODULE.info(MESSAGE2) + check_file_is_empty(temp_file_path) + + def test_info_with_info_log_level(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + TEST_LOGGER_SUB_MODULE.setLevel(logging.INFO) + TEST_LOGGER_SUB_MODULE.info(MESSAGE1) + TEST_LOGGER_SUB_MODULE.info(MESSAGE2) + TEST_LOGGER_SUB_MODULE.setLevel(logging.NOTSET) + check_file_for_messages(temp_file_path, "INFO", (MESSAGE1, MESSAGE2)) + + def test_info_with_sub_module_info_log_level(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + TEST_LOGGER_SUB_MODULE.setLevel(logging.INFO) + TEST_LOGGER_SUB_MODULE.info(MESSAGE1) + TEST_LOGGER_SUB_MODULE.info(MESSAGE2) + TEST_LOGGER_SUB_MODULE.setLevel(logging.NOTSET) + check_file_for_messages(temp_file_path, "INFO", (MESSAGE1, MESSAGE2)) + + def test_warning(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) + TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) + check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) + + def test_warning_multiple_enable(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER) + diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER) + TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) + TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) + check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) + + def test_error(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + TEST_LOGGER_SUB_MODULE.error(MESSAGE1) + TEST_LOGGER_SUB_MODULE.error(MESSAGE2) + check_file_for_messages(temp_file_path, "ERROR", (MESSAGE1, MESSAGE2)) + + def test_off_app_service_info(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=False) + TEST_LOGGER.info(MESSAGE1) + TEST_LOGGER.info(MESSAGE2) + TEST_LOGGER_SUB_MODULE.info(MESSAGE1) + TEST_LOGGER_SUB_MODULE.info(MESSAGE2) + check_file_is_empty(temp_file_path) + + def test_off_app_service_warning(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=False) + TEST_LOGGER.warning(MESSAGE1) + TEST_LOGGER.warning(MESSAGE2) + TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) + TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) + check_file_is_empty(temp_file_path) + + def test_off_app_service_error(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=False) + TEST_LOGGER.error(MESSAGE1) + TEST_LOGGER.error(MESSAGE2) + TEST_LOGGER_SUB_MODULE.error(MESSAGE1) + TEST_LOGGER_SUB_MODULE.error(MESSAGE2) + check_file_is_empty(temp_file_path) + + def test_subscription_id_plus(self, temp_file_path): + set_up( + temp_file_path, + is_diagnostics_enabled=True, + subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, + ) + assert diagnostic_logger._SUBSCRIPTION_ID == TEST_SUBSCRIPTION_ID + TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) + TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) + check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) + + def test_subscription_id_no_plus(self, temp_file_path): + set_up( + temp_file_path, + is_diagnostics_enabled=True, + subscription_id_env_var=TEST_SUBSCRIPTION_ID, + ) + assert diagnostic_logger._SUBSCRIPTION_ID == TEST_SUBSCRIPTION_ID + TEST_LOGGER_SUB_MODULE.warning(MESSAGE1) + TEST_LOGGER_SUB_MODULE.warning(MESSAGE2) + check_file_for_messages(temp_file_path, "WARNING", (MESSAGE1, MESSAGE2)) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_status_logger.py b/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_status_logger.py index d7851bf95377..2b5fcf75b5d4 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_status_logger.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/diagnostics/test_status_logger.py @@ -1,136 +1,136 @@ -# # ------------------------------------------------------------------------- -# # Copyright (c) Microsoft Corporation. All rights reserved. -# # Licensed under the MIT License. See License in the project root for -# # license information. -# # -------------------------------------------------------------------------- - -# import os -# from json import loads -# from unittest.mock import patch - -# from azure.monitor.opentelemetry._diagnostics.status_logger import ( -# AzureStatusLogger, -# _get_status_logger_file_name -# ) - -# TEST_MACHINE_NAME = "TEST_MACHINE_NAME" -# TEST_PID = 321 -# TEST_OPERATION = "TEST_OPERATION" -# TEST_OPERATION = "TEST_OPERATION" -# TEST_SITE_NAME = "TEST_SITE_NAME" -# TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY" -# TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION" -# TEST_VERSION = "TEST_VERSION" -# TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID" -# MESSAGE1 = "MESSAGE1" -# MESSAGE2 = "MESSAGE2" - - -# def set_up(file_path, is_diagnostics_enabled=True): - -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH", -# file_path, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH", -# os.path.dirname(file_path), -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger._get_status_logger_file_name", -# return_value=os.path.basename(file_path), -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger._get_customer_ikey_from_env_var", -# return_value=TEST_CUSTOMER_IKEY, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger._EXTENSION_VERSION", -# TEST_EXTENSION_VERSION, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger.VERSION", -# TEST_VERSION, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger._IS_DIAGNOSTICS_ENABLED", -# is_diagnostics_enabled, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger.getpid", -# return_value=TEST_PID, -# ).start() -# patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME", -# TEST_MACHINE_NAME, -# ).start() - - -# def check_file_for_messages(agent_initialized_successfully, file_path, reason=None): -# with open(file_path, "r") as f: -# f.seek(0) -# json = loads(f.readline()) -# assert json["AgentInitializedSuccessfully"] == agent_initialized_successfully -# assert json["AppType"] == "python" -# assert json["MachineName"] == TEST_MACHINE_NAME -# assert json["PID"] == TEST_PID -# assert json["SdkVersion"] == TEST_VERSION -# assert json["Ikey"] == TEST_CUSTOMER_IKEY -# assert json["ExtensionVersion"] == TEST_EXTENSION_VERSION -# if reason: -# assert json["Reason"] == reason -# else: -# assert "Reason" not in json -# assert not f.read() - - -# def check_file_is_empty(file_path): -# with open(file_path, "r") as f: -# f.seek(0) -# assert not f.read() - - -# class TestStatusLogger: - -# def test_log_status_success(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# AzureStatusLogger.log_status(False, MESSAGE1) -# AzureStatusLogger.log_status(True, MESSAGE2) -# check_file_for_messages(True, temp_file_path, MESSAGE2) - -# def test_log_status_failed_initialization(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# AzureStatusLogger.log_status(True, MESSAGE1) -# AzureStatusLogger.log_status(False, MESSAGE2) -# check_file_for_messages(False, temp_file_path, MESSAGE2) - -# def test_log_status_no_reason(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=True) -# AzureStatusLogger.log_status(False, MESSAGE1) -# AzureStatusLogger.log_status(True) -# check_file_for_messages(True, temp_file_path) - -# def test_disabled_log_status_success(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=False) -# AzureStatusLogger.log_status(False, MESSAGE1) -# AzureStatusLogger.log_status(True, MESSAGE2) -# check_file_is_empty(temp_file_path) - -# def test_disabled_log_status_failed_initialization(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=False) -# AzureStatusLogger.log_status(True, MESSAGE1) -# AzureStatusLogger.log_status(False, MESSAGE2) -# check_file_is_empty(temp_file_path) - -# def test_disabled_log_status_no_reason(self, temp_file_path): -# set_up(temp_file_path, is_diagnostics_enabled=False) -# AzureStatusLogger.log_status(False, MESSAGE1) -# AzureStatusLogger.log_status(True) -# check_file_is_empty(temp_file_path) - -# @patch( -# "azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME", -# TEST_MACHINE_NAME, -# ) -# def test_get_status_logger_file_name(self): -# assert _get_status_logger_file_name(TEST_PID) == f"status_{TEST_MACHINE_NAME}_{TEST_PID}.json" +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +import os +from json import loads +from unittest.mock import patch + +from azure.monitor.opentelemetry._diagnostics.status_logger import ( + AzureStatusLogger, + _get_status_logger_file_name +) + +TEST_MACHINE_NAME = "TEST_MACHINE_NAME" +TEST_PID = 321 +TEST_OPERATION = "TEST_OPERATION" +TEST_OPERATION = "TEST_OPERATION" +TEST_SITE_NAME = "TEST_SITE_NAME" +TEST_CUSTOMER_IKEY = "TEST_CUSTOMER_IKEY" +TEST_EXTENSION_VERSION = "TEST_EXTENSION_VERSION" +TEST_VERSION = "TEST_VERSION" +TEST_SUBSCRIPTION_ID = "TEST_SUBSCRIPTION_ID" +MESSAGE1 = "MESSAGE1" +MESSAGE2 = "MESSAGE2" + + +def set_up(file_path, is_diagnostics_enabled=True): + + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH", + file_path, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger._STATUS_LOG_PATH", + os.path.dirname(file_path), + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger._get_status_logger_file_name", + return_value=os.path.basename(file_path), + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger._get_customer_ikey_from_env_var", + return_value=TEST_CUSTOMER_IKEY, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger._EXTENSION_VERSION", + TEST_EXTENSION_VERSION, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger.VERSION", + TEST_VERSION, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger._IS_DIAGNOSTICS_ENABLED", + is_diagnostics_enabled, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger.getpid", + return_value=TEST_PID, + ).start() + patch( + "azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME", + TEST_MACHINE_NAME, + ).start() + + +def check_file_for_messages(agent_initialized_successfully, file_path, reason=None): + with open(file_path, "r") as f: + f.seek(0) + json = loads(f.readline()) + assert json["AgentInitializedSuccessfully"] == agent_initialized_successfully + assert json["AppType"] == "python" + assert json["MachineName"] == TEST_MACHINE_NAME + assert json["PID"] == TEST_PID + assert json["SdkVersion"] == TEST_VERSION + assert json["Ikey"] == TEST_CUSTOMER_IKEY + assert json["ExtensionVersion"] == TEST_EXTENSION_VERSION + if reason: + assert json["Reason"] == reason + else: + assert "Reason" not in json + assert not f.read() + + +def check_file_is_empty(file_path): + with open(file_path, "r") as f: + f.seek(0) + assert not f.read() + + +class TestStatusLogger: + + def test_log_status_success(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + AzureStatusLogger.log_status(False, MESSAGE1) + AzureStatusLogger.log_status(True, MESSAGE2) + check_file_for_messages(True, temp_file_path, MESSAGE2) + + def test_log_status_failed_initialization(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + AzureStatusLogger.log_status(True, MESSAGE1) + AzureStatusLogger.log_status(False, MESSAGE2) + check_file_for_messages(False, temp_file_path, MESSAGE2) + + def test_log_status_no_reason(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=True) + AzureStatusLogger.log_status(False, MESSAGE1) + AzureStatusLogger.log_status(True) + check_file_for_messages(True, temp_file_path) + + def test_disabled_log_status_success(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=False) + AzureStatusLogger.log_status(False, MESSAGE1) + AzureStatusLogger.log_status(True, MESSAGE2) + check_file_is_empty(temp_file_path) + + def test_disabled_log_status_failed_initialization(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=False) + AzureStatusLogger.log_status(True, MESSAGE1) + AzureStatusLogger.log_status(False, MESSAGE2) + check_file_is_empty(temp_file_path) + + def test_disabled_log_status_no_reason(self, temp_file_path): + set_up(temp_file_path, is_diagnostics_enabled=False) + AzureStatusLogger.log_status(False, MESSAGE1) + AzureStatusLogger.log_status(True) + check_file_is_empty(temp_file_path) + + @patch( + "azure.monitor.opentelemetry._diagnostics.status_logger._MACHINE_NAME", + TEST_MACHINE_NAME, + ) + def test_get_status_logger_file_name(self): + assert _get_status_logger_file_name(TEST_PID) == f"status_{TEST_MACHINE_NAME}_{TEST_PID}.json" diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/exporter/test_exporter.py b/sdk/monitor/azure-monitor-opentelemetry/tests/exporter/test_exporter.py index f5ac3b76f0aa..21561ac56bdc 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/exporter/test_exporter.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/exporter/test_exporter.py @@ -1,38 +1,38 @@ -# # Copyright The OpenTelemetry Authors -# # -# # Licensed under the Apache License, Version 2.0 (the "License"); -# # you may not use this file except in compliance with the License. -# # You may obtain a copy of the License at -# # -# # http://www.apache.org/licenses/LICENSE-2.0 -# # -# # Unless required by applicable law or agreed to in writing, software -# # distributed under the License is distributed on an "AS IS" BASIS, -# # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# # See the License for the specific language governing permissions and -# # limitations under the License. +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. -# import unittest +import unittest -# from azure.monitor.opentelemetry.exporter import ( -# AzureMonitorLogExporter, -# AzureMonitorMetricExporter, -# AzureMonitorTraceExporter, -# ) +from azure.monitor.opentelemetry.exporter import ( + AzureMonitorLogExporter, + AzureMonitorMetricExporter, + AzureMonitorTraceExporter, +) -# class TestAzureMonitorExporters(unittest.TestCase): -# def test_constructors(self): -# cs_string = "InstrumentationKey=1234abcd-5678-4efa-8abc-1234567890ab" -# for exporter in [ -# AzureMonitorLogExporter, -# AzureMonitorMetricExporter, -# AzureMonitorTraceExporter, -# ]: -# try: -# exporter(connection_string=cs_string) -# except Exception as ex: # pylint: disable=broad-except -# print(ex) -# self.fail( -# f"Unexpected exception raised when instantiating {exporter.__name__}" -# ) +class TestAzureMonitorExporters(unittest.TestCase): + def test_constructors(self): + cs_string = "InstrumentationKey=1234abcd-5678-4efa-8abc-1234567890ab" + for exporter in [ + AzureMonitorLogExporter, + AzureMonitorMetricExporter, + AzureMonitorTraceExporter, + ]: + try: + exporter(connection_string=cs_string) + except Exception as ex: # pylint: disable=broad-except + print(ex) + self.fail( + f"Unexpected exception raised when instantiating {exporter.__name__}" + ) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py index 87a7c5939d52..b78f1846bf98 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py @@ -1,171 +1,171 @@ -# # ------------------------------------------------------------------------- -# # Copyright (c) Microsoft Corporation. All rights reserved. -# # Licensed under the MIT License. See License in the project root for -# # license information. -# # -------------------------------------------------------------------------- - -# from importlib import reload -# from os import environ -# from unittest import TestCase -# from unittest.mock import patch - -# from azure.monitor.opentelemetry import _constants - -# TEST_VALUE = "TEST_VALUE" -# TEST_IKEY = "1234abcd-ab12-34cd-ab12-a23456abcdef" -# TEST_CONN_STR = f"InstrumentationKey={TEST_IKEY};IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/" - - -# def clear_env_var(env_var): -# if env_var in environ: -# del environ[env_var] - - -# class TestConstants(TestCase): -# @patch.dict( -# "os.environ", -# {"ApplicationInsightsAgent_EXTENSION_VERSION": TEST_VALUE}, -# ) -# def test_extension_version(self): -# reload(_constants) -# self.assertEqual(_constants._EXTENSION_VERSION, TEST_VALUE) - -# def test_extension_version_default(self): -# clear_env_var("ApplicationInsightsAgent_EXTENSION_VERSION") -# reload(_constants) -# self.assertEqual(_constants._EXTENSION_VERSION, "disabled") - -# @patch.dict( -# "os.environ", {"APPLICATIONINSIGHTS_CONNECTION_STRING": TEST_CONN_STR} -# ) -# def test_ikey(self): -# reload(_constants) -# self.assertEqual( -# _constants._get_customer_ikey_from_env_var(), TEST_IKEY -# ) - -# def test_ikey_defaults(self): -# clear_env_var("APPLICATIONINSIGHTS_CONNECTION_STRING") -# reload(_constants) -# self.assertEqual( -# _constants._get_customer_ikey_from_env_var(), "unknown" -# ) - -# # TODO: Enabled when duplicate logging issue is solved -# # @patch.dict( -# # "os.environ", -# # {"AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS": "True"}, -# # ) -# # def test_exporter_diagnostics_enabled(self): -# # reload(_constants) -# # self.assertTrue(_constants._EXPORTER_DIAGNOSTICS_ENABLED) - -# # def test_exporter_diagnostics_disabled(self): -# # clear_env_var("AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS") -# # reload(_constants) -# # self.assertFalse(_constants._EXPORTER_DIAGNOSTICS_ENABLED) - -# # @patch.dict( -# # "os.environ", -# # {"AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS": "foobar"}, -# # ) -# # def test_exporter_diagnostics_other(self): -# # reload(_constants) -# # self.assertFalse(_constants._EXPORTER_DIAGNOSTICS_ENABLED) - -# @patch.dict("os.environ", {"WEBSITE_SITE_NAME": TEST_VALUE}) -# def test_diagnostics_enabled(self): -# reload(_constants) -# self.assertTrue(_constants._IS_DIAGNOSTICS_ENABLED) - -# def test_diagnostics_disabled(self): -# clear_env_var("WEBSITE_SITE_NAME") -# reload(_constants) -# self.assertFalse(_constants._IS_DIAGNOSTICS_ENABLED) - -# @patch( -# "azure.monitor.opentelemetry._constants.platform.system", -# return_value="Linux", -# ) -# def test_log_path_linux(self, mock_system): -# self.assertEqual( -# _constants._get_log_path(), "/var/log/applicationinsights" -# ) - -# @patch( -# "azure.monitor.opentelemetry._constants.platform.system", -# return_value="Linux", -# ) -# def test_status_log_path_linux(self, mock_system): -# self.assertEqual( -# _constants._get_log_path(status_log_path=True), -# "/var/log/applicationinsights", -# ) - -# @patch( -# "azure.monitor.opentelemetry._constants.platform.system", -# return_value="Windows", -# ) -# @patch("pathlib.Path.home", return_value="\\HOME\\DIR") -# def test_log_path_windows(self, mock_system, mock_home): -# self.assertEqual( -# _constants._get_log_path(), -# "\\HOME\\DIR\\LogFiles\\ApplicationInsights", -# ) - -# @patch( -# "azure.monitor.opentelemetry._constants.platform.system", -# return_value="Windows", -# ) -# @patch("pathlib.Path.home", return_value="\\HOME\\DIR") -# def test_status_log_path_windows(self, mock_system, mock_home): -# self.assertEqual( -# _constants._get_log_path(status_log_path=True), -# "\\HOME\\DIR\\LogFiles\\ApplicationInsights\\status", -# ) - -# @patch( -# "azure.monitor.opentelemetry._constants.platform.system", -# return_value="Window", -# ) -# def test_log_path_other(self, mock_platform): -# self.assertIsNone(_constants._get_log_path()) - -# @patch( -# "azure.monitor.opentelemetry._constants.platform.system", -# return_value="linux", -# ) -# def test_status_log_path_other(self, mock_platform): -# self.assertIsNone(_constants._get_log_path(status_log_path=True)) - -# @patch.dict("os.environ", {"key": "value"}) -# def test_env_var_or_default(self): -# self.assertEqual(_constants._env_var_or_default("key"), "value") - -# @patch.dict("os.environ", {}) -# def test_env_var_or_default_empty(self): -# self.assertEqual(_constants._env_var_or_default("key"), "") - -# @patch.dict("os.environ", {}) -# def test_env_var_or_default_empty_with_defaults(self): -# self.assertEqual( -# _constants._env_var_or_default("key", default_val="value"), "value" -# ) - -# @patch( -# "azure.monitor.opentelemetry._constants.isdir", -# return_value=True, -# ) -# def test_attach_enabled(self, mock_isdir): -# self.assertEqual( -# _constants._is_attach_enabled(), True -# ) - -# @patch( -# "azure.monitor.opentelemetry._constants.isdir", -# return_value=False, -# ) -# def test_attach_disabled(self, mock_isdir): -# self.assertEqual( -# _constants._is_attach_enabled(), False -# ) +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License in the project root for +# license information. +# -------------------------------------------------------------------------- + +from importlib import reload +from os import environ +from unittest import TestCase +from unittest.mock import patch + +from azure.monitor.opentelemetry import _constants + +TEST_VALUE = "TEST_VALUE" +TEST_IKEY = "1234abcd-ab12-34cd-ab12-a23456abcdef" +TEST_CONN_STR = f"InstrumentationKey={TEST_IKEY};IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/" + + +def clear_env_var(env_var): + if env_var in environ: + del environ[env_var] + + +class TestConstants(TestCase): + @patch.dict( + "os.environ", + {"ApplicationInsightsAgent_EXTENSION_VERSION": TEST_VALUE}, + ) + def test_extension_version(self): + reload(_constants) + self.assertEqual(_constants._EXTENSION_VERSION, TEST_VALUE) + + def test_extension_version_default(self): + clear_env_var("ApplicationInsightsAgent_EXTENSION_VERSION") + reload(_constants) + self.assertEqual(_constants._EXTENSION_VERSION, "disabled") + + @patch.dict( + "os.environ", {"APPLICATIONINSIGHTS_CONNECTION_STRING": TEST_CONN_STR} + ) + def test_ikey(self): + reload(_constants) + self.assertEqual( + _constants._get_customer_ikey_from_env_var(), TEST_IKEY + ) + + def test_ikey_defaults(self): + clear_env_var("APPLICATIONINSIGHTS_CONNECTION_STRING") + reload(_constants) + self.assertEqual( + _constants._get_customer_ikey_from_env_var(), "unknown" + ) + + # TODO: Enabled when duplicate logging issue is solved + # @patch.dict( + # "os.environ", + # {"AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS": "True"}, + # ) + # def test_exporter_diagnostics_enabled(self): + # reload(_constants) + # self.assertTrue(_constants._EXPORTER_DIAGNOSTICS_ENABLED) + + # def test_exporter_diagnostics_disabled(self): + # clear_env_var("AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS") + # reload(_constants) + # self.assertFalse(_constants._EXPORTER_DIAGNOSTICS_ENABLED) + + # @patch.dict( + # "os.environ", + # {"AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS": "foobar"}, + # ) + # def test_exporter_diagnostics_other(self): + # reload(_constants) + # self.assertFalse(_constants._EXPORTER_DIAGNOSTICS_ENABLED) + + @patch.dict("os.environ", {"WEBSITE_SITE_NAME": TEST_VALUE}) + def test_diagnostics_enabled(self): + reload(_constants) + self.assertTrue(_constants._IS_DIAGNOSTICS_ENABLED) + + def test_diagnostics_disabled(self): + clear_env_var("WEBSITE_SITE_NAME") + reload(_constants) + self.assertFalse(_constants._IS_DIAGNOSTICS_ENABLED) + + @patch( + "azure.monitor.opentelemetry._constants.platform.system", + return_value="Linux", + ) + def test_log_path_linux(self, mock_system): + self.assertEqual( + _constants._get_log_path(), "/var/log/applicationinsights" + ) + + @patch( + "azure.monitor.opentelemetry._constants.platform.system", + return_value="Linux", + ) + def test_status_log_path_linux(self, mock_system): + self.assertEqual( + _constants._get_log_path(status_log_path=True), + "/var/log/applicationinsights", + ) + + @patch( + "azure.monitor.opentelemetry._constants.platform.system", + return_value="Windows", + ) + @patch("pathlib.Path.home", return_value="\\HOME\\DIR") + def test_log_path_windows(self, mock_system, mock_home): + self.assertEqual( + _constants._get_log_path(), + "\\HOME\\DIR\\LogFiles\\ApplicationInsights", + ) + + @patch( + "azure.monitor.opentelemetry._constants.platform.system", + return_value="Windows", + ) + @patch("pathlib.Path.home", return_value="\\HOME\\DIR") + def test_status_log_path_windows(self, mock_system, mock_home): + self.assertEqual( + _constants._get_log_path(status_log_path=True), + "\\HOME\\DIR\\LogFiles\\ApplicationInsights\\status", + ) + + @patch( + "azure.monitor.opentelemetry._constants.platform.system", + return_value="Window", + ) + def test_log_path_other(self, mock_platform): + self.assertIsNone(_constants._get_log_path()) + + @patch( + "azure.monitor.opentelemetry._constants.platform.system", + return_value="linux", + ) + def test_status_log_path_other(self, mock_platform): + self.assertIsNone(_constants._get_log_path(status_log_path=True)) + + @patch.dict("os.environ", {"key": "value"}) + def test_env_var_or_default(self): + self.assertEqual(_constants._env_var_or_default("key"), "value") + + @patch.dict("os.environ", {}) + def test_env_var_or_default_empty(self): + self.assertEqual(_constants._env_var_or_default("key"), "") + + @patch.dict("os.environ", {}) + def test_env_var_or_default_empty_with_defaults(self): + self.assertEqual( + _constants._env_var_or_default("key", default_val="value"), "value" + ) + + @patch( + "azure.monitor.opentelemetry._constants.isdir", + return_value=True, + ) + def test_attach_enabled(self, mock_isdir): + self.assertEqual( + _constants._is_attach_enabled(), True + ) + + @patch( + "azure.monitor.opentelemetry._constants.isdir", + return_value=False, + ) + def test_attach_disabled(self, mock_isdir): + self.assertEqual( + _constants._is_attach_enabled(), False + ) From 017b2e7829268fc974572f28826369bfa03b158d Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 12:00:18 -0700 Subject: [PATCH 13/22] remove importlib --- .../monitor/opentelemetry/exporter/_utils.py | 19 ++++++++++++------- .../setup.py | 1 - shared_requirements.txt | 1 - 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py index 5a66eb988d4f..c0369c1056db 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/_utils.py @@ -8,12 +8,6 @@ import threading import time -try: - from importlib.metadata import version -except ImportError: - # fallback for python 3.7 - from importlib_metadata import version - from opentelemetry.semconv.resource import ResourceAttributes from opentelemetry.sdk.util import ns_to_iso_str @@ -22,8 +16,19 @@ from azure.monitor.opentelemetry.exporter._constants import _INSTRUMENTATIONS_BIT_MAP +opentelemetry_version = "" + # Workaround for missing version file -opentelemetry_version = version("opentelemetry-sdk") +try: + from importlib.metadata import version + opentelemetry_version = version("opentelemetry-sdk") +except ImportError: + # Temporary workaround for =0.6.10", "opentelemetry-api~=1.20", "opentelemetry-sdk~=1.20", - "importlib-metadata~=6.0; python_version < '3.8'" ], entry_points={ "opentelemetry_traces_exporter": [ diff --git a/shared_requirements.txt b/shared_requirements.txt index bfc5ded731f0..3212428a3952 100644 --- a/shared_requirements.txt +++ b/shared_requirements.txt @@ -56,7 +56,6 @@ websocket-client avro uamqp yarl -importlib-metadata azure-identity openai wrapt From fe6bc53c838e0a70025b7b114b5cb95ba21948ea Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 13:25:54 -0700 Subject: [PATCH 14/22] bandit --- sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml | 1 + sdk/monitor/azure-monitor-opentelemetry/pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml b/sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml index ff779a518143..6a4ade2a03bb 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml @@ -3,3 +3,4 @@ mypy = false pyright = false type_check_samples = false verifytypes = false +bandit = false \ No newline at end of file diff --git a/sdk/monitor/azure-monitor-opentelemetry/pyproject.toml b/sdk/monitor/azure-monitor-opentelemetry/pyproject.toml index 0df88a919dca..2031fbbb52b3 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/pyproject.toml +++ b/sdk/monitor/azure-monitor-opentelemetry/pyproject.toml @@ -4,4 +4,4 @@ verifytypes = false pyright = false mypy = false pylint = true -bandit = true +bandit = false From e0108fde7d8aecbf4b9e4ce44212760d996e0c88 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 14:07:15 -0700 Subject: [PATCH 15/22] Update CHANGELOG.md --- sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index 8e96b5c1e92e..468dba4a31a6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -5,7 +5,7 @@ ### Bugs Fixed - Handle missing or empty message data - ([#31944](https://github.com/Azure/azure-sdk-for-python/pull/31944)) + ([#31944](https://github.com/Azure/azure-sdk-for-python/pull/31944)) ## 1.0.0b16 (2023-08-30) From 27a8789d94acce3f21949e1ccaefeaddc04b2721 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 14:07:25 -0700 Subject: [PATCH 16/22] Update CHANGELOG.md --- sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md index 468dba4a31a6..8e96b5c1e92e 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/CHANGELOG.md @@ -5,7 +5,7 @@ ### Bugs Fixed - Handle missing or empty message data - ([#31944](https://github.com/Azure/azure-sdk-for-python/pull/31944)) + ([#31944](https://github.com/Azure/azure-sdk-for-python/pull/31944)) ## 1.0.0b16 (2023-08-30) From 1dd015d1f26779b7a5c8c77b932093e505ec653f Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 15:37:37 -0700 Subject: [PATCH 17/22] version --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 82332be1c198..492d9f350bd0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,6 +88,7 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", + "opentelemetry-api~=1.20", "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", @@ -95,6 +96,7 @@ "opentelemetry-instrumentation-requests~=0.41b0", "opentelemetry-instrumentation-urllib~=0.41b0", "opentelemetry-instrumentation-urllib3~=0.41b0", + "opentelemetry-sdk~=1.20", ], entry_points={ "opentelemetry_distro": [ From 7621708c9759d79bb25e7940ace0fa3af357f6ad Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 18:08:09 -0700 Subject: [PATCH 18/22] bump exporter --- .../azure-monitor-opentelemetry-exporter/pyproject.toml | 1 - sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt | 1 - sdk/monitor/azure-monitor-opentelemetry/pyproject.toml | 2 +- sdk/monitor/azure-monitor-opentelemetry/setup.py | 3 +-- 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml b/sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml index 6a4ade2a03bb..ff779a518143 100644 --- a/sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml +++ b/sdk/monitor/azure-monitor-opentelemetry-exporter/pyproject.toml @@ -3,4 +3,3 @@ mypy = false pyright = false type_check_samples = false verifytypes = false -bandit = false \ No newline at end of file diff --git a/sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt b/sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt index b3ef301f96f8..32110e7dd2b7 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt +++ b/sdk/monitor/azure-monitor-opentelemetry/dev_requirements.txt @@ -1,6 +1,5 @@ -e ../../../tools/azure-sdk-tools -e ../../../tools/azure-devtools -../azure-monitor-opentelemetry-exporter pytest django fastapi diff --git a/sdk/monitor/azure-monitor-opentelemetry/pyproject.toml b/sdk/monitor/azure-monitor-opentelemetry/pyproject.toml index 2031fbbb52b3..0df88a919dca 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/pyproject.toml +++ b/sdk/monitor/azure-monitor-opentelemetry/pyproject.toml @@ -4,4 +4,4 @@ verifytypes = false pyright = false mypy = false pylint = true -bandit = false +bandit = true diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 492d9f350bd0..0f9827893fc8 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,7 +88,7 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", - "opentelemetry-api~=1.20", + "azure-monitor-opentelemetry-exporter~=1.0.0b17" "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", @@ -96,7 +96,6 @@ "opentelemetry-instrumentation-requests~=0.41b0", "opentelemetry-instrumentation-urllib~=0.41b0", "opentelemetry-instrumentation-urllib3~=0.41b0", - "opentelemetry-sdk~=1.20", ], entry_points={ "opentelemetry_distro": [ From c9a8fb589e0a4835afd1e143fe57e761bf9cc2d4 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 18:30:35 -0700 Subject: [PATCH 19/22] Update setup.py --- sdk/monitor/azure-monitor-opentelemetry/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 0f9827893fc8..51ab333dbd66 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -88,7 +88,7 @@ install_requires=[ "azure-core<2.0.0,>=1.24.0", "azure-core-tracing-opentelemetry~=1.0.0b10", - "azure-monitor-opentelemetry-exporter~=1.0.0b17" + "azure-monitor-opentelemetry-exporter~=1.0.0b17", "opentelemetry-instrumentation-django~=0.41b0", "opentelemetry-instrumentation-fastapi~=0.41b0", "opentelemetry-instrumentation-flask~=0.41b0", From 678b6d27ef16dd6b6b12771949822c3125bfa302 Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 20:23:50 -0700 Subject: [PATCH 20/22] detectors --- .../azure-monitor-opentelemetry/CHANGELOG.md | 5 +++ .../azure/monitor/opentelemetry/_configure.py | 18 +++++++++ .../azure-monitor-opentelemetry/setup.py | 1 + .../tests/configuration/test_configure.py | 39 ++++++++++++++++++- 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index 46f5c01a7ba7..ba5c1041adf2 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -2,6 +2,11 @@ ## 1.0.0 (2023-09-12) +### Features Added + +- Add Azure resource detectors + ([#31931](https://github.com/Azure/azure-sdk-for-python/pull/31931)) + ### Other Changes - The `autoinstrumentation', 'diagnostics' and 'util' subnamespaces have been made internal. diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py index 53c5d6b3da7a..95f7c0d1907b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py @@ -3,6 +3,8 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- +import os + from logging import getLogger from typing import Dict, cast @@ -14,6 +16,7 @@ BaseInstrumentor, ) from opentelemetry.metrics import set_meter_provider +from opentelemetry.sdk.environment_variables import OTEL_EXPERIMENTAL_RESOURCE_DETECTORS from opentelemetry.sdk._logs import LoggerProvider, LoggingHandler from opentelemetry.sdk._logs.export import BatchLogRecordProcessor from opentelemetry.sdk.metrics import MeterProvider @@ -54,6 +57,11 @@ "urllib3", ) +_SUPPORTED_RESOURCE_DETECTORS = ( + "azure_app_service", + "azure_vm", +) + def configure_azure_monitor(**kwargs) -> None: """This function works as a configuration layer that allows the @@ -77,6 +85,9 @@ def configure_azure_monitor(**kwargs) -> None: disable_logging = configurations[DISABLE_LOGGING_ARG] disable_metrics = configurations[DISABLE_METRICS_ARG] + # Setup resources + _setup_resources() + # Setup tracing pipeline if not disable_tracing: _setup_tracing(configurations) @@ -94,6 +105,13 @@ def configure_azure_monitor(**kwargs) -> None: # instanstiated in the other setup steps _setup_instrumentations(configurations) +def _setup_resources(): + detectors = os.environ.get(OTEL_EXPERIMENTAL_RESOURCE_DETECTORS, "") + if detectors: + detectors = detectors + "," + detectors += ",".join(_SUPPORTED_RESOURCE_DETECTORS) + os.environ[OTEL_EXPERIMENTAL_RESOURCE_DETECTORS] = detectors + def _setup_tracing(configurations: Dict[str, ConfigurationValue]): sampling_ratio = configurations[SAMPLING_RATIO_ARG] diff --git a/sdk/monitor/azure-monitor-opentelemetry/setup.py b/sdk/monitor/azure-monitor-opentelemetry/setup.py index 51ab333dbd66..0cf2467b946a 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/setup.py +++ b/sdk/monitor/azure-monitor-opentelemetry/setup.py @@ -96,6 +96,7 @@ "opentelemetry-instrumentation-requests~=0.41b0", "opentelemetry-instrumentation-urllib~=0.41b0", "opentelemetry-instrumentation-urllib3~=0.41b0", + "opentelemetry-resource-detector-azure~=0.1.0", ], entry_points={ "opentelemetry_distro": [ diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py index 446ce56b10df..3b8ecb11517d 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py @@ -11,7 +11,7 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - +import os import unittest from unittest.mock import Mock, patch @@ -21,6 +21,7 @@ _setup_instrumentations, _setup_logging, _setup_metrics, + _setup_resources, _setup_tracing, configure_azure_monitor, ) @@ -39,8 +40,12 @@ class TestConfigure(unittest.TestCase): @patch( "azure.monitor.opentelemetry._configure._setup_tracing", ) + @patch( + "azure.monitor.opentelemetry._configure._setup_resources", + ) def test_configure_azure_monitor( self, + resource_mock, tracing_mock, logging_mock, metrics_mock, @@ -50,6 +55,7 @@ def test_configure_azure_monitor( "connection_string": "test_cs", } configure_azure_monitor(**kwargs) + resource_mock.assert_called_once() tracing_mock.assert_called_once() logging_mock.assert_called_once() metrics_mock.assert_called_once() @@ -67,12 +73,16 @@ def test_configure_azure_monitor( @patch( "azure.monitor.opentelemetry._configure._setup_tracing", ) + @patch( + "azure.monitor.opentelemetry._configure._setup_resources", + ) @patch( "azure.monitor.opentelemetry._configure._get_configurations", ) def test_configure_azure_monitor_disable_tracing( self, config_mock, + resource_mock, tracing_mock, logging_mock, metrics_mock, @@ -86,6 +96,7 @@ def test_configure_azure_monitor_disable_tracing( } config_mock.return_value = configurations configure_azure_monitor() + resource_mock.assert_called_once() tracing_mock.assert_not_called() logging_mock.assert_called_once_with(configurations) metrics_mock.assert_called_once_with(configurations) @@ -103,12 +114,16 @@ def test_configure_azure_monitor_disable_tracing( @patch( "azure.monitor.opentelemetry._configure._setup_tracing", ) + @patch( + "azure.monitor.opentelemetry._configure._setup_resources", + ) @patch( "azure.monitor.opentelemetry._configure._get_configurations", ) def test_configure_azure_monitor_disable_logging( self, config_mock, + resource_mock, tracing_mock, logging_mock, metrics_mock, @@ -122,6 +137,7 @@ def test_configure_azure_monitor_disable_logging( } config_mock.return_value = configurations configure_azure_monitor() + resource_mock.assert_called_once() tracing_mock.assert_called_once_with(configurations) logging_mock.assert_not_called() metrics_mock.assert_called_once_with(configurations) @@ -139,12 +155,16 @@ def test_configure_azure_monitor_disable_logging( @patch( "azure.monitor.opentelemetry._configure._setup_tracing", ) + @patch( + "azure.monitor.opentelemetry._configure._setup_resources", + ) @patch( "azure.monitor.opentelemetry._configure._get_configurations", ) def test_configure_azure_monitor_disable_metrics( self, config_mock, + resource_mock, tracing_mock, logging_mock, metrics_mock, @@ -158,11 +178,28 @@ def test_configure_azure_monitor_disable_metrics( } config_mock.return_value = configurations configure_azure_monitor() + resource_mock.assert_called_once() tracing_mock.assert_called_once_with(configurations) logging_mock.assert_called_once_with(configurations) metrics_mock.assert_not_called() instrumentation_mock.assert_called_once_with(configurations) + @patch.dict("os.environ", {"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": ""}) + def test_setup_resources(self): + _setup_resources() + self.assertEqual( + os.environ["OTEL_EXPERIMENTAL_RESOURCE_DETECTORS"], + "azure_app_service,azure_vm" + ) + + @patch.dict("os.environ", {"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "test_detector"}) + def test_setup_resources_existing_detectors(self): + _setup_resources() + self.assertEqual( + os.environ["OTEL_EXPERIMENTAL_RESOURCE_DETECTORS"], + "test_detector,azure_app_service,azure_vm" + ) + @patch( "azure.monitor.opentelemetry._configure.settings", ) From 8adce37707f0ead72774f86c3406a014e9409b6f Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 20:26:32 -0700 Subject: [PATCH 21/22] Update CHANGELOG.md --- sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index ba5c1041adf2..35a6cb8b1aba 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -5,7 +5,7 @@ ### Features Added - Add Azure resource detectors - ([#31931](https://github.com/Azure/azure-sdk-for-python/pull/31931)) + ([#32087](https://github.com/Azure/azure-sdk-for-python/pull/32087)) ### Other Changes From 8745e5fba6268fced5da4624677da61a67d3312b Mon Sep 17 00:00:00 2001 From: Leighton Chen Date: Wed, 13 Sep 2023 20:31:42 -0700 Subject: [PATCH 22/22] Update shared_requirements.txt --- shared_requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/shared_requirements.txt b/shared_requirements.txt index 3212428a3952..7762b42610cb 100644 --- a/shared_requirements.txt +++ b/shared_requirements.txt @@ -46,6 +46,7 @@ opentelemetry-instrumentation-psycopg2 opentelemetry-instrumentation-requests opentelemetry-instrumentation-urllib opentelemetry-instrumentation-urllib3 +opentelemetry-resource-detector-azure azure-nspkg azure-ai-nspkg azure-cognitiveservices-nspkg