Skip to content

Commit 0ea4200

Browse files
authored
Enable sampler for attach (#35218)
* Enable sampler for attach * Allow sampler to take None param * lint
1 parent 0832f65 commit 0ea4200

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

sdk/monitor/azure-monitor-opentelemetry-exporter/azure/monitor/opentelemetry/exporter/export/trace/_sampling.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ def azure_monitor_opentelemetry_sampler_factory(sampler_argument): # pylint: di
9595
try:
9696
rate = float(sampler_argument)
9797
return ApplicationInsightsSampler(rate)
98-
except ValueError:
98+
except (ValueError, TypeError):
9999
return ApplicationInsightsSampler()

sdk/monitor/azure-monitor-opentelemetry-exporter/tests/trace/test_sampling.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55

66
from azure.monitor.opentelemetry.exporter.export.trace._sampling import (
77
ApplicationInsightsSampler,
8+
azure_monitor_opentelemetry_sampler_factory,
89
)
910

1011
# pylint: disable=protected-access
1112
class TestApplicationInsightsSampler(unittest.TestCase):
1213

1314
def test_constructor(self):
15+
sampler = ApplicationInsightsSampler()
16+
self.assertEqual(sampler._ratio, 1.0)
17+
self.assertEqual(sampler._sample_rate, 100)
18+
19+
def test_constructor_ratio(self):
1420
sampler = ApplicationInsightsSampler(0.75)
1521
self.assertEqual(sampler._ratio, 0.75)
1622
self.assertEqual(sampler._sample_rate, 75)
@@ -38,3 +44,15 @@ def test_should_sample_not_sampled(self, score_mock):
3844
result = sampler.should_sample(None, 0, "test")
3945
self.assertEqual(result.attributes["_MS.sampleRate"], 50)
4046
self.assertFalse(result.decision.is_sampled())
47+
48+
def test_sampler_factory(self):
49+
sampler = azure_monitor_opentelemetry_sampler_factory("1.0")
50+
self.assertEqual(sampler._ratio, 1.0)
51+
52+
def test_sampler_factory_none(self):
53+
sampler = azure_monitor_opentelemetry_sampler_factory(None)
54+
self.assertEqual(sampler._ratio, 1.0)
55+
56+
def test_sampler_factory_empty(self):
57+
sampler = azure_monitor_opentelemetry_sampler_factory("")
58+
self.assertEqual(sampler._ratio, 1.0)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Features Added
66

7+
- Enable sampling for attach
8+
([#35218](https://github.com/Azure/azure-sdk-for-python/pull/35218))
9+
710
### Breaking Changes
811

912
### Bugs Fixed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from opentelemetry.sdk.environment_variables import (
1818
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED,
1919
OTEL_EXPERIMENTAL_RESOURCE_DETECTORS,
20+
OTEL_TRACES_SAMPLER,
2021
)
2122

2223
from azure.core.settings import settings
@@ -70,6 +71,9 @@ def _configure_auto_instrumentation() -> None:
7071
environ.setdefault(
7172
OTEL_LOGS_EXPORTER, "azure_monitor_opentelemetry_exporter"
7273
)
74+
environ.setdefault(
75+
OTEL_TRACES_SAMPLER, "azure_monitor_opentelemetry_sampler"
76+
)
7377
environ.setdefault(
7478
_OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED, "true"
7579
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def _setup_instrumentations(configurations: Dict[str, ConfigurationValue]):
168168
continue
169169
try:
170170
# Check if dependent libraries/version are installed
171-
conflict = get_dist_dependency_conflicts(entry_point.dist)
171+
conflict = get_dist_dependency_conflicts(entry_point.dist) # type: ignore
172172
if conflict:
173173
_logger.debug(
174174
"Skipping instrumentation %s: %s",

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from os import environ
12
import warnings
23
from unittest import TestCase
34
from unittest.mock import patch
@@ -13,6 +14,7 @@
1314

1415

1516
class TestDistro(TestCase):
17+
@patch.dict("os.environ", {}, clear=True)
1618
@patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=True)
1719
@patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings")
1820
@patch(
@@ -30,8 +32,56 @@ def test_configure(self, mock_diagnostics, azure_core_mock, attach_mock):
3032
self.assertEqual(
3133
azure_core_mock.tracing_implementation, OpenTelemetrySpan
3234
)
35+
self.assertEqual(
36+
environ,
37+
{
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",
42+
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "true",
43+
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "azure_app_service",
44+
}
45+
)
46+
47+
@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",
52+
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "false",
53+
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "custom_resource_detector",
54+
}, clear=True)
55+
@patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=True)
56+
@patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings")
57+
@patch(
58+
"azure.monitor.opentelemetry._autoinstrumentation.distro.AzureDiagnosticLogging"
59+
)
60+
def test_configure_env_vars_set(self, mock_diagnostics, azure_core_mock, attach_mock):
61+
distro = AzureMonitorDistro()
62+
with warnings.catch_warnings():
63+
warnings.simplefilter("error")
64+
distro.configure()
65+
mock_diagnostics.info.assert_called_once_with(
66+
"Azure Monitor OpenTelemetry Distro configured successfully.",
67+
_ATTACH_SUCCESS_DISTRO
68+
)
69+
self.assertEqual(
70+
azure_core_mock.tracing_implementation, OpenTelemetrySpan
71+
)
72+
self.assertEqual(
73+
environ,
74+
{
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",
79+
"OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED": "false",
80+
"OTEL_EXPERIMENTAL_RESOURCE_DETECTORS": "custom_resource_detector",
81+
}
82+
)
3383

34-
@patch.dict("os.environ", {"OTEL_PYTHON_DISABLED_INSTRUMENTATIONS": " flask,azure_sdk , urllib3"})
84+
@patch.dict("os.environ", {"OTEL_PYTHON_DISABLED_INSTRUMENTATIONS": " flask,azure_sdk , urllib3"}, clear=True)
3585
@patch("azure.monitor.opentelemetry._autoinstrumentation.distro._is_attach_enabled", return_value=True)
3686
@patch("azure.monitor.opentelemetry._autoinstrumentation.distro.settings")
3787
@patch(

0 commit comments

Comments
 (0)