Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions sdk/monitor/azure-monitor-opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
RESOURCE_ARG,
SAMPLING_RATIO_ARG,
SPAN_PROCESSORS_ARG,
_is_attach_enabled,
)
from azure.monitor.opentelemetry._types import ConfigurationValue
from azure.monitor.opentelemetry.exporter import ( # pylint: disable=import-error,no-name-in-module
Expand All @@ -44,6 +45,10 @@
AzureMonitorMetricExporter,
AzureMonitorTraceExporter,
)
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import (
_DISTRO_DETECTS_ATTACH,
AzureDiagnosticLogging,
)
from azure.monitor.opentelemetry._util.configurations import (
_get_configurations,
_is_instrumentation_enabled,
Expand Down Expand Up @@ -76,6 +81,8 @@ def configure_azure_monitor(**kwargs) -> None: # pylint: disable=C4758
:rtype: None
"""

_detect_attach()

configurations = _get_configurations(**kwargs)

disable_tracing = configurations[DISABLE_TRACING_ARG]
Expand Down Expand Up @@ -177,3 +184,11 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
lib_name,
exc_info=ex,
)


def _detect_attach():
Comment thread
lzchen marked this conversation as resolved.
Outdated
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)
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ def _env_var_or_default(var_name, default_val=""):
# Autoinstrumentation

def _is_attach_enabled():
Comment thread
lzchen marked this conversation as resolved.
Outdated
return isdir("/agents/python/")
if _IS_ON_APP_SERVICE:
return isdir("/agents/python/")
return False

_AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME = "azure_app_service"
_AZURE_VM_RESOURCE_DETECTOR_NAME = "azure_vm"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@

from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
from azure.monitor.opentelemetry._configure import (
_detect_attach,
_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._detect_attach",
)
@patch(
"azure.monitor.opentelemetry._configure._setup_instrumentations",
)
Expand All @@ -48,6 +53,7 @@ def test_configure_azure_monitor(
logging_mock,
metrics_mock,
instrumentation_mock,
detect_attach_mock,
):
kwargs = {
"connection_string": "test_cs",
Expand All @@ -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",
Expand Down Expand Up @@ -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_detect_attach_true(
self,
is_attach_enabled_mock,
mock_diagnostics,
):
is_attach_enabled_mock.return_value = True
_detect_attach()
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_detect_attach_false(
self,
is_attach_enabled_mock,
mock_diagnostics,
):
is_attach_enabled_mock.return_value = False
_detect_attach()
mock_diagnostics.warning.assert_not_called()
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def test_env_var_or_default_empty_with_defaults(self):
return_value=True,
)
def test_attach_enabled(self, mock_isdir):
_constants._IS_ON_APP_SERVICE = True
Comment thread
jeremydvoss marked this conversation as resolved.
Outdated
self.assertEqual(
_constants._is_attach_enabled(), True
)
Expand All @@ -143,7 +144,18 @@ def test_attach_enabled(self, mock_isdir):
"azure.monitor.opentelemetry._constants.isdir",
return_value=False,
)
def test_attach_disabled(self, mock_isdir):
def test_attach_app_service_disabled(self, mock_isdir):
_constants._IS_ON_APP_SERVICE = True
Comment thread
jeremydvoss marked this conversation as resolved.
Outdated
self.assertEqual(
_constants._is_attach_enabled(), False
)

@patch(
"azure.monitor.opentelemetry._constants.isdir",
return_value=True,
)
def test_attach_off_app_service_with_agent(self, mock_isdir):
_constants._IS_ON_APP_SERVICE = False
self.assertEqual(
_constants._is_attach_enabled(), False
)