diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index 6f72db2d7113..fd8f90e7bab4 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -2,6 +2,9 @@ ## 1.0.1 (Unreleased) +- Add message ids for AppLens + ([#32195](https://github.com/Azure/azure-sdk-for-python/pull/32195)) + ### Features Added ### Breaking Changes diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/configurator.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/configurator.py index bc990e74cd28..b2d70f580410 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/configurator.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/configurator.py @@ -5,7 +5,6 @@ # -------------------------------------------------------------------------- -import logging from warnings import warn from opentelemetry.sdk._configuration import _OTelSDKConfigurator @@ -16,9 +15,12 @@ ) from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import ( AzureDiagnosticLogging, + _ATTACH_FAILURE_CONFIGURATOR, + _ATTACH_SUCCESS_CONFIGURATOR, +) +from azure.monitor.opentelemetry._diagnostics.status_logger import ( + AzureStatusLogger, ) - -_logger = logging.getLogger(__name__) class AzureMonitorConfigurator(_OTelSDKConfigurator): @@ -26,15 +28,15 @@ def _configure(self, **kwargs): if not _is_attach_enabled(): warn(_PREVIEW_ENTRY_POINT_WARNING) try: - AzureDiagnosticLogging.enable(_logger) super()._configure(**kwargs) - except ValueError as e: - _logger.error( - "Azure Monitor Configurator failed during configuration due to a ValueError: %s", e + AzureStatusLogger.log_status(True) + AzureDiagnosticLogging.info( + "Azure Monitor Configurator configured successfully.", + _ATTACH_SUCCESS_CONFIGURATOR ) - raise e except Exception as e: - _logger.error( - "Azure Monitor Configurator failed during configuration: %s", e + AzureDiagnosticLogging.error( + "Azure Monitor Configurator failed during configuration: %s" % str(e), + _ATTACH_FAILURE_CONFIGURATOR, ) raise e diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py index cdc45ce2adbc..b0f5870d6fb6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py @@ -3,7 +3,6 @@ # Licensed under the MIT License. See License in the project root for # license information. # -------------------------------------------------------------------------- -import logging from os import environ from warnings import warn @@ -27,18 +26,13 @@ ) from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import ( AzureDiagnosticLogging, + _ATTACH_FAILURE_DISTRO, + _ATTACH_SUCCESS_DISTRO, ) from azure.monitor.opentelemetry._diagnostics.status_logger import ( AzureStatusLogger, ) -_CONFIG_FAILED_MSG = "Azure Monitor OpenTelemetry Distro failed during configuration: %s" - -_logger = logging.getLogger(__name__) -_opentelemetry_logger = logging.getLogger("opentelemetry") -# TODO: Enabled when duplicate logging issue is solved -# _exporter_logger = logging.getLogger("azure.monitor.opentelemetry.exporter") - class AzureMonitorDistro(BaseDistro): def _configure(self, **kwargs) -> None: @@ -46,36 +40,31 @@ def _configure(self, **kwargs) -> None: warn(_PREVIEW_ENTRY_POINT_WARNING) try: _configure_auto_instrumentation() - except Exception as ex: - _logger.exception( - ("Error occurred auto-instrumenting AzureMonitorDistro") + AzureStatusLogger.log_status(True) + AzureDiagnosticLogging.info( + "Azure Monitor OpenTelemetry Distro configured successfully.", + _ATTACH_SUCCESS_DISTRO + ) + except Exception as e: + AzureStatusLogger.log_status(False, reason=str(e)) + AzureDiagnosticLogging.error( + "Azure Monitor OpenTelemetry Distro failed during configuration: %s" % str(e), + _ATTACH_FAILURE_DISTRO, ) - raise ex + raise e def _configure_auto_instrumentation() -> None: - try: - AzureStatusLogger.log_status(False, "Distro being configured.") - AzureDiagnosticLogging.enable(_logger) - AzureDiagnosticLogging.enable(_opentelemetry_logger) - environ.setdefault( - OTEL_METRICS_EXPORTER, "azure_monitor_opentelemetry_exporter" - ) - environ.setdefault( - OTEL_TRACES_EXPORTER, "azure_monitor_opentelemetry_exporter" - ) - environ.setdefault( - OTEL_LOGS_EXPORTER, "azure_monitor_opentelemetry_exporter" - ) - environ.setdefault( - _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "true" - ) - settings.tracing_implementation = OpenTelemetrySpan - AzureStatusLogger.log_status(True) - _logger.info( - "Azure Monitor OpenTelemetry Distro configured successfully." - ) - except Exception as exc: - AzureStatusLogger.log_status(False, reason=exc) - _logger.error(_CONFIG_FAILED_MSG, exc) - raise exc + environ.setdefault( + OTEL_METRICS_EXPORTER, "azure_monitor_opentelemetry_exporter" + ) + environ.setdefault( + OTEL_TRACES_EXPORTER, "azure_monitor_opentelemetry_exporter" + ) + environ.setdefault( + OTEL_LOGS_EXPORTER, "azure_monitor_opentelemetry_exporter" + ) + environ.setdefault( + _OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "true" + ) + settings.tracing_implementation = OpenTelemetrySpan diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py index 6f1011a0d409..a48da1941d4b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py @@ -32,10 +32,6 @@ _IS_ON_APP_SERVICE = "WEBSITE_SITE_NAME" in environ # TODO: Add environment variable to enabled diagnostics off of App Service _IS_DIAGNOSTICS_ENABLED = _IS_ON_APP_SERVICE -# TODO: Enabled when duplicate logging issue is solved -# _EXPORTER_DIAGNOSTICS_ENABLED_ENV_VAR = ( -# "AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS" -# ) _CUSTOMER_IKEY_ENV_VAR = None _PREVIEW_ENTRY_POINT_WARNING = "Autoinstrumentation for the Azure Monitor OpenTelemetry Distro is in preview." logger = logging.getLogger(__name__) @@ -74,19 +70,9 @@ def _env_var_or_default(var_name, default_val=""): return default_val -# TODO: Enabled when duplicate logging issue is solved -# def _is_exporter_diagnostics_enabled(): -# return ( -# _EXPORTER_DIAGNOSTICS_ENABLED_ENV_VAR in environ -# and environ[_EXPORTER_DIAGNOSTICS_ENABLED_ENV_VAR] == "True" -# ) - - _EXTENSION_VERSION = _env_var_or_default( "ApplicationInsightsAgent_EXTENSION_VERSION", "disabled" ) -# TODO: Enabled when duplicate logging issue is solved -# _EXPORTER_DIAGNOSTICS_ENABLED = _is_exporter_diagnostics_enabled() def _is_attach_enabled(): diff --git a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py index cdfad69d0f1f..d860d33f5dd0 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py @@ -25,13 +25,18 @@ _SUBSCRIPTION_ID_ENV_VAR.split("+")[0] if _SUBSCRIPTION_ID_ENV_VAR else None ) _logger = logging.getLogger(__name__) +_logger.propagate = False +_logger.setLevel(logging.INFO) _DIAGNOSTIC_LOG_PATH = _get_log_path() +_ATTACH_SUCCESS_DISTRO = "4200" +_ATTACH_SUCCESS_CONFIGURATOR = "4201" +_ATTACH_FAILURE_DISTRO = "4400" +_ATTACH_FAILURE_CONFIGURATOR = "4401" class AzureDiagnosticLogging: _initialized = False _lock = threading.Lock() - _f_handler = None @classmethod def _initialize(cls): @@ -51,13 +56,14 @@ def _initialize(cls): + f'"extensionVersion":"{_EXTENSION_VERSION}", ' + f'"sdkVersion":"{VERSION}", ' + f'"subscriptionId":"{_SUBSCRIPTION_ID}", ' + + '"msgId":"%(msgId)s", ' + '"language":"python"' + "}" + "}" ) if not exists(_DIAGNOSTIC_LOG_PATH): makedirs(_DIAGNOSTIC_LOG_PATH) - AzureDiagnosticLogging._f_handler = logging.FileHandler( + f_handler = logging.FileHandler( join( _DIAGNOSTIC_LOG_PATH, _DIAGNOSTIC_LOGGER_FILE_NAME ) @@ -65,15 +71,21 @@ def _initialize(cls): formatter = logging.Formatter( fmt=log_format, datefmt="%Y-%m-%dT%H:%M:%S" ) - AzureDiagnosticLogging._f_handler.setFormatter(formatter) + f_handler.setFormatter(formatter) + _logger.addHandler(f_handler) AzureDiagnosticLogging._initialized = True - _logger.info("Initialized Azure Diagnostic Logger.") @classmethod - def enable(cls, logger: logging.Logger): + def info(cls, message: str, message_id: int): AzureDiagnosticLogging._initialize() - if AzureDiagnosticLogging._initialized and AzureDiagnosticLogging._f_handler: - logger.addHandler(AzureDiagnosticLogging._f_handler) - _logger.info( - "Added Azure diagnostics logging to %s.", logger.name - ) + _logger.info(message, extra={'msgId': message_id}) + + @classmethod + def warning(cls, message: str, message_id: int): + AzureDiagnosticLogging._initialize() + _logger.warning(message, extra={'msgId': message_id}) + + @classmethod + def error(cls, message: str, message_id: int): + AzureDiagnosticLogging._initialize() + _logger.error(message, extra={'msgId': message_id}) 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..c1c2cf4c04df 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py @@ -6,26 +6,51 @@ from azure.monitor.opentelemetry._autoinstrumentation.configurator import ( AzureMonitorConfigurator, ) +from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import ( + _ATTACH_FAILURE_CONFIGURATOR, + _ATTACH_SUCCESS_CONFIGURATOR +) class TestConfigurator(TestCase): @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True) @patch( - "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" + "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging" ) def test_configure(self, mock_diagnostics, attach_mock): configurator = AzureMonitorConfigurator() with warnings.catch_warnings(): warnings.simplefilter("error") configurator._configure() - mock_diagnostics.assert_called_once() + mock_diagnostics.info.assert_called_once_with( + "Azure Monitor Configurator configured successfully.", + _ATTACH_SUCCESS_CONFIGURATOR + ) @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=False) @patch( - "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging.enable" + "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging" ) def test_configure_preview(self, mock_diagnostics, attach_mock): configurator = AzureMonitorConfigurator() with self.assertWarns(Warning): configurator._configure() - mock_diagnostics.assert_called_once() + mock_diagnostics.info.assert_called_once_with( + "Azure Monitor Configurator configured successfully.", + _ATTACH_SUCCESS_CONFIGURATOR + ) + + @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.super") + @patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True) + @patch( + "azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging" + ) + def test_configure_exc(self, mock_diagnostics, attach_mock, super_mock): + configurator = AzureMonitorConfigurator() + super_mock()._configure.side_effect = Exception("Test Exception") + with self.assertRaises(Exception): + configurator._configure() + mock_diagnostics.error.assert_called_once_with( + "Azure Monitor Configurator failed during configuration: Test Exception", + _ATTACH_FAILURE_CONFIGURATOR + ) 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..510d1cacac4b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py @@ -6,20 +6,27 @@ from azure.monitor.opentelemetry._autoinstrumentation.distro import ( AzureMonitorDistro, ) +from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import ( + _ATTACH_FAILURE_DISTRO, + _ATTACH_SUCCESS_DISTRO +) 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" + "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging" ) 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) + mock_diagnostics.info.assert_called_once_with( + "Azure Monitor OpenTelemetry Distro configured successfully.", + _ATTACH_SUCCESS_DISTRO + ) self.assertEqual( azure_core_mock.tracing_implementation, OpenTelemetrySpan ) @@ -27,13 +34,32 @@ def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock): @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" + "azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging" ) 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) + mock_diagnostics.info.assert_called_once_with( + "Azure Monitor OpenTelemetry Distro configured successfully.", + _ATTACH_SUCCESS_DISTRO + ) self.assertEqual( azure_core_mock.tracing_implementation, OpenTelemetrySpan ) + + @patch("azure.monitor.opentelemetry._autoinstrumentation.distro._configure_auto_instrumentation") + @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" + ) + def test_configure_exc(self, mock_diagnostics, azure_core_mock, attach_mock, configure_mock): + distro = AzureMonitorDistro() + configure_mock.side_effect = Exception("Test Exception") + with self.assertRaises(Exception): + distro.configure() + mock_diagnostics.error.assert_called_once_with( + "Azure Monitor OpenTelemetry Distro failed during configuration: Test Exception", + _ATTACH_FAILURE_DISTRO + ) 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..0b2c222b4e79 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 @@ -17,15 +17,11 @@ 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_PLUS = "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): @@ -34,14 +30,14 @@ def clear_file(file_path): f.truncate() -def check_file_for_messages(file_path, level, messages, logger_name=TEST_LOGGER_NAME_SUB_MODULE): +def check_file_for_messages(file_path, level, messages): with open(file_path, "r") as f: f.seek(0) - for message in messages: + for (message, message_id) in messages: json = loads(f.readline()) assert json["time"] assert json["level"] == level - assert json["logger"] == logger_name + assert json["logger"] == "azure.monitor.opentelemetry._diagnostics.diagnostic_logging" assert json["message"] == message properties = json["properties"] assert properties["operation"] == "Startup" @@ -50,6 +46,7 @@ def check_file_for_messages(file_path, level, messages, logger_name=TEST_LOGGER_ assert properties["extensionVersion"] == TEST_EXTENSION_VERSION assert properties["sdkVersion"] == TEST_VERSION assert properties["subscriptionId"] == TEST_SUBSCRIPTION_ID + assert properties["msgId"] == message_id assert not f.read() @@ -62,14 +59,9 @@ def check_file_is_empty(file_path): def set_up( file_path, is_diagnostics_enabled, - logger=TEST_LOGGER, - subscription_id_env_var=TEST_SUBSCRIPTION_ID_ENV_VAR, + subscription_id_env_var=TEST_SUBSCRIPTION_ID_PLUS, ) -> 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", { @@ -103,13 +95,13 @@ def set_up( "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) + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE1, "4200") assert diagnostic_logger.AzureDiagnosticLogging._initialized is True def test_uninitialized(self, temp_file_path): @@ -118,80 +110,50 @@ def test_uninitialized(self, temp_file_path): 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)) + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE1, "4200") + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE2, "4301") + check_file_for_messages(temp_file_path, "INFO", ((MESSAGE1, "4200"), (MESSAGE2, "4301"))) 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)) + diagnostic_logger.AzureDiagnosticLogging.warning(MESSAGE1, "4200") + diagnostic_logger.AzureDiagnosticLogging.warning(MESSAGE2, "4301") + check_file_for_messages(temp_file_path, "WARNING", ((MESSAGE1, "4200"), (MESSAGE2, "4301"))) 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)) + diagnostic_logger.AzureDiagnosticLogging.error(MESSAGE1, "4200") + diagnostic_logger.AzureDiagnosticLogging.error(MESSAGE2, "4301") + check_file_for_messages(temp_file_path, "ERROR", ((MESSAGE1, "4200"), (MESSAGE2, "4301"))) 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) + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE1, "4200") + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE2, "4301") 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) + diagnostic_logger.AzureDiagnosticLogging.warning(MESSAGE1, "4200") + diagnostic_logger.AzureDiagnosticLogging.warning(MESSAGE2, "4301") 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) + diagnostic_logger.AzureDiagnosticLogging.error(MESSAGE1, "4200") + diagnostic_logger.AzureDiagnosticLogging.error(MESSAGE2, "4301") 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, + subscription_id_env_var=TEST_SUBSCRIPTION_ID_PLUS, ) 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)) + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE1, "4200") + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE2, "4301") + check_file_for_messages(temp_file_path, "INFO", ((MESSAGE1, "4200"), (MESSAGE2, "4301"))) def test_subscription_id_no_plus(self, temp_file_path): set_up( @@ -200,6 +162,6 @@ def test_subscription_id_no_plus(self, temp_file_path): 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)) + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE1, "4200") + diagnostic_logger.AzureDiagnosticLogging.info(MESSAGE2, "4301") + check_file_for_messages(temp_file_path, "INFO", ((MESSAGE1, "4200"), (MESSAGE2, "4301"))) diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py index b78f1846bf98..82ad1faa3dae 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py @@ -51,28 +51,6 @@ def test_ikey_defaults(self): _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)