1+ import importlib
2+ from types import ModuleType
3+
4+ import pytest
5+ from fastapi import FastAPI
6+
7+ from app .core import config as cfg
8+ from app .utils import observability as obs
9+
10+
11+ @pytest .mark .parametrize ("proto" , ["grpc" , "http" ])
12+ def test_otlp_protocol_switch (monkeypatch , proto ):
13+ """configure_observability chooses correct exporter for both protocols."""
14+
15+ # Ensure endpoint & flags are enabled
16+ monkeypatch .setattr (cfg .settings , "OTEL_EXPORTER_OTLP_ENDPOINT" , "http://collector:4318" , raising = False )
17+ monkeypatch .setattr (cfg .settings , "OTEL_EXPORTER_OTLP_PROTOCOL" , proto , raising = False )
18+ monkeypatch .setattr (cfg .settings , "OTEL_TRACES_ENABLED" , True , raising = False )
19+
20+ # Reset observability internal flag
21+ obs ._otel_instrumented = False # type: ignore[attr-defined]
22+
23+ app = FastAPI ()
24+
25+ try :
26+ obs .configure_observability (app )
27+ except Exception as exc : # pragma: no cover
28+ pytest .fail (f"configure_observability raised unexpected exception: { exc } " )
29+
30+ # If OpenTelemetry libs missing in test env, _OTEL_READY will be False – skip assertion.
31+ if not obs ._OTEL_READY : # type: ignore[attr-defined]
32+ pytest .skip ("OpenTelemetry libraries not available in test environment" )
33+
34+ assert obs ._otel_instrumented # type: ignore[attr-defined]
0 commit comments