Skip to content

Commit 2b9c7c1

Browse files
ericapisaniclaude
andauthored
fix(tests): Fix flaky OTel propagator entry point test (#6655)
Related flaky test run: https://github.com/getsentry/sentry-python/actions/runs/28110662230/job/83239750332 ## Summary - Fix flaky `test_propagator_loaded_if_mentioned_in_environment_variable` in the `potel` test suite - The old test used `importlib.reload(propagate)` with a patched env var, which was flaky because OTel's `_importlib_metadata` caches `entry_points()` with `@functools.cache` - Replace with a direct `importlib.metadata.entry_points()` query that verifies registration, value, and loaded class --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e7262e9 commit 2b9c7c1

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
import importlib
2-
import os
3-
from unittest.mock import patch
4-
5-
from opentelemetry import propagate
1+
try:
2+
from importlib.metadata import entry_points
3+
except ImportError:
4+
from importlib_metadata import entry_points
65

76
from sentry_sdk.integrations.opentelemetry import SentryPropagator
87

98

10-
def test_propagator_loaded_if_mentioned_in_environment_variable():
11-
try:
12-
with patch.dict(os.environ, {"OTEL_PROPAGATORS": "sentry"}):
13-
importlib.reload(propagate)
9+
def test_propagator_registered_as_entry_point():
10+
all_eps = entry_points()
11+
12+
if isinstance(all_eps, dict):
13+
# Python 3.7-3.8 stdlib returns a dict keyed by group
14+
matches = [
15+
ep
16+
for ep in all_eps.get("opentelemetry_propagator", [])
17+
if ep.name == "sentry"
18+
]
19+
else:
20+
matches = list(all_eps.select(group="opentelemetry_propagator", name="sentry"))
21+
22+
assert len(matches) >= 1
23+
assert matches[0].value == "sentry_sdk.integrations.opentelemetry:SentryPropagator"
1424

15-
assert len(propagate.propagators) == 1
16-
assert isinstance(propagate.propagators[0], SentryPropagator)
17-
finally:
18-
importlib.reload(propagate)
25+
loaded = matches[0].load()
26+
assert loaded is SentryPropagator

0 commit comments

Comments
 (0)