litellm/tests/unit/integrations/otel/test_otel_v2_mount.py
yuneng-jiang cf491d1df9
test: move tests/test_litellm integrations and secret_managers into tests/unit (#43194)
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: move tests/test_litellm/llms into tests/unit/llms

Rename-only. Moves the provider tests and the fine-tuning fixtures they
load, mirroring the old paths. Follow-up commits merge, split and wire them.

* test: merge, split and prune the moved llms tests

Merges the Databricks chat transformation tests into the existing unit
file, keeps the tests that need real keys or the network in
tests/test_litellm, deletes the audited tests a stronger unit test
already covers, and points imports at tests.unit.llms.

* ci: run the moved llms tests under their legacy flags

The Vertex AI and All Other Providers shards keep their legacy test-path
for the retained files and add the llm-vertex-ai and llm-other-providers
unit selections. CircleCI gets matching unit jobs.

* test: make the tests/unit/llms directories packages

Adds __init__.py to the moved dirs and drops the legacy ones whose
directories no longer hold tests.

* test: drop script runners and path hacks the llms split left dangling

The __main__ runners in the split openai_like files and the Databricks e2e
runner called tests that now live in the other half of the split or were
deleted. The retained legacy halves also no longer need sys.path edits.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

* test: move tests/test_litellm integrations and secret_managers into tests/unit

Rename-only. Mirrors the old paths, including the directory conftests
and the prompt and JSON fixtures. Follow-up commits prune and wire them.

* test: prune and repoint the moved integrations tests

Deletes the 7 audited tests a stronger test in the same tree already
covers, imports the TLS sink helpers from their new conftest path, and
restores os.environ after each integrations test. Some presets write
OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the
legacy tree's test ordering that header leaked into the AgentOps tests.

* ci: run the moved integrations tests under their legacy flag

The integrations GHA shard and a new CircleCI job run the integrations
unit selection. secret_managers joins the misc selection.

* docs: point integrations and secret_managers references at tests/unit

* test: make the moved integrations directories packages

* test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path

The Databricks e2e file is a manual script whose main() calls the tests
that were pruned, so pruning them broke the documented run. It is back to
its main version. The SageMaker Nova docstring now points at the file's
real location in tests/local_testing.

* test: keep the job's UNIT_FLAG out of the shard-script tests

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 12:57:07 -07:00

256 lines
9.2 KiB
Python

"""V2 entrypoint: the FastAPI instrumentation proxy_server mounts at app creation
(gated by LITELLM_OTEL_V2). The mount logic lives in
``litellm.integrations.otel.mount``; this exercises both that module's public
surface and the server-span + shared-provider behavior it produces.
"""
from datetime import datetime, timezone
import pytest
pytest.importorskip("opentelemetry")
pytest.importorskip("opentelemetry.instrumentation.fastapi")
fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient # noqa: E402
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor # noqa: E402
from opentelemetry.sdk.trace.export import SimpleSpanProcessor # noqa: E402
from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( # noqa: E402
InMemorySpanExporter,
)
from opentelemetry import trace # noqa: E402
from opentelemetry.trace import SpanKind # noqa: E402
from litellm.integrations.otel.model.config import ( # noqa: E402
OpenTelemetryV2Config,
is_otel_v2_enabled,
)
from litellm.integrations.otel.logger import OpenTelemetryV2 # noqa: E402
from litellm.integrations.otel.mount import ( # noqa: E402
PASSTHROUGH_PREFIXES,
_passthrough_span_name_hook,
instrument_fastapi_app,
)
from litellm.integrations.otel.plumbing.context import ( # noqa: E402
request_root_http_route,
set_request_root_span,
)
@pytest.fixture(autouse=True)
def _reset_request_root_span():
"""Clear the root-span anchor around every test. Production gets a fresh
contextvar copy per request task; the test process shares one context."""
from litellm.integrations.otel.plumbing import context as _otel_context
_otel_context._request_root_span.set(None)
_otel_context._mcp_message_transport_span.set(None)
yield
_otel_context._request_root_span.set(None)
_otel_context._mcp_message_transport_span.set(None)
@pytest.fixture(autouse=True)
def _clear_otel_v2_flag_cache():
is_otel_v2_enabled.cache_clear()
yield
is_otel_v2_enabled.cache_clear()
class _FakeSpan:
"""Minimal recording span capturing what the hook writes."""
def __init__(self, recording=True):
self._recording = recording
self.name = None
self.attributes = {}
def is_recording(self):
return self._recording
def update_name(self, name):
self.name = name
def set_attribute(self, key, value):
self.attributes[key] = value
def _instrumented_app():
"""Mirror proxy_server's startup mount: a logger builds the shared provider,
and the FastAPI instrumentor is attached to it."""
app = fastapi.FastAPI()
@app.get("/ping")
def ping():
return {"ok": True}
logger = OpenTelemetryV2(config=OpenTelemetryV2Config(exporter="in_memory"))
FastAPIInstrumentor.instrument_app(app, tracer_provider=logger._tracer_provider)
return app, logger
def test_gate_toggles_with_env(monkeypatch):
"""The startup mount is guarded by this flag."""
monkeypatch.delenv("LITELLM_OTEL_V2", raising=False)
is_otel_v2_enabled.cache_clear()
assert is_otel_v2_enabled() is False
monkeypatch.setenv("LITELLM_OTEL_V2", "1")
is_otel_v2_enabled.cache_clear()
assert is_otel_v2_enabled() is True
def test_instrumented_app_emits_server_span():
app, logger = _instrumented_app()
exporter = InMemorySpanExporter()
logger._tracer_provider.add_span_processor(SimpleSpanProcessor(exporter))
TestClient(app).get("/ping")
server_spans = [
s for s in exporter.get_finished_spans() if s.kind is SpanKind.SERVER
]
assert server_spans, "FastAPI instrumentor should emit a SERVER span per request"
attrs = server_spans[0].attributes or {}
assert any("route" in k or "method" in k for k in attrs)
def test_logger_and_instrumentor_share_provider():
"""Gen-ai spans (logger) and server spans (instrumentor) write to one provider."""
_, logger = _instrumented_app()
assert logger._emitter._tracer is logger.tracer
def test_passthrough_hook_renames_catch_all_span():
"""A passthrough route gets its span renamed to the real request path."""
span = _FakeSpan()
_passthrough_span_name_hook(
span, {"path": "/openai/v1/chat/completions", "method": "POST"}
)
assert span.name == "POST /openai/v1/chat/completions"
assert span.attributes["http.route"] == "/openai/v1/chat/completions"
def test_passthrough_hook_leaves_non_passthrough_route_unchanged():
"""A normal route keeps its low-cardinality template name (hook no-ops)."""
span = _FakeSpan()
_passthrough_span_name_hook(span, {"path": "/v1/models", "method": "GET"})
assert span.name is None
assert "http.route" not in span.attributes
def test_passthrough_hook_ignores_non_recording_span():
span = _FakeSpan(recording=False)
_passthrough_span_name_hook(
span, {"path": "/openai/v1/chat/completions", "method": "POST"}
)
assert span.name is None
def test_llm_span_route_is_read_off_the_server_span(monkeypatch):
"""``request_root_http_route`` answers with the SERVER span's own ``http.route``.
Driven through ``instrument_fastapi_app`` and the same
``create_litellm_proxy_request_started_span`` call the proxy makes per request,
so breaking either the mount or the anchor capture fails this."""
monkeypatch.setenv("LITELLM_OTEL_V2", "1")
is_otel_v2_enabled.cache_clear()
app = fastapi.FastAPI()
seen = {}
def _anchor_then_read(key):
logger.create_litellm_proxy_request_started_span(start_time=datetime.now(timezone.utc), headers=None)
seen[key] = request_root_http_route()
@app.post("/engines/{model:path}/chat/completions")
async def engines(model: str):
_anchor_then_read("templated")
return {}
@app.post("/openai/{endpoint:path}")
async def openai_passthrough(endpoint: str):
_anchor_then_read("passthrough")
return {}
logger = OpenTelemetryV2(config=OpenTelemetryV2Config(exporter="in_memory"))
exporter = InMemorySpanExporter()
logger._tracer_provider.add_span_processor(SimpleSpanProcessor(exporter))
# instrument_fastapi_app passes no provider, so it binds to the OTel global the
# way the proxy does once proxy_startup_event publishes one. set_tracer_provider
# is a once-per-process door, so place it directly and let monkeypatch undo it.
monkeypatch.setattr(trace, "_TRACER_PROVIDER", logger._tracer_provider)
instrument_fastapi_app(app)
client = TestClient(app)
client.post("/engines/gpt-4o-mini/chat/completions")
client.post("/openai/v1/responses/resp_abc123")
routes = {
(s.attributes or {})["http.route"] for s in exporter.get_finished_spans() if s.kind is SpanKind.SERVER
}
# a parameterized route keeps its template; the passthrough hook rewrote the
# catch-all to the literal path, and both spans have to follow their own span
assert routes == {"/engines/{model:path}/chat/completions", "/openai/v1/responses/resp_abc123"}
assert seen["templated"] == "/engines/{model:path}/chat/completions"
assert seen["passthrough"] == "/openai/v1/responses/resp_abc123"
def test_server_span_route_survives_the_span_ending():
"""The LLM span closes in an async callback that can run after the server span
has ended, so the attribute has to still be readable then."""
from opentelemetry.sdk.trace import TracerProvider
span = TracerProvider().get_tracer("t").start_span("POST /v1/responses/{response_id}")
span.set_attribute("http.route", "/v1/responses/{response_id}")
set_request_root_span(span)
span.end()
assert request_root_http_route() == "/v1/responses/{response_id}"
def test_no_server_span_means_no_route():
"""An SDK call has no anchored server span, so the attribute is omitted rather
than reported as empty."""
assert request_root_http_route() is None
def test_blank_route_on_the_server_span_is_omitted():
"""An excluded or unmatched path leaves the server span without a usable route.
Report nothing rather than a span attribute whose value is the empty string."""
from opentelemetry.sdk.trace import TracerProvider
span = TracerProvider().get_tracer("t").start_span("GET")
span.set_attribute("http.route", "")
set_request_root_span(span)
assert request_root_http_route() is None
def test_known_passthrough_prefixes_present():
"""Guard the prefix set against accidental edits."""
assert {"openai", "anthropic", "vertex_ai", "bedrock"} <= PASSTHROUGH_PREFIXES
def test_instrument_fastapi_app_noop_when_gate_off(monkeypatch):
"""With the gate off the mount is a no-op — no instrumentation attached."""
monkeypatch.delenv("LITELLM_OTEL_V2", raising=False)
app = fastapi.FastAPI()
instrument_fastapi_app(app)
assert getattr(app, "_is_instrumented_by_opentelemetry", False) is False
def test_instrument_fastapi_app_attaches_when_gate_on(monkeypatch):
"""With the gate on the FastAPI app is instrumented for server spans."""
monkeypatch.setenv("LITELLM_OTEL_V2", "1")
app = fastapi.FastAPI()
@app.get("/ping")
def ping():
return {"ok": True}
instrument_fastapi_app(app)
try:
assert getattr(app, "_is_instrumented_by_opentelemetry", False) is True
finally:
FastAPIInstrumentor.uninstrument_app(app)