mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* feat(otel v2): proxy-level default langfuse_span_scope for key and team destinations Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(otel): regenerate dashboard API documentation types Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(otel): preserve tenant destinations with malformed otel settings Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(otel): cover explicit narrow scope and non-Langfuse defaults Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * Read only the prefixed Langfuse destination scope environment variable 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>
138 lines
5.3 KiB
Python
138 lines
5.3 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
from collections.abc import Iterator
|
|
from typing import Final, Literal
|
|
|
|
import pytest
|
|
from integration._support.client import Gateway, gateway_from_environment, held
|
|
from integration._support.otlp_sink import (
|
|
AuditConfigWriter,
|
|
SpanSinks,
|
|
langfuse_callback_variables,
|
|
recorded_spans,
|
|
spans_for_trace,
|
|
)
|
|
from integration._support.process import owned_proxy_process
|
|
from integration._support.wire import Reply, Request, Wire, wire_server
|
|
from pydantic import JsonValue
|
|
|
|
|
|
def _chat(request: Request) -> Reply:
|
|
assert request.target.endswith("/chat/completions"), request.target
|
|
assert b'"messages"' in request.body, request.body
|
|
return Reply(
|
|
body=(
|
|
b'{"id":"chatcmpl-scope","object":"chat.completion","created":1,"model":"gpt-4o-mini",'
|
|
b'"choices":[{"index":0,"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}],'
|
|
b'"usage":{"prompt_tokens":7,"completion_tokens":2,"total_tokens":9}}'
|
|
)
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def provider() -> Iterator[Wire]:
|
|
with wire_server(_chat) as wire:
|
|
yield wire
|
|
|
|
|
|
@pytest.fixture(scope="module", params=["llm_only", None, "full"], ids=["proxy-llm-only", "proxy-unset", "proxy-full"])
|
|
def proxy(
|
|
request: pytest.FixtureRequest,
|
|
audit_sinks: SpanSinks,
|
|
otel_audit_config: AuditConfigWriter,
|
|
tmp_path_factory: pytest.TempPathFactory,
|
|
) -> Iterator[tuple[Gateway, bool]]:
|
|
directory: Final = tmp_path_factory.mktemp("langfuse-destination-scope")
|
|
narrowed: Final = request.param == "llm_only"
|
|
default: Final = "llm_only" if narrowed else "full"
|
|
with (
|
|
gateway_from_environment() as gateway,
|
|
owned_proxy_process(
|
|
gateway,
|
|
directory,
|
|
{
|
|
"LITELLM_OTEL_V2": "true",
|
|
"ARIZE_HTTP_ENDPOINT": audit_sinks.arize + "/v1/traces",
|
|
**({"LITELLM_OTEL_LANGFUSE_DESTINATION_SPAN_SCOPE": default} if request.param is not None else {}),
|
|
},
|
|
config=otel_audit_config(directory, {}),
|
|
remove_environment=(
|
|
"LANGFUSE_PUBLIC_KEY",
|
|
"LANGFUSE_SECRET_KEY",
|
|
"LANGFUSE_HOST",
|
|
"LITELLM_OTEL_LANGFUSE_SPAN_SCOPE",
|
|
*(("LITELLM_OTEL_LANGFUSE_DESTINATION_SPAN_SCOPE",) if request.param is None else ()),
|
|
),
|
|
workers=2,
|
|
) as owned,
|
|
):
|
|
yield owned.gateway, narrowed
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("level", "scope", "backend"),
|
|
[
|
|
("key", None, "langfuse_otel"),
|
|
("key", "full", "langfuse_otel"),
|
|
("team", None, "langfuse_otel"),
|
|
("key", "llm_only", "langfuse_otel"),
|
|
("key", None, "arize"),
|
|
],
|
|
ids=["key-default", "key-full", "team-default", "key-llm-only", "arize-default"],
|
|
)
|
|
def test_proxy_default_limits_only_destinations_without_an_explicit_scope(
|
|
level: Literal["key", "team"],
|
|
scope: str | None,
|
|
backend: Literal["langfuse_otel", "arize"],
|
|
proxy: tuple[Gateway, bool],
|
|
provider: Wire,
|
|
audit_sinks: SpanSinks,
|
|
) -> None:
|
|
gateway, narrowed = proxy
|
|
logging: Final[list[JsonValue]] = [
|
|
{
|
|
"callback_name": backend,
|
|
"callback_type": "success",
|
|
"callback_vars": {
|
|
**(
|
|
langfuse_callback_variables(audit_sinks)
|
|
if backend == "langfuse_otel"
|
|
else {"arize_api_key": "arize-test-key", "arize_space_id": "arize-test-space"}
|
|
),
|
|
**({"langfuse_span_scope": scope} if scope else {}),
|
|
},
|
|
}
|
|
]
|
|
sink: Final = audit_sinks.tenant if backend == "langfuse_otel" else audit_sinks.arize
|
|
with gateway.scenario() as scenario:
|
|
model: Final = scenario.model(api_base=provider.url + "/v1")
|
|
key: Final = (
|
|
scenario.key(metadata={"logging": logging})
|
|
if level == "key"
|
|
else scenario.key(team_id=scenario.team(metadata={"logging": logging}))
|
|
)
|
|
since: Final = recorded_spans(sink)[0]
|
|
trace_id: Final = uuid.uuid4().hex
|
|
response: Final = gateway.request(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{"model": model, "messages": [{"role": "user", "content": "destination span scope"}]},
|
|
key=key,
|
|
headers={"traceparent": f"00-{trace_id}-{uuid.uuid4().hex[:16]}-01"},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
spans: Final = held(
|
|
lambda: spans_for_trace(recorded_spans(sink, since)[1], trace_id),
|
|
lambda found: any("gen_ai.operation.name" in span["attributes"] for span in found),
|
|
holding=3,
|
|
seconds=40,
|
|
)
|
|
if backend == "langfuse_otel" and (scope == "llm_only" or (narrowed and scope is None)):
|
|
assert len(spans) == 1, [(span["name"], span["kind"]) for span in spans]
|
|
assert "gen_ai.operation.name" in spans[0]["attributes"], spans
|
|
assert not any(span["kind"] == 2 or span["attributes"].get("db.system") == "postgresql" for span in spans)
|
|
return
|
|
assert any(span["kind"] == 2 for span in spans), [(span["name"], span["kind"]) for span in spans]
|
|
assert any("gen_ai.operation.name" in span["attributes"] for span in spans), spans
|
|
assert any(span["attributes"].get("db.system") == "postgresql" for span in spans), spans
|