diff --git a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md index 8e98f6de937f..2eaaeec33b77 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md +++ b/sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md @@ -10,6 +10,8 @@ ([#31740](https://github.com/Azure/azure-sdk-for-python/pull/31740)) - Un-vendoring instrumentations ([#31744](https://github.com/Azure/azure-sdk-for-python/pull/31740)) +- Add preview warning for Autoinstrumentation entry points + ([#31767](https://github.com/Azure/azure-sdk-for-python/pull/31767)) ### Breaking Changes 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 98ebb0c26873..079ce1f9c024 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py @@ -7,6 +7,7 @@ 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 @@ -36,6 +37,7 @@ # "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__) @@ -85,3 +87,7 @@ def _env_var_or_default(var_name, default_val=""): ) # TODO: Enabled when duplicate logging issue is solved # _EXPORTER_DIAGNOSTICS_ENABLED = _is_exporter_diagnostics_enabled() + + +def _is_attach_enabled(): + return isdir("/agents/python/") 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 8988cb1ab8d8..c3bedbae6504 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 @@ -6,9 +6,14 @@ import logging +from warnings import warn from opentelemetry.sdk._configuration import _OTelSDKConfigurator +from azure.monitor.opentelemetry._constants import ( + _is_attach_enabled, + _PREVIEW_ENTRY_POINT_WARNING, +) from azure.monitor.opentelemetry.diagnostics._diagnostic_logging import ( AzureDiagnosticLogging, ) @@ -18,6 +23,8 @@ class AzureMonitorConfigurator(_OTelSDKConfigurator): def _configure(self, **kwargs): + if not _is_attach_enabled(): + warn(_PREVIEW_ENTRY_POINT_WARNING) try: AzureDiagnosticLogging.enable(_logger) super()._configure(**kwargs) 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 2911421ef8dd..e9c13f77f9da 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 @@ -5,6 +5,7 @@ # -------------------------------------------------------------------------- import logging from os import environ +from warnings import warn from opentelemetry.environment_variables import ( OTEL_LOGS_EXPORTER, @@ -20,6 +21,10 @@ from azure.core.settings import settings from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan +from azure.monitor.opentelemetry._constants import ( + _is_attach_enabled, + _PREVIEW_ENTRY_POINT_WARNING, +) from azure.monitor.opentelemetry.diagnostics._diagnostic_logging import ( AzureDiagnosticLogging, ) @@ -37,6 +42,8 @@ class AzureMonitorDistro(BaseDistro): def _configure(self, **kwargs) -> None: + if not _is_attach_enabled(): + warn(_PREVIEW_ENTRY_POINT_WARNING) try: _configure_auto_instrumentation() except Exception as ex: diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py new file mode 100644 index 000000000000..72897c09d918 --- /dev/null +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py @@ -0,0 +1,31 @@ +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, +) + + +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() 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 f161506f0af7..1b91ae1de8c6 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py @@ -1,3 +1,4 @@ +import warnings from unittest import TestCase from unittest.mock import patch @@ -8,13 +9,30 @@ 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): + def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock): distro = AzureMonitorDistro() - distro.configure() + 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 diff --git a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py index adb7b7d0d648..b78f1846bf98 100644 --- a/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py +++ b/sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py @@ -151,3 +151,21 @@ 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 + )