Skip to content

Commit 394dc9a

Browse files
authored
Adding diagnostic log for distro + attach (Azure#34971)
* Adding diagnostic log for distro + attach * changelog * lint * lint * Update message * lint * feedback * Remove unused tests * leaving attach change for next pr
1 parent d48629e commit 394dc9a

File tree

8 files changed

+54
-29
lines changed

8 files changed

+54
-29
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Features Added
66

7+
- Adding diagnostic warning when distro detects RP attach
8+
([#34971](https://github.com/Azure/azure-sdk-for-python/pull/34971))
9+
710
### Breaking Changes
811

912
### Bugs Fixed

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/configurator.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010
from opentelemetry.sdk._configuration import _OTelSDKConfigurator
1111

12-
from azure.monitor.opentelemetry._constants import (
13-
_is_attach_enabled,
14-
_PREVIEW_ENTRY_POINT_WARNING,
15-
)
12+
from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module
13+
from azure.monitor.opentelemetry._constants import _PREVIEW_ENTRY_POINT_WARNING
1614
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import (
1715
AzureDiagnosticLogging,
1816
_ATTACH_FAILURE_CONFIGURATOR,

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_autoinstrumentation/distro.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
from azure.core.settings import settings
2323
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
24+
from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module
2425
from azure.monitor.opentelemetry._constants import (
25-
_is_attach_enabled,
2626
_AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME,
2727
_AZURE_SDK_INSTRUMENTATION_NAME,
2828
_PREVIEW_ENTRY_POINT_WARNING,

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

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
AzureMonitorMetricExporter,
4545
AzureMonitorTraceExporter,
4646
)
47+
from azure.monitor.opentelemetry.exporter._utils import _is_attach_enabled # pylint: disable=import-error,no-name-in-module
48+
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import (
49+
_DISTRO_DETECTS_ATTACH,
50+
AzureDiagnosticLogging,
51+
)
4752
from azure.monitor.opentelemetry._util.configurations import (
4853
_get_configurations,
4954
_is_instrumentation_enabled,
@@ -76,6 +81,8 @@ def configure_azure_monitor(**kwargs) -> None: # pylint: disable=C4758
7681
:rtype: None
7782
"""
7883

84+
_send_attach_warning()
85+
7986
configurations = _get_configurations(**kwargs)
8087

8188
disable_tracing = configurations[DISABLE_TRACING_ARG]
@@ -177,3 +184,11 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
177184
lib_name,
178185
exc_info=ex,
179186
)
187+
188+
189+
def _send_attach_warning():
190+
if _is_attach_enabled():
191+
AzureDiagnosticLogging.warning(
192+
"Distro detected that automatic attach may have occurred. Check your data to ensure "
193+
"that telemetry is not being duplicated. This may impact your cost.",
194+
_DISTRO_DETECTS_ATTACH)

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

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

1312
from azure.monitor.opentelemetry.exporter._connection_string_parser import ( # pylint: disable=import-error,no-name-in-module
@@ -99,10 +98,5 @@ def _env_var_or_default(var_name, default_val=""):
9998
_PREVIEW_INSTRUMENTED_LIBRARIES = ()
10099
_ALL_SUPPORTED_INSTRUMENTED_LIBRARIES = _FULLY_SUPPORTED_INSTRUMENTED_LIBRARIES + _PREVIEW_INSTRUMENTED_LIBRARIES
101100

102-
# Autoinstrumentation
103-
104-
def _is_attach_enabled():
105-
return isdir("/agents/python/")
106-
107101
_AZURE_APP_SERVICE_RESOURCE_DETECTOR_NAME = "azure_app_service"
108102
_AZURE_VM_RESOURCE_DETECTOR_NAME = "azure_vm"

sdk/monitor/azure-monitor-opentelemetry/azure/monitor/opentelemetry/_diagnostics/diagnostic_logging.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
_logger.propagate = False
2929
_logger.setLevel(logging.INFO)
3030
_DIAGNOSTIC_LOG_PATH = _get_log_path()
31+
_DISTRO_DETECTS_ATTACH = "4100"
3132
_ATTACH_SUCCESS_DISTRO = "4200"
3233
_ATTACH_SUCCESS_CONFIGURATOR = "4201"
3334
_ATTACH_FAILURE_DISTRO = "4400"

sdk/monitor/azure-monitor-opentelemetry/tests/configuration/test_configure.py

+32
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@
1818

1919
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan
2020
from azure.monitor.opentelemetry._configure import (
21+
_send_attach_warning,
2122
_setup_instrumentations,
2223
_setup_logging,
2324
_setup_metrics,
2425
_setup_tracing,
2526
configure_azure_monitor,
2627
)
28+
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import _DISTRO_DETECTS_ATTACH
2729

2830

2931
TEST_RESOURCE = Resource({"foo": "bar"})
3032

3133

3234
class TestConfigure(unittest.TestCase):
35+
@patch(
36+
"azure.monitor.opentelemetry._configure._send_attach_warning",
37+
)
3338
@patch(
3439
"azure.monitor.opentelemetry._configure._setup_instrumentations",
3540
)
@@ -48,6 +53,7 @@ def test_configure_azure_monitor(
4853
logging_mock,
4954
metrics_mock,
5055
instrumentation_mock,
56+
detect_attach_mock,
5157
):
5258
kwargs = {
5359
"connection_string": "test_cs",
@@ -57,6 +63,7 @@ def test_configure_azure_monitor(
5763
logging_mock.assert_called_once()
5864
metrics_mock.assert_called_once()
5965
instrumentation_mock.assert_called_once()
66+
detect_attach_mock.assert_called_once()
6067

6168
@patch(
6269
"azure.monitor.opentelemetry._configure._setup_instrumentations",
@@ -464,3 +471,28 @@ def test_setup_instrumentations_disabled(
464471
ep2_mock.load.assert_called_once()
465472
instrumentor_mock.instrument.assert_called_once()
466473
logger_mock.debug.assert_called_once()
474+
475+
@patch("azure.monitor.opentelemetry._configure.AzureDiagnosticLogging")
476+
@patch("azure.monitor.opentelemetry._configure._is_attach_enabled")
477+
def test_send_attach_warning_true(
478+
self,
479+
is_attach_enabled_mock,
480+
mock_diagnostics,
481+
):
482+
is_attach_enabled_mock.return_value = True
483+
_send_attach_warning()
484+
mock_diagnostics.warning.assert_called_once_with(
485+
"Distro detected that automatic attach may have occurred. Check your data to ensure that telemetry is not being duplicated. This may impact your cost.",
486+
_DISTRO_DETECTS_ATTACH,
487+
)
488+
489+
@patch("azure.monitor.opentelemetry._configure.AzureDiagnosticLogging")
490+
@patch("azure.monitor.opentelemetry._configure._is_attach_enabled")
491+
def test_send_attach_warning_false(
492+
self,
493+
is_attach_enabled_mock,
494+
mock_diagnostics,
495+
):
496+
is_attach_enabled_mock.return_value = False
497+
_send_attach_warning()
498+
mock_diagnostics.warning.assert_not_called()

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

-18
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,3 @@ def test_env_var_or_default_empty_with_defaults(self):
129129
self.assertEqual(
130130
_constants._env_var_or_default("key", default_val="value"), "value"
131131
)
132-
133-
@patch(
134-
"azure.monitor.opentelemetry._constants.isdir",
135-
return_value=True,
136-
)
137-
def test_attach_enabled(self, mock_isdir):
138-
self.assertEqual(
139-
_constants._is_attach_enabled(), True
140-
)
141-
142-
@patch(
143-
"azure.monitor.opentelemetry._constants.isdir",
144-
return_value=False,
145-
)
146-
def test_attach_disabled(self, mock_isdir):
147-
self.assertEqual(
148-
_constants._is_attach_enabled(), False
149-
)

0 commit comments

Comments
 (0)