litellm/tests/unit/integrations/lens/test_storage.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

86 lines
2.9 KiB
Python

import json
from typing import Final
import httpx
import pytest
from pydantic import JsonValue
from litellm.integrations.lens.remote import RemoteTraceStore
from litellm.integrations.lens.storage import LensTraceStorage
@pytest.mark.parametrize(
("body", "rows"),
(
(
json.dumps(
{
"meta": [{"name": "value", "type": "UInt64"}],
"data": [
{
"integer": 9007199254740993,
"integer_string": "9007199254740993",
"fraction": 2.5,
"nested": {"values": [True, None, "text", {"count": 2}]},
}
],
"rows": 1,
"statistics": {"elapsed": 0.01, "rows_read": 1, "bytes_read": 8},
"rows_before_limit_at_least": 1,
"unknown_extra": {"kept_by_internal_validator": True},
}
),
[
{
"integer": 9007199254740993,
"integer_string": "9007199254740993",
"fraction": 2.5,
"nested": {"values": [True, None, "text", {"count": 2}]},
}
],
),
(
json.dumps(
{
"meta": [],
"data": [],
"rows": 0,
"statistics": {"elapsed": 0, "rows_read": 0, "bytes_read": 0},
}
),
[],
),
),
)
async def test_sql_query_returns_only_rows_without_normalizing_json_values(
body: str, rows: list[dict[str, JsonValue]]
) -> None:
async with httpx.AsyncClient(
base_url="http://lens", transport=httpx.MockTransport(lambda request: httpx.Response(200, content=body))
) as client:
result: Final = await LensTraceStorage(RemoteTraceStore(client)).query_sql(
"SELECT 1", {"kind": "all"}, "secret"
)
assert result.model_dump(mode="json") == {"data": rows}
if rows:
row: Final = result.data[0]
assert type(row["integer"]) is int
assert type(row["integer_string"]) is str
assert type(row["fraction"]) is float
assert result.data == tuple(rows)
@pytest.mark.parametrize(
"body",
(
'{"meta":[],"data":[],"rows":0}',
'{"meta":[],"data":{},"rows":0,"statistics":{"elapsed":0,"rows_read":0,"bytes_read":0}}',
),
)
async def test_sql_query_rejects_malformed_clickhouse_envelopes(body: str) -> None:
async with httpx.AsyncClient(
base_url="http://lens", transport=httpx.MockTransport(lambda request: httpx.Response(200, content=body))
) as client:
with pytest.raises(RuntimeError, match="Lens trace query returned an invalid response"):
await LensTraceStorage(RemoteTraceStore(client)).query_sql("SELECT 1", {"kind": "all"}, "secret")