Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__)


Expand Down Expand Up @@ -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/")
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# --------------------------------------------------------------------------
import logging
from os import environ
from warnings import warn

from opentelemetry.environment_variables import (
OTEL_LOGS_EXPORTER,
Expand All @@ -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,
)
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from unittest import TestCase
from unittest.mock import patch

Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)