Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def _is_on_functions():
return environ.get(_FUNCTIONS_WORKER_RUNTIME) is not None

def _is_attach_enabled():
return isdir("/agents/python/")
if _is_on_app_service():
return isdir("/agents/python/")
return False


# AKS
Expand Down
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 @@ -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
from azure.monitor.opentelemetry._constants import _PREVIEW_ENTRY_POINT_WARNING
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import (
AzureDiagnosticLogging,
_ATTACH_FAILURE_CONFIGURATOR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
from azure.monitor.opentelemetry._constants import (
_is_attach_enabled,
_AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME,
_AZURE_SDK_INSTRUMENTATION_NAME,
_PREVIEW_ENTRY_POINT_WARNING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
AzureMonitorMetricExporter,
AzureMonitorTraceExporter,
)
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import (
_DISTRO_DETECTS_ATTACH,
AzureDiagnosticLogging,
)
from azure.monitor.opentelemetry.exporter._utils import (
_is_attach_enabled,
)
from azure.monitor.opentelemetry._util.configurations import (
_get_configurations,
_is_instrumentation_enabled,
Expand Down Expand Up @@ -76,6 +83,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]
Expand Down Expand Up @@ -177,3 +186,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)
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,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"
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 (
_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",
)
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_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()
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
)