Skip to content

Commit

Permalink
Adding diagnostic log for distro + attach (Azure#34971)
Browse files Browse the repository at this point in the history
* Adding diagnostic log for distro + attach

* changelog

* lint

* lint

* Update message

* lint

* feedback

* Remove unused tests

* leaving attach change for next pr
  • Loading branch information
jeremydvoss authored Apr 2, 2024
1 parent d48629e commit 394dc9a
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 29 deletions.
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 # pylint: disable=import-error,no-name-in-module
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 # pylint: disable=import-error,no-name-in-module
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,11 @@
AzureMonitorMetricExporter,
AzureMonitorTraceExporter,
)
from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module
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
"""

_send_attach_warning()

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 _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 @@ -7,7 +7,6 @@
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,no-name-in-module
Expand Down Expand Up @@ -99,10 +98,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()
18 changes: 0 additions & 18 deletions sdk/monitor/azure-monitor-opentelemetry/tests/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,3 @@ 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
)

0 comments on commit 394dc9a

Please sign in to comment.