diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index ea741a3be623..d913fb578dc3 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -4,6 +4,9 @@ ### Features Added +- Adding diagnostic warning when distro detects RP attach + ([#34971](https://github.com/Azure/azure-sdk-for-python/pull/34971)) + ### Breaking Changes ### Bugs Fixed 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 b2d70f580410..e229ff901902 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 @@ -9,10 +9,8 @@ from opentelemetry.sdk._configuration import _OTelSDKConfigurator -from azure.monitor.opentelemetry._constants import ( - _is_attach_enabled, - _PREVIEW_ENTRY_POINT_WARNING, -) +from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module +from azure.monitor.opentelemetry._constants import _PREVIEW_ENTRY_POINT_WARNING from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import ( AzureDiagnosticLogging, _ATTACH_FAILURE_CONFIGURATOR, 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 90e7ad512660..12e6970df5bb 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 @@ -21,8 +21,8 @@ from azure.core.settings import settings from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan +from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module from azure.monitor.opentelemetry._constants import ( - _is_attach_enabled, _AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME, _AZURE_SDK_INSTRUMENTATION_NAME, _PREVIEW_ENTRY_POINT_WARNING, 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 65c7e2816af3..a2d19b153be5 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_configure.py @@ -44,6 +44,11 @@ AzureMonitorMetricExporter, AzureMonitorTraceExporter, ) +from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module +from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import ( + _DISTRO_DETECTS_ATTACH, + AzureDiagnosticLogging, +) from azure.monitor.opentelemetry._util.configurations import ( _get_configurations, _is_instrumentation_enabled, @@ -76,6 +81,8 @@ def configure_azure_monitor(**kwargs) -> None: # pylint: disable=C4758 :rtype: None """ + _send_attach_warning() + configurations = _get_configurations(**kwargs) disable_tracing = configurations[DISABLE_TRACING_ARG] @@ -177,3 +184,11 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]): lib_name, exc_info=ex, ) + + +def _send_attach_warning(): + if _is_attach_enabled(): + AzureDiagnosticLogging.warning( + "Distro detected that automatic attach may have occurred. Check your data to ensure " + "that telemetry is not being duplicated. This may impact your cost.", + _DISTRO_DETECTS_ATTACH) 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 9f32f875374d..70ab4882b129 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py @@ -7,7 +7,6 @@ import logging import platform from os import environ -from os.path import isdir from pathlib import Path from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error,no-name-in-module @@ -99,10 +98,5 @@ def _env_var_or_default(var_name, default_val=""): _PREVIEW_INSTRUMENTED_LIBRARIES = () _ALL_SUPPORTED_INSTRUMENTED_LIBRARIES = _FULLY_SUPPORTED_INSTRUMENTED_LIBRARIES + _PREVIEW_INSTRUMENTED_LIBRARIES -# Autoinstrumentation - -def _is_attach_enabled(): - return isdir("/agents/python/") - _AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME = "azure_app_service" _AZURE_VM_RESOURCE_DETECTOR_NAME = "azure_vm" 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 07d0111ad9a8..6ba4cd6ef89a 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 @@ -28,6 +28,7 @@ _logger.propagate = False _logger.setLevel(logging.INFO) _DIAGNOSTIC_LOG_PATH = _get_log_path() +_DISTRO_DETECTS_ATTACH = "4100" _ATTACH_SUCCESS_DISTRO = "4200" _ATTACH_SUCCESS_CONFIGURATOR = "4201" _ATTACH_FAILURE_DISTRO = "4400" 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 bb7a4fea950c..844ce556eb8b 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py @@ -18,18 +18,23 @@ from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan from azure.monitor.opentelemetry._configure import ( + _send_attach_warning, _setup_instrumentations, _setup_logging, _setup_metrics, _setup_tracing, configure_azure_monitor, ) +from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import _DISTRO_DETECTS_ATTACH TEST_RESOURCE = Resource({"foo": "bar"}) class TestConfigure(unittest.TestCase): + @patch( + "azure.monitor.opentelemetry._configure._send_attach_warning", + ) @patch( "azure.monitor.opentelemetry._configure._setup_instrumentations", ) @@ -48,6 +53,7 @@ def test_configure_azure_monitor( logging_mock, metrics_mock, instrumentation_mock, + detect_attach_mock, ): kwargs = { "connection_string": "test_cs", @@ -57,6 +63,7 @@ def test_configure_azure_monitor( logging_mock.assert_called_once() metrics_mock.assert_called_once() instrumentation_mock.assert_called_once() + detect_attach_mock.assert_called_once() @patch( "azure.monitor.opentelemetry._configure._setup_instrumentations", @@ -464,3 +471,28 @@ def test_setup_instrumentations_disabled( ep2_mock.load.assert_called_once() instrumentor_mock.instrument.assert_called_once() logger_mock.debug.assert_called_once() + + @patch("azure.monitor.opentelemetry._configure.AzureDiagnosticLogging") + @patch("azure.monitor.opentelemetry._configure._is_attach_enabled") + def test_send_attach_warning_true( + self, + is_attach_enabled_mock, + mock_diagnostics, + ): + is_attach_enabled_mock.return_value = True + _send_attach_warning() + mock_diagnostics.warning.assert_called_once_with( + "Distro detected that automatic attach may have occurred. Check your data to ensure that telemetry is not being duplicated. This may impact your cost.", + _DISTRO_DETECTS_ATTACH, + ) + + @patch("azure.monitor.opentelemetry._configure.AzureDiagnosticLogging") + @patch("azure.monitor.opentelemetry._configure._is_attach_enabled") + def test_send_attach_warning_false( + self, + is_attach_enabled_mock, + mock_diagnostics, + ): + is_attach_enabled_mock.return_value = False + _send_attach_warning() + mock_diagnostics.warning.assert_not_called() diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py index 82ad1faa3dae..884e5df08960 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py @@ -129,21 +129,3 @@ 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 - )