mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-01 01:18:34 +00:00
* fix(logging): mark mirrored silent_model calls with is_silent_experiment in spend logs and logging payloads * fix(router): keep the mirror flag when litellm_metadata is set and strip a client-sent is_silent_experiment Spend rows read router metadata from litellm_metadata when a call carries it, so the router now sets the flag there too. The proxy drops a client-sent is_silent_experiment with the other router-owned keys, so real traffic cannot be recorded as mirrored. * fix(logging): mark mirrored calls with a router-only key so clients cannot set is_silent_experiment Spend rows and the standard logging payload now derive is_silent_experiment from _litellm_silent_experiment, which only the router writes and the proxy and pass-through routes strip from client metadata. A caller's own is_silent_experiment key stays untouched, so it keeps its current effect on mirroring and on provider metadata, and unmirrored calls log false in both places. * test(langfuse): expect is_silent_experiment false on every logged generation * fix(logging): mark mirrored calls with a router-only enum value so no client route can set is_silent_experiment The router stamps SilentExperimentMarker.MIRRORED, which client JSON can never produce, so batches, Responses WebSocket frames and pass-through need no strip. Langfuse layers the flag after its requester-metadata dedupe. * fix(logging): set is_silent_experiment only on mirrored calls so real calls keep their payloads Real calls no longer gain an is_silent_experiment key in spend rows or the standard logging payload, so OTEL, S3, GCS and every other logger receive what they did before. The Langfuse regression test now runs the real prompt helper. * fix(logging): keep the mirror marker out of callback metadata and read the spend-row flag from the logging payload Logging now strips the router's mirror marker from the request metadata buckets before any callback or vendor sees it, without touching the router's own dicts, so retries keep it. Spend rows read is_silent_experiment from the standard logging payload, which survives the collector's JSON round trip, so collector-mode rows keep the flag too. * fix(logging): mark mirrored calls through a context variable so router usage counts them once * fix(router): leave callback-started calls unmarked and stop client flags from turning mirroring off Logging handlers now run outside the mirrored-call context, so an LLM call a success or failure callback starts while logging a mirrored call is not flagged as mirrored. The guard against mirroring a mirror reads the same context variable instead of request metadata, so a client-sent is_silent_experiment no longer skips the mirrors. The unused module-level get_standard_logging_metadata stops copying resolved keys from raw metadata. * fix(router): keep main's client flag mirroring skip and the other logging metadata keys A client's is_silent_experiment: true skips mirroring again as on main, the module-level get_standard_logging_metadata drops only the flag, and the silent experiment tests wait on a logged-call signal instead of polling a deadline * fix(logging): take the mirror flag in Prometheus labels and Datadog cost tags only from the router * test: type the new parametrized tests and keep the recorder from changing its argument * fix(datadog): keep client request tags from setting the silent experiment cost tag A request tag is_silent_experiment:true set the Datadog cost tag on a direct call. Only the router flag sets it now. Also covers the sync failure handler's mirrored-call reset with a failure function callback test. --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
242 lines
8.2 KiB
Python
242 lines
8.2 KiB
Python
"""
|
|
Unit tests for spend_logs_metadata inclusion in Prometheus custom labels.
|
|
|
|
Verifies that metadata from x-litellm-spend-logs-metadata header is available
|
|
in Prometheus custom labels via combined_metadata.
|
|
"""
|
|
|
|
from typing import Final
|
|
|
|
import pytest
|
|
|
|
from litellm.integrations.prometheus import (
|
|
_get_combined_custom_metadata_from_standard_logging_payload,
|
|
get_custom_labels_from_metadata,
|
|
)
|
|
|
|
|
|
def test_get_custom_labels_includes_spend_logs_metadata(monkeypatch):
|
|
"""
|
|
Test that get_custom_labels_from_metadata extracts fields from
|
|
spend_logs_metadata when it is merged into combined_metadata.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.department", "metadata.env"],
|
|
)
|
|
|
|
# Simulate combined_metadata after merging all three sources
|
|
combined_metadata = {
|
|
"request_key": "from_requester", # from requester_metadata
|
|
"auth_key": "from_auth", # from user_api_key_auth_metadata
|
|
"department": "engineering", # from spend_logs_metadata
|
|
"env": "production", # from spend_logs_metadata
|
|
}
|
|
|
|
result = get_custom_labels_from_metadata(combined_metadata)
|
|
assert result == {
|
|
"metadata_department": "engineering",
|
|
"metadata_env": "production",
|
|
}
|
|
|
|
|
|
def test_spend_logs_metadata_overrides_earlier_sources(monkeypatch):
|
|
"""
|
|
Test that spend_logs_metadata values take precedence over
|
|
requester_metadata and user_api_key_auth_metadata when keys overlap,
|
|
since it is spread last in combined_metadata.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.team"],
|
|
)
|
|
|
|
# Simulate the dict spread order: requester -> auth -> spend_logs
|
|
requester_metadata = {"team": "old_team"}
|
|
user_api_key_auth_metadata = {"team": "auth_team"}
|
|
spend_logs_metadata = {"team": "spend_team"}
|
|
|
|
combined_metadata = {
|
|
**requester_metadata,
|
|
**user_api_key_auth_metadata,
|
|
**spend_logs_metadata,
|
|
}
|
|
|
|
result = get_custom_labels_from_metadata(combined_metadata)
|
|
assert result == {"metadata_team": "spend_team"}
|
|
|
|
|
|
def test_combined_metadata_with_all_three_sources(monkeypatch):
|
|
"""
|
|
Test that combined_metadata correctly merges requester_metadata,
|
|
user_api_key_auth_metadata, and spend_logs_metadata.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.from_requester", "metadata.from_auth", "metadata.from_spend"],
|
|
)
|
|
|
|
# Reproduce the exact spread pattern from prometheus.py
|
|
_requester_metadata = {"from_requester": "val1"}
|
|
user_api_key_auth_metadata = {"from_auth": "val2"}
|
|
spend_logs_metadata = {"from_spend": "val3"}
|
|
|
|
combined_metadata = {
|
|
**(_requester_metadata if _requester_metadata else {}),
|
|
**(user_api_key_auth_metadata if user_api_key_auth_metadata else {}),
|
|
**(spend_logs_metadata if spend_logs_metadata else {}),
|
|
}
|
|
|
|
result = get_custom_labels_from_metadata(combined_metadata)
|
|
assert result == {
|
|
"metadata_from_requester": "val1",
|
|
"metadata_from_auth": "val2",
|
|
"metadata_from_spend": "val3",
|
|
}
|
|
|
|
|
|
def test_combined_metadata_with_none_spend_logs(monkeypatch):
|
|
"""
|
|
Test that combined_metadata works when spend_logs_metadata is None.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.foo"],
|
|
)
|
|
|
|
_requester_metadata = {"foo": "bar"}
|
|
user_api_key_auth_metadata = None
|
|
spend_logs_metadata = None
|
|
|
|
combined_metadata = {
|
|
**(_requester_metadata if _requester_metadata else {}),
|
|
**(user_api_key_auth_metadata if user_api_key_auth_metadata else {}),
|
|
**(spend_logs_metadata if spend_logs_metadata else {}),
|
|
}
|
|
|
|
result = get_custom_labels_from_metadata(combined_metadata)
|
|
assert result == {"metadata_foo": "bar"}
|
|
|
|
|
|
def test_combined_metadata_includes_top_level_fields():
|
|
"""
|
|
Regression test for LIT-3741: user_api_key_project_alias (and other
|
|
top-level metadata fields) must be included in the combined metadata
|
|
so they can be referenced via custom_prometheus_metadata_labels.
|
|
"""
|
|
standard_logging_payload = {
|
|
"metadata": {
|
|
"user_api_key_hash": "sk-abc123",
|
|
"user_api_key_alias": "hotel-key",
|
|
"user_api_key_team_id": "team-1",
|
|
"user_api_key_team_alias": "hotel-team",
|
|
"user_api_key_project_id": "proj-1",
|
|
"user_api_key_project_alias": "hotel-recommendations",
|
|
"user_api_key_user_id": "user-1",
|
|
"user_api_key_user_email": "user@example.com",
|
|
"user_api_key_end_user_id": None,
|
|
"user_api_key_org_id": None,
|
|
"user_api_key_org_alias": None,
|
|
"user_api_key_request_route": "/v1/chat/completions",
|
|
"requester_metadata": {"custom_field": "custom_value"},
|
|
"user_api_key_auth_metadata": {"auth_field": "auth_value"},
|
|
"spend_logs_metadata": None,
|
|
}
|
|
}
|
|
|
|
combined = _get_combined_custom_metadata_from_standard_logging_payload(
|
|
standard_logging_payload
|
|
)
|
|
|
|
assert combined["user_api_key_project_alias"] == "hotel-recommendations"
|
|
assert combined["user_api_key_project_id"] == "proj-1"
|
|
assert combined["user_api_key_team_alias"] == "hotel-team"
|
|
assert combined["user_api_key_request_route"] == "/v1/chat/completions"
|
|
assert combined["custom_field"] == "custom_value"
|
|
assert combined["auth_field"] == "auth_value"
|
|
|
|
|
|
def test_project_alias_accessible_via_custom_prometheus_labels(monkeypatch):
|
|
"""
|
|
Regression test for LIT-3741: configuring
|
|
custom_prometheus_metadata_labels with "metadata.user_api_key_project_alias"
|
|
should produce a label with the project's alias value.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["metadata.user_api_key_project_alias"],
|
|
)
|
|
|
|
standard_logging_payload = {
|
|
"metadata": {
|
|
"user_api_key_project_alias": "hotel-recommendations",
|
|
"requester_metadata": None,
|
|
"user_api_key_auth_metadata": None,
|
|
"spend_logs_metadata": None,
|
|
}
|
|
}
|
|
|
|
combined = _get_combined_custom_metadata_from_standard_logging_payload(
|
|
standard_logging_payload
|
|
)
|
|
result = get_custom_labels_from_metadata(combined)
|
|
|
|
assert result == {"metadata_user_api_key_project_alias": "hotel-recommendations"}
|
|
|
|
|
|
def test_project_alias_accessible_without_prefix(monkeypatch):
|
|
"""
|
|
user_api_key_project_alias should also be accessible without
|
|
the "metadata." prefix in custom_prometheus_metadata_labels config.
|
|
"""
|
|
monkeypatch.setattr(
|
|
"litellm.custom_prometheus_metadata_labels",
|
|
["user_api_key_project_alias"],
|
|
)
|
|
|
|
standard_logging_payload = {
|
|
"metadata": {
|
|
"user_api_key_project_alias": "hotel-recommendations",
|
|
"requester_metadata": None,
|
|
"user_api_key_auth_metadata": None,
|
|
"spend_logs_metadata": None,
|
|
}
|
|
}
|
|
|
|
combined = _get_combined_custom_metadata_from_standard_logging_payload(
|
|
standard_logging_payload
|
|
)
|
|
result = get_custom_labels_from_metadata(combined)
|
|
|
|
assert result == {"user_api_key_project_alias": "hotel-recommendations"}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("standard_logging_metadata", "expected_labels"),
|
|
[
|
|
(
|
|
{"is_silent_experiment": True, "requester_metadata": {"is_silent_experiment": False}},
|
|
{"metadata_is_silent_experiment": "true"},
|
|
),
|
|
(
|
|
{
|
|
"requester_metadata": {"is_silent_experiment": True},
|
|
"spend_logs_metadata": {"is_silent_experiment": "true"},
|
|
},
|
|
{},
|
|
),
|
|
],
|
|
ids=["mirrored call", "direct call with a client flag"],
|
|
)
|
|
def test_silent_experiment_label_comes_only_from_the_router_flag(
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
standard_logging_metadata: dict[str, object],
|
|
expected_labels: dict[str, str],
|
|
):
|
|
monkeypatch.setattr("litellm.custom_prometheus_metadata_labels", ["metadata.is_silent_experiment"])
|
|
|
|
combined: Final = _get_combined_custom_metadata_from_standard_logging_payload(
|
|
{"metadata": standard_logging_metadata}
|
|
)
|
|
|
|
assert get_custom_labels_from_metadata(combined) == expected_labels
|