litellm/tests/unit/integrations/lens/test_receiver.py
Kerry Lu bd1c3968f4
feat(proxy): send gateway model calls to Lens as traces when the Lens store is connected (#53042)
* Send gateway model calls to Lens as traces when the Lens store is connected

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

* Sort the tracing runtime imports

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

* Compare the old Lens probe status against HTTPStatus so the type gate holds

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

* Parked: drop caller IP, isolate Lens config from OTEL env, shut down exporter, delivery test

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

* feat(proxy): group gateway traces in Lens by session, name the agent and report tool calls as steps

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

* fix(proxy): keep metadata inherited from request baggage off Lens traces

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

* Revert "fix(proxy): keep metadata inherited from request baggage off Lens traces"

This reverts commit d23f0e628e.

* fix(proxy): send the caller IP to Lens as the OTel callback does, and satisfy the type discipline gate

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

* test(proxy): deliver Lens traces in process instead of over a socket

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

* test(proxy): compare the gateway Lens trace with a recorded Claude Code trace

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

* test(proxy): check the tool results the provider receives in the Lens parity replay

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

* test(proxy): keep one for clause per comprehension in the Lens delivery tests

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

* refactor(proxy): send Lens traces to its trace route from one adapter module under integrations/lens

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

* refactor(lens): move the gateway's Lens package from litellm/tracing to litellm/integrations/lens

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

* refactor(clickhouse): keep the cost row types with the ClickHouse cost logger

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

* refactor(lens): move the gateway's Lens routes and startup from litellm/proxy to litellm/integrations/lens/proxy

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

* test(lens): keep one for clause per comprehension in the parity test

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

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-20 10:12:08 -08:00

71 lines
2.4 KiB
Python

from datetime import datetime, timezone
from typing import Final, cast
import pytest
from litellm.constants import AGENT_TRACING_AGENT_LIST_LIMIT
from litellm.integrations.lens.generated.models import TraceAgentRow, TraceAgentsParams
from litellm.integrations.lens.generated.types import TraceScope
from litellm.integrations.lens.storage import LensTraceStorage
from litellm.integrations.lens import TraceReceiver
from litellm.integrations.lens.types import TraceAgent
class AgentRowsStorage:
def __init__(self, rows: tuple[TraceAgentRow, ...]) -> None:
self.rows: Final = rows
self.requests: tuple[TraceAgentsParams, ...] = ()
async def trace_agents(self, parameters: TraceAgentsParams) -> tuple[TraceAgentRow, ...]:
self.requests = (*self.requests, parameters)
return self.rows
@pytest.mark.asyncio
@pytest.mark.parametrize(
("scope", "all_teams", "user_id", "team_ids"),
(
pytest.param(TraceScope(all_teams=1, user_id="", team_ids=()), 1, "", (), id="all-teams"),
pytest.param(TraceScope(all_teams=0, user_id="u1", team_ids=("t1", "t2")), 0, "u1", ("t1", "t2"), id="owned"),
),
)
async def test_list_agents_queries_the_reader_scope_and_shapes_rows(
scope: TraceScope, all_teams: int, user_id: str, team_ids: tuple[str, ...]
) -> None:
storage: Final = AgentRowsStorage(
(
TraceAgentRow(
agent_name="moyai", runs=5, failed_runs=2, last_seen_ms=1_791_405_060_123, frameworks=("pi",)
),
TraceAgentRow(agent_name="research", runs=1, failed_runs=0, last_seen_ms=0),
)
)
receiver: Final = TraceReceiver(storage=cast(LensTraceStorage, storage))
result: Final = await receiver.list_agents(scope, start_ms=10, end_ms=20)
assert storage.requests == (
TraceAgentsParams(
all_teams=all_teams,
user_id=user_id,
team_ids=team_ids,
start_ms=10,
end_ms=20,
limit=AGENT_TRACING_AGENT_LIST_LIMIT,
),
)
assert result.agents == (
TraceAgent(
name="moyai",
runs=5,
failed_runs=2,
last_seen=datetime(2026, 10, 7, 20, 31, 0, 123000, tzinfo=timezone.utc),
frameworks=("pi",),
),
TraceAgent(
name="research",
runs=1,
failed_runs=0,
last_seen=datetime(1970, 1, 1, tzinfo=timezone.utc),
),
)