Skip to content

Commit 8c581a2

Browse files
authored
Autoinstrumentation rework (#35890)
* Configure exporters and sampler directly in configurator * lint
1 parent d0fcdff commit 8c581a2

File tree

8 files changed

+109
-44
lines changed

8 files changed

+109
-44
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Release History
22

3-
## 1.5.1 (Unreleased)
3+
## 1.6.0 (Unreleased)
44

55
### Features Added
66

7+
- Rework autoinstrumentation: Configure exporters and samplers directly
8+
([#35890](https://github.com/Azure/azure-sdk-for-python/pull/35890))
9+
710
### Breaking Changes
811

912
### Bugs Fixed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,25 @@
55
# --------------------------------------------------------------------------
66

77

8+
from os import environ
89
from warnings import warn
910

11+
from opentelemetry.environment_variables import (
12+
OTEL_LOGS_EXPORTER,
13+
OTEL_METRICS_EXPORTER,
14+
OTEL_TRACES_EXPORTER,
15+
)
1016
from opentelemetry.sdk._configuration import _OTelSDKConfigurator
1117

18+
from azure.monitor.opentelemetry.exporter import ApplicationInsightsSampler # pylint: disable=import-error,no-name-in-module
1219
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
20+
from azure.monitor.opentelemetry._constants import (
21+
_PREVIEW_ENTRY_POINT_WARNING,
22+
LOG_EXPORTER_NAMES_ARG,
23+
METRIC_EXPORTER_NAMES_ARG,
24+
SAMPLER_ARG,
25+
TRACE_EXPORTER_NAMES_ARG,
26+
)
1427
from azure.monitor.opentelemetry._diagnostics.diagnostic_logging import (
1528
AzureDiagnosticLogging,
1629
_ATTACH_FAILURE_CONFIGURATOR,
@@ -26,6 +39,18 @@ def _configure(self, **kwargs):
2639
if not _is_attach_enabled():
2740
warn(_PREVIEW_ENTRY_POINT_WARNING)
2841
try:
42+
if environ.get(OTEL_TRACES_EXPORTER, "").lower().strip() != "none":
43+
kwargs.setdefault(TRACE_EXPORTER_NAMES_ARG, ["azure-monitor-opentelemetry-exporter"])
44+
try:
45+
sample_rate = float(environ.get("OTEL_TRACES_SAMPLER_ARG", 1.0))
46+
except ValueError:
47+
sample_rate = 1.0
48+
kwargs.setdefault(SAMPLER_ARG, ApplicationInsightsSampler(sample_rate))
49+
if environ.get(OTEL_METRICS_EXPORTER, "").lower().strip() != "none":
50+
kwargs.setdefault(METRIC_EXPORTER_NAMES_ARG, ["azure-monitor-opentelemetry-exporter"])
51+
if environ.get(OTEL_LOGS_EXPORTER, "").lower().strip() != "none":
52+
kwargs.setdefault(LOG_EXPORTER_NAMES_ARG, ["azure-monitor-opentelemetry-exporter"])
53+
# As of OTel SDK 1.25.0, exporters passed as kwargs will be added to those specified in env vars.
2954
super()._configure(**kwargs)
3055
AzureStatusLogger.log_status(True)
3156
AzureDiagnosticLogging.info(

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66
from os import environ
77
from warnings import warn
88

9-
from opentelemetry.environment_variables import (
10-
OTEL_LOGS_EXPORTER,
11-
OTEL_METRICS_EXPORTER,
12-
OTEL_TRACES_EXPORTER,
13-
)
149
from opentelemetry.instrumentation.distro import ( # type: ignore
1510
BaseDistro,
1611
)
1712
from opentelemetry.sdk.environment_variables import (
1813
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED,
1914
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS,
20-
OTEL_TRACES_SAMPLER,
2115
)
2216

2317
from azure.core.settings import settings
@@ -62,18 +56,6 @@ def _configure(self, **kwargs) -> None:
6256

6357

6458
def _configure_auto_instrumentation() -> None:
65-
environ.setdefault(
66-
OTEL_METRICS_EXPORTER, "azure_monitor_opentelemetry_exporter"
67-
)
68-
environ.setdefault(
69-
OTEL_TRACES_EXPORTER, "azure_monitor_opentelemetry_exporter"
70-
)
71-
environ.setdefault(
72-
OTEL_LOGS_EXPORTER, "azure_monitor_opentelemetry_exporter"
73-
)
74-
environ.setdefault(
75-
OTEL_TRACES_SAMPLER, "azure_monitor_opentelemetry_sampler"
76-
)
7759
environ.setdefault(
7860
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "true"
7961
)

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
_AZURE_MONITOR_DISTRO_VERSION_ARG,
99
)
1010

11-
# --------------------Configuration------------------------------------------
11+
# --------------------Distro Configuration------------------------------------------
1212

1313
CONNECTION_STRING_ARG = "connection_string"
1414
ENABLE_LIVE_METRICS_ARG = "enable_live_metrics"
@@ -24,6 +24,14 @@
2424
SPAN_PROCESSORS_ARG = "span_processors"
2525

2626

27+
# --------------------Autoinstrumentation Configuration------------------------------------------
28+
29+
LOG_EXPORTER_NAMES_ARG = "log_exporter_names"
30+
METRIC_EXPORTER_NAMES_ARG = "metric_exporter_names"
31+
SAMPLER_ARG = "sampler"
32+
TRACE_EXPORTER_NAMES_ARG = "trace_exporter_names"
33+
34+
2735
# --------------------Diagnostic/status logging------------------------------
2836

2937
_LOG_PATH_LINUX = "/var/log/applicationinsights"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# license information.
55
# --------------------------------------------------------------------------
66

7-
VERSION = "1.5.1"
7+
VERSION = "1.6.0"

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@
8888
"azure-core<2.0.0,>=1.28.0",
8989
"azure-core-tracing-opentelemetry~=1.0.0b11",
9090
"azure-monitor-opentelemetry-exporter~=1.0.0b26",
91-
"opentelemetry-instrumentation-django~=0.42b0",
92-
"opentelemetry-instrumentation-fastapi~=0.42b0",
93-
"opentelemetry-instrumentation-flask~=0.42b0",
94-
"opentelemetry-instrumentation-psycopg2~=0.42b0",
95-
"opentelemetry-instrumentation-requests~=0.42b0",
96-
"opentelemetry-instrumentation-urllib~=0.42b0",
97-
"opentelemetry-instrumentation-urllib3~=0.42b0",
91+
"opentelemetry-instrumentation-django~=0.46b0",
92+
"opentelemetry-instrumentation-fastapi~=0.46b0",
93+
"opentelemetry-instrumentation-flask~=0.46b0",
94+
"opentelemetry-instrumentation-psycopg2~=0.46b0",
95+
"opentelemetry-instrumentation-requests~=0.46b0",
96+
"opentelemetry-instrumentation-urllib~=0.46b0",
97+
"opentelemetry-instrumentation-urllib3~=0.46b0",
9898
"opentelemetry-resource-detector-azure~=0.1.4",
99+
"opentelemetry-sdk~=1.25",
99100
],
100101
entry_points={
101102
"opentelemetry_distro": [

sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_configurator.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,79 @@
1212
)
1313

1414

15+
@patch.dict("os.environ", {}, clear=True)
1516
class TestConfigurator(TestCase):
17+
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.super")
18+
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.ApplicationInsightsSampler")
1619
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True)
1720
@patch(
1821
"azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging"
1922
)
20-
def test_configure(self, mock_diagnostics, attach_mock):
23+
def test_configure(self, mock_diagnostics, attach_mock, sampler_mock, super_mock):
24+
sampler_mock.return_value = "TEST_SAMPLER"
2125
configurator = AzureMonitorConfigurator()
2226
with warnings.catch_warnings():
2327
warnings.simplefilter("error")
24-
configurator._configure()
28+
configurator._configure(auto_instrumentation_version="TEST_VERSION")
29+
sampler_mock.assert_called_once_with(1.0)
30+
super_mock()._configure.assert_called_once_with(
31+
auto_instrumentation_version="TEST_VERSION",
32+
trace_exporter_names=["azure-monitor-opentelemetry-exporter"],
33+
metric_exporter_names=["azure-monitor-opentelemetry-exporter"],
34+
log_exporter_names=["azure-monitor-opentelemetry-exporter"],
35+
sampler="TEST_SAMPLER",
36+
)
2537
mock_diagnostics.info.assert_called_once_with(
2638
"Azure Monitor Configurator configured successfully.",
2739
_ATTACH_SUCCESS_CONFIGURATOR
2840
)
2941

42+
43+
@patch.dict("os.environ", {"OTEL_TRACES_SAMPLER_ARG": "0.5"}, clear=True)
44+
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.super")
45+
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.ApplicationInsightsSampler")
46+
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=True)
47+
@patch(
48+
"azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging"
49+
)
50+
def test_configure_sampler_arg(self, mock_diagnostics, attach_mock, sampler_mock, super_mock):
51+
sampler_mock.return_value = "TEST_SAMPLER"
52+
configurator = AzureMonitorConfigurator()
53+
with warnings.catch_warnings():
54+
warnings.simplefilter("error")
55+
configurator._configure(auto_instrumentation_version="TEST_VERSION")
56+
sampler_mock.assert_called_once_with(0.5)
57+
super_mock()._configure.assert_called_once_with(
58+
auto_instrumentation_version="TEST_VERSION",
59+
trace_exporter_names=["azure-monitor-opentelemetry-exporter"],
60+
metric_exporter_names=["azure-monitor-opentelemetry-exporter"],
61+
log_exporter_names=["azure-monitor-opentelemetry-exporter"],
62+
sampler="TEST_SAMPLER",
63+
)
64+
mock_diagnostics.info.assert_called_once_with(
65+
"Azure Monitor Configurator configured successfully.",
66+
_ATTACH_SUCCESS_CONFIGURATOR
67+
)
68+
69+
70+
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.super")
71+
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator.ApplicationInsightsSampler")
3072
@patch("azure.monitor.opentelemetry._autoinstrumentation.configurator._is_attach_enabled", return_value=False)
3173
@patch(
3274
"azure.monitor.opentelemetry._autoinstrumentation.configurator.AzureDiagnosticLogging"
3375
)
34-
def test_configure_preview(self, mock_diagnostics, attach_mock):
76+
def test_configure_preview(self, mock_diagnostics, attach_mock, sampler_mock, super_mock):
77+
sampler_mock.return_value = "TEST_SAMPLER"
3578
configurator = AzureMonitorConfigurator()
3679
with self.assertWarns(Warning):
3780
configurator._configure()
81+
sampler_mock.assert_called_once_with(1.0)
82+
super_mock()._configure.assert_called_once_with(
83+
trace_exporter_names=["azure-monitor-opentelemetry-exporter"],
84+
metric_exporter_names=["azure-monitor-opentelemetry-exporter"],
85+
log_exporter_names=["azure-monitor-opentelemetry-exporter"],
86+
sampler="TEST_SAMPLER",
87+
)
3888
mock_diagnostics.info.assert_called_once_with(
3989
"Azure Monitor Configurator configured successfully.",
4090
_ATTACH_SUCCESS_CONFIGURATOR

sdk/monitor/azure-monitor-opentelemetry/tests/autoinstrumentation/test_distro.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,16 @@ def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock):
3535
self.assertEqual(
3636
environ,
3737
{
38-
"OTEL_METRICS_EXPORTER": "azure_monitor_opentelemetry_exporter",
39-
"OTEL_TRACES_EXPORTER": "azure_monitor_opentelemetry_exporter",
40-
"OTEL_LOGS_EXPORTER": "azure_monitor_opentelemetry_exporter",
41-
"OTEL_TRACES_SAMPLER": "azure_monitor_opentelemetry_sampler",
4238
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "true",
4339
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "azure_app_service",
4440
}
4541
)
4642

4743
@patch.dict("os.environ", {
48-
"OTEL_METRICS_EXPORTER": "custom_metrics_exporter",
49-
"OTEL_TRACES_EXPORTER": "custom_traces_exporter",
50-
"OTEL_LOGS_EXPORTER": "custom_logs_exporter",
51-
"OTEL_TRACES_SAMPLER": "custom_traces_sampler",
44+
# "OTEL_METRICS_EXPORTER": "custom_metrics_exporter",
45+
# "OTEL_TRACES_EXPORTER": "custom_traces_exporter",
46+
# "OTEL_LOGS_EXPORTER": "custom_logs_exporter",
47+
# "OTEL_TRACES_SAMPLER": "custom_traces_sampler",
5248
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "false",
5349
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "custom_resource_detector",
5450
}, clear=True)
@@ -72,10 +68,10 @@ def test_configure_env_vars_set(self, mock_diagnostics, azure_core_mock, attach_
7268
self.assertEqual(
7369
environ,
7470
{
75-
"OTEL_METRICS_EXPORTER": "custom_metrics_exporter",
76-
"OTEL_TRACES_EXPORTER": "custom_traces_exporter",
77-
"OTEL_LOGS_EXPORTER": "custom_logs_exporter",
78-
"OTEL_TRACES_SAMPLER": "custom_traces_sampler",
71+
# "OTEL_METRICS_EXPORTER": "custom_metrics_exporter",
72+
# "OTEL_TRACES_EXPORTER": "custom_traces_exporter",
73+
# "OTEL_LOGS_EXPORTER": "custom_logs_exporter",
74+
# "OTEL_TRACES_SAMPLER": "custom_traces_sampler",
7975
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "false",
8076
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "custom_resource_detector",
8177
}

0 commit comments

Comments
 (0)