Skip to content

Commit 49eac0d

Browse files
committed
Disabling diagnostics for diagnostic module. Added duplicate handler
prevention
1 parent d80b63d commit 49eac0d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

azure-monitor-opentelemetry-distro/azure/monitor/opentelemetry/distro/_diagnostics/_diagnostic_logging.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
)
1919
from azure.monitor.opentelemetry.distro._version import VERSION
2020

21-
_OPENTELEMETRY_DIAGNOSTIC_LOGGER_NAME = "opentelemetry"
2221
_DIAGNOSTIC_LOGGER_FILE_NAME = "applicationinsights-extension.log"
2322
_SITE_NAME = _env_var_or_default("WEBSITE_SITE_NAME")
2423
_SUBSCRIPTION_ID_ENV_VAR = _env_var_or_default("WEBSITE_OWNER_NAME")
@@ -68,14 +67,18 @@ def _initialize():
6867
fmt=format, datefmt="%Y-%m-%dT%H:%M:%S"
6968
)
7069
AzureDiagnosticLogging._f_handler.setFormatter(formatter)
71-
_logger.addHandler(AzureDiagnosticLogging._f_handler)
7270
AzureDiagnosticLogging._initialized = True
7371
_logger.info("Initialized Azure Diagnostic Logger.")
7472

7573
def enable(logger: logging.Logger):
7674
AzureDiagnosticLogging._initialize()
7775
if AzureDiagnosticLogging._initialized:
78-
logger.addHandler(AzureDiagnosticLogging._f_handler)
79-
_logger.info(
80-
"Added Azure diagnostics logging to %s." % logger.name
81-
)
76+
if AzureDiagnosticLogging._f_handler in logger.handlers:
77+
_logger.info(
78+
"Azure diagnostics already enabled for %s logger." % logger.name
79+
)
80+
else:
81+
logger.addHandler(AzureDiagnosticLogging._f_handler)
82+
_logger.info(
83+
"Added Azure diagnostics logging to %s." % logger.name
84+
)

azure-monitor-opentelemetry-distro/tests/diagnostics/test_diagnostic_logging.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ def test_warning(self):
151151
TEST_LOGGER_SUB_MODULE.warning(MESSAGE2)
152152
check_file_for_messages("WARNING", (MESSAGE1, MESSAGE2))
153153

154+
def test_warning_multiple_enable(self):
155+
set_up(is_diagnostics_enabled=True)
156+
diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER)
157+
diagnostic_logger.AzureDiagnosticLogging.enable(TEST_LOGGER)
158+
TEST_LOGGER_SUB_MODULE.warning(MESSAGE1)
159+
TEST_LOGGER_SUB_MODULE.warning(MESSAGE2)
160+
check_file_for_messages("WARNING", (MESSAGE1, MESSAGE2))
161+
154162
def test_error(self):
155163
set_up(is_diagnostics_enabled=True)
156164
TEST_LOGGER_SUB_MODULE.error(MESSAGE1)

0 commit comments

Comments
 (0)