Skip to content

Commit 14a864a

Browse files
authored
Private entry points (#31767)
* Add preview warning for entry points * Configurator tests * More detailed message * typo
1 parent 4e4d430 commit 14a864a

File tree

7 files changed

+91
-2
lines changed

7 files changed

+91
-2
lines changed

sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
([#31740](https://github.com/Azure/azure-sdk-for-python/pull/31740))
1111
- Un-vendoring instrumentations
1212
([#31744](https://github.com/Azure/azure-sdk-for-python/pull/31740))
13+
- Add preview warning for Autoinstrumentation entry points
14+
([#31767](https://github.com/Azure/azure-sdk-for-python/pull/31767))
1315

1416
### Breaking Changes
1517

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_constants.py

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import platform
99
from os import environ
10+
from os.path import isdir
1011
from pathlib import Path
1112

1213
from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error
@@ -36,6 +37,7 @@
3637
# "AZURE_MONITOR_OPENTELEMETRY_DISTRO_ENABLE_EXPORTER_DIAGNOSTICS"
3738
# )
3839
_CUSTOMER_IKEY_ENV_VAR = None
40+
_PREVIEW_ENTRY_POINT_WARNING = "Autoinstrumentation for the Azure Monitor OpenTelemetry Distro is in preview."
3941
logger = logging.getLogger(__name__)
4042

4143

@@ -85,3 +87,7 @@ def _env_var_or_default(var_name, default_val=""):
8587
)
8688
# TODO: Enabled when duplicate logging issue is solved
8789
# _EXPORTER_DIAGNOSTICS_ENABLED = _is_exporter_diagnostics_enabled()
90+
91+
92+
def _is_attach_enabled():
93+
return isdir("/agents/python/")

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/autoinstrumentation/_configurator.py

+7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66

77

88
import logging
9+
from warnings import warn
910

1011
from opentelemetry.sdk._configuration import _OTelSDKConfigurator
1112

13+
from azure.monitor.opentelemetry._constants import (
14+
_is_attach_enabled,
15+
_PREVIEW_ENTRY_POINT_WARNING,
16+
)
1217
from azure.monitor.opentelemetry.diagnostics._diagnostic_logging import (
1318
AzureDiagnosticLogging,
1419
)
@@ -18,6 +23,8 @@
1823

1924
class AzureMonitorConfigurator(_OTelSDKConfigurator):
2025
def _configure(self, **kwargs):
26+
if not _is_attach_enabled():
27+
warn(_PREVIEW_ENTRY_POINT_WARNING)
2128
try:
2229
AzureDiagnosticLogging.enable(_logger)
2330
super()._configure(**kwargs)

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/autoinstrumentation/_distro.py

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# --------------------------------------------------------------------------
66
import logging
77
from os import environ
8+
from warnings import warn
89

910
from opentelemetry.environment_variables import (
1011
OTEL_LOGS_EXPORTER,
@@ -20,6 +21,10 @@
2021

2122
from azure.core.settings import settings
2223
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
24+
from azure.monitor.opentelemetry._constants import (
25+
_is_attach_enabled,
26+
_PREVIEW_ENTRY_POINT_WARNING,
27+
)
2328
from azure.monitor.opentelemetry.diagnostics._diagnostic_logging import (
2429
AzureDiagnosticLogging,
2530
)
@@ -37,6 +42,8 @@
3742

3843
class AzureMonitorDistro(BaseDistro):
3944
def _configure(self, **kwargs) -> None:
45+
if not _is_attach_enabled():
46+
warn(_PREVIEW_ENTRY_POINT_WARNING)
4047
try:
4148
_configure_auto_instrumentation()
4249
except Exception as ex:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import warnings
2+
from unittest import TestCase
3+
from unittest.mock import patch
4+
5+
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
6+
from azure.monitor.opentelemetry.autoinstrumentation._configurator import (
7+
AzureMonitorConfigurator,
8+
)
9+
10+
11+
class TestConfigurator(TestCase):
12+
@patch("azure.monitor.opentelemetry.autoinstrumentation._configurator._is_attach_enabled", return_value=True)
13+
@patch(
14+
"azure.monitor.opentelemetry.autoinstrumentation._configurator.AzureDiagnosticLogging.enable"
15+
)
16+
def test_configure(self, mock_diagnostics, attach_mock):
17+
configurator = AzureMonitorConfigurator()
18+
with warnings.catch_warnings():
19+
warnings.simplefilter("error")
20+
configurator._configure()
21+
mock_diagnostics.assert_called_once()
22+
23+
@patch("azure.monitor.opentelemetry.autoinstrumentation._configurator._is_attach_enabled", return_value=False)
24+
@patch(
25+
"azure.monitor.opentelemetry.autoinstrumentation._configurator.AzureDiagnosticLogging.enable"
26+
)
27+
def test_configure_preview(self, mock_diagnostics, attach_mock):
28+
configurator = AzureMonitorConfigurator()
29+
with self.assertWarns(Warning):
30+
configurator._configure()
31+
mock_diagnostics.assert_called_once()

sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from unittest import TestCase
23
from unittest.mock import patch
34

@@ -8,13 +9,30 @@
89

910

1011
class TestDistro(TestCase):
12+
@patch("azure.monitor.opentelemetry.autoinstrumentation._distro._is_attach_enabled", return_value=True)
1113
@patch("azure.monitor.opentelemetry.autoinstrumentation._distro.settings")
1214
@patch(
1315
"azure.monitor.opentelemetry.autoinstrumentation._distro.AzureDiagnosticLogging.enable"
1416
)
15-
def test_configure(self, mock_diagnostics, azure_core_mock):
17+
def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock):
1618
distro = AzureMonitorDistro()
17-
distro.configure()
19+
with warnings.catch_warnings():
20+
warnings.simplefilter("error")
21+
distro.configure()
22+
self.assertEqual(mock_diagnostics.call_count, 2)
23+
self.assertEqual(
24+
azure_core_mock.tracing_implementation, OpenTelemetrySpan
25+
)
26+
27+
@patch("azure.monitor.opentelemetry.autoinstrumentation._distro._is_attach_enabled", return_value=False)
28+
@patch("azure.monitor.opentelemetry.autoinstrumentation._distro.settings")
29+
@patch(
30+
"azure.monitor.opentelemetry.autoinstrumentation._distro.AzureDiagnosticLogging.enable"
31+
)
32+
def test_configure_preview(self, mock_diagnostics, azure_core_mock, attach_mock):
33+
distro = AzureMonitorDistro()
34+
with self.assertWarns(Warning):
35+
distro.configure()
1836
self.assertEqual(mock_diagnostics.call_count, 2)
1937
self.assertEqual(
2038
azure_core_mock.tracing_implementation, OpenTelemetrySpan

sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py

+18
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,21 @@ def test_env_var_or_default_empty_with_defaults(self):
151151
self.assertEqual(
152152
_constants._env_var_or_default("key", default_val="value"), "value"
153153
)
154+
155+
@patch(
156+
"azure.monitor.opentelemetry._constants.isdir",
157+
return_value=True,
158+
)
159+
def test_attach_enabled(self, mock_isdir):
160+
self.assertEqual(
161+
_constants._is_attach_enabled(), True
162+
)
163+
164+
@patch(
165+
"azure.monitor.opentelemetry._constants.isdir",
166+
return_value=False,
167+
)
168+
def test_attach_disabled(self, mock_isdir):
169+
self.assertEqual(
170+
_constants._is_attach_enabled(), False
171+
)

0 commit comments

Comments
 (0)