mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-15 09:12:18 +00:00
fix(langfuse_otel): build the trace endpoint in one place
The Langfuse OTLP endpoint was assembled in three spots, so fixing one left the others building a different URL. This puts both the host and the per-key/team destination through one helper that always names /api/public/otel/v1/traces, and also collapses a host that already carries that path instead of doubling it. A key-scoped export with no langfuse_host and no LANGFUSE_HOST used to hand _build_langfuse_otel_config no host, which skipped the trace path entirely, so it now falls back to US cloud. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1fbd1e9ce9
commit
7ba582bb2d
6 changed files with 71 additions and 28 deletions
|
|
@ -24,12 +24,27 @@ else:
|
|||
Span = Any
|
||||
|
||||
|
||||
LANGFUSE_CLOUD_EU_ENDPOINT: Final = "https://cloud.langfuse.com/api/public/otel"
|
||||
LANGFUSE_CLOUD_US_ENDPOINT: Final = "https://us.cloud.langfuse.com/api/public/otel"
|
||||
LANGFUSE_CLOUD_EU_ENDPOINT: Final = "https://cloud.langfuse.com"
|
||||
LANGFUSE_CLOUD_US_ENDPOINT: Final = "https://us.cloud.langfuse.com"
|
||||
LANGFUSE_INGESTION_VERSION_HEADER: Final = "x-langfuse-ingestion-version"
|
||||
LANGFUSE_INGESTION_VERSION: Final = "4"
|
||||
|
||||
|
||||
def langfuse_otlp_traces_endpoint(host: str) -> str:
|
||||
"""Point a Langfuse host at its OTLP trace receiver.
|
||||
|
||||
Accepts a bare host, a URL, or a full endpoint that already carries the path,
|
||||
so an operator who pastes the URL from Langfuse's docs is not sent to a
|
||||
doubled path. Langfuse exposes no OTLP route anywhere but ``/v1/traces``.
|
||||
"""
|
||||
base: Final = host if host.startswith("http") else f"https://{host}"
|
||||
stripped: Final = base.rstrip("/")
|
||||
for suffix in ("/api/public/otel/v1/traces", "/api/public/otel"):
|
||||
if stripped.endswith(suffix):
|
||||
return f"{stripped[: -len(suffix)]}/api/public/otel/v1/traces"
|
||||
return f"{stripped}/api/public/otel/v1/traces"
|
||||
|
||||
|
||||
class LangfuseOtelLogger(OpenTelemetry):
|
||||
def __init__(self, config=None, *args, **kwargs):
|
||||
# Prevent LangfuseOtelLogger from modifying global environment variables by constructing config manually
|
||||
|
|
@ -325,8 +340,7 @@ class LangfuseOtelLogger(OpenTelemetry):
|
|||
given host (US cloud when no host is provided), authorized with the given keys.
|
||||
"""
|
||||
if langfuse_host:
|
||||
normalized_host: Final = langfuse_host if langfuse_host.startswith("http") else f"https://{langfuse_host}"
|
||||
endpoint = f"{normalized_host.rstrip('/')}/api/public/otel"
|
||||
endpoint = langfuse_otlp_traces_endpoint(langfuse_host)
|
||||
verbose_logger.debug("Using Langfuse OTEL endpoint from host: %s", endpoint)
|
||||
else:
|
||||
endpoint = LANGFUSE_CLOUD_US_ENDPOINT
|
||||
|
|
@ -413,7 +427,11 @@ class LangfuseOtelLogger(OpenTelemetry):
|
|||
if not public_key or not secret_key:
|
||||
return None
|
||||
|
||||
langfuse_host: Final = standard_callback_dynamic_params.get("langfuse_host") or self._get_langfuse_otel_host()
|
||||
langfuse_host: Final = (
|
||||
standard_callback_dynamic_params.get("langfuse_host")
|
||||
or self._get_langfuse_otel_host()
|
||||
or LANGFUSE_CLOUD_US_ENDPOINT
|
||||
)
|
||||
return LangfuseOtelLogger._build_langfuse_otel_config(
|
||||
public_key=public_key,
|
||||
secret_key=secret_key,
|
||||
|
|
|
|||
|
|
@ -44,14 +44,14 @@ def _langfuse_destination(params: StandardCallbackDynamicParams) -> "_Destinatio
|
|||
from litellm.integrations.langfuse.langfuse_otel import (
|
||||
LANGFUSE_CLOUD_US_ENDPOINT,
|
||||
LangfuseOtelLogger,
|
||||
langfuse_otlp_traces_endpoint,
|
||||
)
|
||||
|
||||
tenant_host: Final = params.get("langfuse_host") or None
|
||||
host: Final = tenant_host or LangfuseOtelLogger._get_langfuse_otel_host() # pyright: ignore[reportPrivateUsage] # reuse the backend's own env host resolver rather than duplicating it
|
||||
if not host:
|
||||
return (LANGFUSE_CLOUD_US_ENDPOINT, None)
|
||||
normalized: Final = host if host.startswith("http") else f"https://{host}"
|
||||
endpoint: Final = f"{normalized.rstrip('/')}/api/public/otel"
|
||||
return (langfuse_otlp_traces_endpoint(LANGFUSE_CLOUD_US_ENDPOINT), None)
|
||||
endpoint: Final = langfuse_otlp_traces_endpoint(host)
|
||||
if tenant_host is None:
|
||||
return (endpoint, None)
|
||||
if not is_url_destination_allowed_by_host(endpoint, litellm.provider_url_destination_allowed_hosts):
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ Holds the shared ProxyClient so the ``resources`` fixture cleans up keys, teams,
|
|||
users, orgs, and models it creates. External Langfuse reads go through
|
||||
``e2e_http`` (the only module allowed to call ``requests.*``).
|
||||
|
||||
Uses the ``langfuse_otel`` callback (OTLP to ``{host}/api/public/otel``), not
|
||||
Uses the ``langfuse_otel`` callback (OTLP to ``{host}/api/public/otel/v1/traces``), not
|
||||
the classic ``langfuse`` SDK callback. OTEL generations land as name
|
||||
``litellm_request``; correlate by unique prompt marker and ``user_api_key_alias``
|
||||
in metadata. Spend is on ``calculatedTotalCost`` (StandardLogging response_cost).
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ from litellm.proxy.litellm_pre_call_utils import (
|
|||
from litellm.types.utils import StandardCallbackDynamicParams
|
||||
|
||||
LANGFUSE_DEST = OtelDestination(
|
||||
endpoint="http://tenant.local/api/public/otel",
|
||||
endpoint="http://tenant.local/api/public/otel/v1/traces",
|
||||
headers={"Authorization": "Basic dGVuYW50"},
|
||||
callback_name="langfuse_otel",
|
||||
)
|
||||
|
|
@ -156,9 +156,9 @@ class TestRoutingMode:
|
|||
"""
|
||||
|
||||
OPERATOR_SINK = ("https://cloud.langfuse.com/api/public/otel/v1/traces", (("authorization", "Basic op"),))
|
||||
#: What a tenant destination for that same project looks like before normalizing:
|
||||
#: no signal path yet, and the header name cased the way the backend writes it.
|
||||
SAME_ACCOUNT_ENDPOINT = "https://cloud.langfuse.com/api/public/otel"
|
||||
#: What a tenant destination for that same project looks like:
|
||||
#: the signal path is present, and the header name cased the way the backend writes it.
|
||||
SAME_ACCOUNT_ENDPOINT = "https://cloud.langfuse.com/api/public/otel/v1/traces"
|
||||
|
||||
@staticmethod
|
||||
def _additive(monkeypatch):
|
||||
|
|
@ -1317,7 +1317,7 @@ class TestDestinationResolution:
|
|||
|
||||
destinations = resolve_tenant_otel_destinations(auth)
|
||||
|
||||
assert [d.endpoint for d in destinations] == ["http://team.local/api/public/otel"]
|
||||
assert [d.endpoint for d in destinations] == ["http://team.local/api/public/otel/v1/traces"]
|
||||
assert destinations[0].callback_name == "langfuse_otel"
|
||||
|
||||
def test_a_keys_service_name_outranks_its_teams_on_the_destination(self, monkeypatch):
|
||||
|
|
@ -1391,7 +1391,7 @@ class TestDestinationResolution:
|
|||
team_metadata={"logging": [entry("http://team.local")]},
|
||||
)
|
||||
|
||||
assert [d.endpoint for d in resolve_tenant_otel_destinations(auth)] == ["http://key.local/api/public/otel"]
|
||||
assert [d.endpoint for d in resolve_tenant_otel_destinations(auth)] == ["http://key.local/api/public/otel/v1/traces"]
|
||||
|
||||
def test_nothing_resolves_while_otel_v2_is_off(self, monkeypatch):
|
||||
monkeypatch.delenv("LITELLM_OTEL_V2", raising=False)
|
||||
|
|
@ -1428,7 +1428,7 @@ class TestDestinationResolution:
|
|||
|
||||
|
||||
LLM_ONLY_DEST = OtelDestination(
|
||||
endpoint="http://tenant.local/api/public/otel",
|
||||
endpoint="http://tenant.local/api/public/otel/v1/traces",
|
||||
headers={"Authorization": "Basic dGVuYW50"},
|
||||
callback_name="langfuse_otel",
|
||||
span_scope="llm_only",
|
||||
|
|
@ -2147,7 +2147,7 @@ class TestPresetDegradation:
|
|||
assert logger is not None
|
||||
assert [spec.endpoint for spec in logger.config.exporters] == [
|
||||
"http://collector.local:4318",
|
||||
"https://cloud.langfuse.com/api/public/otel",
|
||||
"https://cloud.langfuse.com/api/public/otel/v1/traces",
|
||||
]
|
||||
assert all(spec.headers for spec in logger.config.exporters if spec.requires_headers)
|
||||
|
||||
|
|
@ -2323,7 +2323,7 @@ class TestTenantConfigAgreement:
|
|||
|
||||
destinations = resolve_tenant_otel_destinations(auth)
|
||||
|
||||
assert [d.endpoint for d in destinations] == ["http://key.local/api/public/otel"]
|
||||
assert [d.endpoint for d in destinations] == ["http://key.local/api/public/otel/v1/traces"]
|
||||
|
||||
def test_a_failure_entry_still_wins_the_merge_next_to_a_success_entry(self):
|
||||
entries = [
|
||||
|
|
@ -2342,7 +2342,7 @@ class TestTenantConfigAgreement:
|
|||
destinations = resolve_tenant_otel_destinations(UserAPIKeyAuth(team_metadata={"logging": entries}))
|
||||
|
||||
assert runtime.callback_vars["langfuse_host"] == "http://key.local"
|
||||
assert [d.endpoint for d in destinations] == ["http://key.local/api/public/otel"]
|
||||
assert [d.endpoint for d in destinations] == ["http://key.local/api/public/otel/v1/traces"]
|
||||
assert destinations[0].headers["Authorization"] == f"Basic {b64encode(b'pk-failure:sk-failure').decode()}"
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -3216,7 +3216,7 @@ class TestTenantHostSsrfGuard:
|
|||
|
||||
destination = destination_for("langfuse_otel", self._langfuse("http://127.0.0.1:9111"))
|
||||
|
||||
assert destination.endpoint == "http://127.0.0.1:9111/api/public/otel"
|
||||
assert destination.endpoint == "http://127.0.0.1:9111/api/public/otel/v1/traces"
|
||||
|
||||
def test_the_operators_own_internal_host_is_never_blocked(self, monkeypatch):
|
||||
"""The operator configures ``LANGFUSE_HOST`` themselves, so an internal
|
||||
|
|
@ -3225,7 +3225,7 @@ class TestTenantHostSsrfGuard:
|
|||
|
||||
destination = destination_for("langfuse_otel", {"langfuse_public_key": "pk", "langfuse_secret_key": "sk"})
|
||||
|
||||
assert destination.endpoint == "http://127.0.0.1:9111/api/public/otel"
|
||||
assert destination.endpoint == "http://127.0.0.1:9111/api/public/otel/v1/traces"
|
||||
|
||||
def test_an_allowlisted_host_is_taken_without_resolving_it(self, monkeypatch):
|
||||
"""The check runs on the asyncio auth path, so it must not block on a name the
|
||||
|
|
@ -3234,7 +3234,7 @@ class TestTenantHostSsrfGuard:
|
|||
|
||||
destination = destination_for("langfuse_otel", self._langfuse("https://lf.invalid"))
|
||||
|
||||
assert destination.endpoint == "https://lf.invalid/api/public/otel"
|
||||
assert destination.endpoint == "https://lf.invalid/api/public/otel/v1/traces"
|
||||
|
||||
def test_a_rejected_host_is_warned_about_once(self, caplog):
|
||||
with caplog.at_level("WARNING", logger="LiteLLM"):
|
||||
|
|
|
|||
|
|
@ -168,7 +168,7 @@ def test_dynamic_headers_do_not_leak_to_other_owners_exporter():
|
|||
),
|
||||
ExporterSpec(
|
||||
kind="otlp_http",
|
||||
endpoint="https://cloud.langfuse.com/api/public/otel",
|
||||
endpoint="https://cloud.langfuse.com/api/public/otel/v1/traces",
|
||||
headers="Authorization=Basic base-langfuse",
|
||||
owner="langfuse_otel",
|
||||
),
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ class TestLangfuseOtelIntegration:
|
|||
assert config.exporter == "otlp_http"
|
||||
assert "Authorization=Basic" in config.headers
|
||||
# Note: We no longer set os.environ explicitly to avoid leakage
|
||||
# assert os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT") == "https://us.cloud.langfuse.com/api/public/otel"
|
||||
# assert os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT") == "https://us.cloud.langfuse.com/api/public/otel/v1/traces"
|
||||
# assert "Authorization=Basic" in os.environ.get("OTEL_EXPORTER_OTLP_HEADERS", "")
|
||||
|
||||
def test_get_langfuse_otel_config_missing_keys(self):
|
||||
|
|
@ -483,6 +483,31 @@ class TestLangfuseOtelIntegration:
|
|||
# Endpoint assertion removed as side effect is gone
|
||||
|
||||
|
||||
class TestLangfuseOtelHostToEndpoint:
|
||||
@pytest.mark.parametrize(
|
||||
"host",
|
||||
("https://langfuse.example.com", "langfuse.example.com/", "https://langfuse.example.com/api/public/otel"),
|
||||
)
|
||||
def test_every_host_shape_lands_on_the_traces_receiver(self, host):
|
||||
config = LangfuseOtelLogger._build_langfuse_otel_config("pk", "sk", host)
|
||||
|
||||
assert config.endpoint == "https://langfuse.example.com/api/public/otel/v1/traces"
|
||||
|
||||
def test_an_endpoint_that_already_names_the_receiver_is_not_doubled(self):
|
||||
config = LangfuseOtelLogger._build_langfuse_otel_config(
|
||||
"pk", "sk", "https://langfuse.example.com/api/public/otel/v1/traces"
|
||||
)
|
||||
|
||||
assert config.endpoint == "https://langfuse.example.com/api/public/otel/v1/traces"
|
||||
|
||||
def test_a_key_scoped_config_with_no_host_at_all_still_names_the_traces_receiver(self):
|
||||
with patch.dict(os.environ, {}, clear=True):
|
||||
config = LangfuseOtelLogger().construct_dynamic_otel_config({"langfuse_public_key": "pk", "langfuse_secret_key": "sk"})
|
||||
|
||||
assert config is not None
|
||||
assert config.endpoint == "https://us.cloud.langfuse.com/api/public/otel/v1/traces"
|
||||
|
||||
|
||||
class TestLangfuseOtelKeyDynamicConfig:
|
||||
"""Key/team-scoped Langfuse credentials must define the full export target
|
||||
(OTLP endpoint + auth), not just auth headers on the init-time exporter."""
|
||||
|
|
@ -522,7 +547,7 @@ class TestLangfuseOtelKeyDynamicConfig:
|
|||
|
||||
assert config is not None
|
||||
assert config.exporter == "otlp_http"
|
||||
assert config.endpoint == "https://langfuse.example.com/api/public/otel"
|
||||
assert config.endpoint == "https://langfuse.example.com/api/public/otel/v1/traces"
|
||||
|
||||
import base64
|
||||
|
||||
|
|
@ -535,7 +560,7 @@ class TestLangfuseOtelKeyDynamicConfig:
|
|||
config = logger.construct_dynamic_otel_config(self._dynamic_params(langfuse_host="langfuse.example.com"))
|
||||
|
||||
assert config is not None
|
||||
assert config.endpoint == "https://langfuse.example.com/api/public/otel"
|
||||
assert config.endpoint == "https://langfuse.example.com/api/public/otel/v1/traces"
|
||||
|
||||
def test_construct_dynamic_otel_config_defaults_to_us_cloud(self):
|
||||
with self._clean_env():
|
||||
|
|
@ -543,7 +568,7 @@ class TestLangfuseOtelKeyDynamicConfig:
|
|||
config = logger.construct_dynamic_otel_config(self._dynamic_params(langfuse_host=None))
|
||||
|
||||
assert config is not None
|
||||
assert config.endpoint == "https://us.cloud.langfuse.com/api/public/otel"
|
||||
assert config.endpoint == "https://us.cloud.langfuse.com/api/public/otel/v1/traces"
|
||||
|
||||
def test_construct_dynamic_otel_config_falls_back_to_env_host(self):
|
||||
with self._clean_env():
|
||||
|
|
@ -552,7 +577,7 @@ class TestLangfuseOtelKeyDynamicConfig:
|
|||
config = logger.construct_dynamic_otel_config(self._dynamic_params(langfuse_host=None))
|
||||
|
||||
assert config is not None
|
||||
assert config.endpoint == "https://env-host.example.com/api/public/otel"
|
||||
assert config.endpoint == "https://env-host.example.com/api/public/otel/v1/traces"
|
||||
|
||||
def test_construct_dynamic_otel_config_requires_both_keys(self):
|
||||
with self._clean_env():
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue