Skip to content

Commit 988e0eb

Browse files
committed
Raise AttributeError (not ImportError) from ydb.opentelemetry.__getattr__
Module __getattr__ raising ImportError broke hasattr()/getattr(default) introspection of OtelTracingProvider when OpenTelemetry is absent. Raise AttributeError instead, keeping the install hint in the message; enable_tracing() still surfaces its own ImportError guidance.
1 parent 2ed85cb commit 988e0eb

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

tests/tracing/test_observability_enable.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,22 @@ def test_disable_is_noop_when_plugin_import_fails(self, monkeypatch):
535535
# Must not raise — silent fallback is documented behavior.
536536
otel_disable()
537537

538+
def test_provider_access_raises_attributeerror_when_plugin_import_fails(self, monkeypatch):
539+
import sys
540+
541+
monkeypatch.setitem(sys.modules, "ydb.opentelemetry.plugin", None)
542+
543+
import ydb.opentelemetry as otel
544+
545+
# AttributeError (not ImportError) keeps introspection sane...
546+
assert hasattr(otel, "OtelTracingProvider") is False
547+
sentinel = object()
548+
assert getattr(otel, "OtelTracingProvider", sentinel) is sentinel
549+
550+
# ...while the install hint still rides along on direct access.
551+
with pytest.raises(AttributeError, match="OpenTelemetry"):
552+
otel.OtelTracingProvider
553+
538554

539555
class TestOtelTracingSpanBridge:
540556
"""Cover the ``TracingSpan`` wrapper directly (no SDK glue)."""

ydb/opentelemetry/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ def disable_tracing():
4040

4141
def __getattr__(name):
4242
# Lazily expose the OTel provider so ``from ydb.opentelemetry import
43-
# OtelTracingProvider`` works without importing ``opentelemetry`` at
44-
# module load time.
43+
# OtelTracingProvider`` works without importing ``opentelemetry`` at module
44+
# load time. When OTel is missing, raise AttributeError (not ImportError) so
45+
# hasattr()/getattr(..., default) introspection behaves normally; the install
46+
# hint rides along in the message, and enable_tracing() keeps its own
47+
# ImportError guidance for the primary entrypoint.
4548
if name == "OtelTracingProvider":
4649
try:
4750
from ydb.opentelemetry.plugin import OtelTracingProvider
4851
except ImportError:
49-
raise ImportError(
50-
"OpenTelemetry packages are required for tracing support. "
52+
raise AttributeError(
53+
"OtelTracingProvider requires the OpenTelemetry packages. "
5154
"Install them with: pip install ydb[opentelemetry]"
5255
) from None
5356
return OtelTracingProvider

0 commit comments

Comments
 (0)