mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-14 23:21:35 +00:00
refactor(spend): stop queueing a batch's claim row for a writer the proxy never builds
SPEND_LOGS_URL only diverts spend logs when db_writer_client is set, and nothing in the proxy ever assigns that global, so the queued copy was only ever skipped as a duplicate by the local insert.
This commit is contained in:
parent
fcb6d2267c
commit
defd8661f4
2 changed files with 0 additions and 58 deletions
|
|
@ -380,9 +380,6 @@ class DBSpendUpdateWriter:
|
|||
skip_duplicates=True,
|
||||
)
|
||||
if claimed == 1:
|
||||
await self._forward_batch_cost_row(
|
||||
row=row, prisma_client=prisma_client, disable_spend_logs=disable_spend_logs
|
||||
)
|
||||
return True
|
||||
existing: Final = await spend_logs.find_unique(
|
||||
where={"request_id": request_id} # mutable-ok: prisma where clause
|
||||
|
|
@ -405,18 +402,6 @@ class DBSpendUpdateWriter:
|
|||
return False
|
||||
return await self._take_over_uncharged_batch_cost_row(payload=payload, prisma_client=prisma_client, row=row)
|
||||
|
||||
async def _forward_batch_cost_row(
|
||||
self, row: Mapping[str, object], prisma_client: "PrismaClient", disable_spend_logs: bool
|
||||
) -> None:
|
||||
"""Queue the claimed row for an external spend log writer, which the claim went around.
|
||||
|
||||
With ``SPEND_LOGS_URL`` set the queue posts every spend log to that writer instead of
|
||||
inserting it, so a batch's cost row reaches it only by being queued here as well.
|
||||
"""
|
||||
if disable_spend_logs is True or os.getenv("SPEND_LOGS_URL") is None:
|
||||
return
|
||||
await self._insert_spend_log_to_db(payload=prisma_client.jsonify_object(row), prisma_client=prisma_client)
|
||||
|
||||
async def _take_over_uncharged_batch_cost_row(
|
||||
self, payload: SpendLogsPayload, prisma_client: "PrismaClient", row: Mapping[str, object]
|
||||
) -> bool:
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
import asyncio
|
||||
import copy
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
|
|
@ -3212,48 +3211,6 @@ async def test_update_database_claims_a_batch_without_logging_the_request_that_p
|
|||
assert db_writer._batch_database_updates.await_count == 1
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("spend_logs_url", "forwarded"),
|
||||
[("http://spend-logs.internal", True), (None, False)],
|
||||
ids=["an_external_writer_takes_the_rows", "rows_are_written_to_this_db"],
|
||||
)
|
||||
async def test_update_database_sends_a_claimed_batch_cost_row_on_to_an_external_spend_log_writer(
|
||||
monkeypatch, spend_logs_url: str | None, forwarded: bool
|
||||
):
|
||||
"""
|
||||
SPEND_LOGS_URL makes the flush post spend logs to that writer instead of inserting them,
|
||||
and the claim writes straight to this table, so the batch's row reaches the writer only
|
||||
by being queued as well. Queueing it with no writer configured would insert it twice.
|
||||
"""
|
||||
db_writer = DBSpendUpdateWriter()
|
||||
db_writer._batch_database_updates = AsyncMock()
|
||||
prisma = _spend_logs_prisma(1, None)
|
||||
if spend_logs_url is None:
|
||||
monkeypatch.delenv("SPEND_LOGS_URL", raising=False)
|
||||
else:
|
||||
monkeypatch.setenv("SPEND_LOGS_URL", spend_logs_url)
|
||||
|
||||
assert await _update_database_with(db_writer, prisma, _batch_cost_payload()) is True
|
||||
|
||||
queued = [row["request_id"] for row in prisma.spend_log_transactions]
|
||||
assert queued == (["batch_abc_batch_cost"] if forwarded else [])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_database_forwards_no_batch_cost_row_a_later_retrieve_had_already_claimed(monkeypatch):
|
||||
"""The retrieve that lost the claim charges nothing, so it must not post a row either."""
|
||||
db_writer = DBSpendUpdateWriter()
|
||||
db_writer._batch_database_updates = AsyncMock()
|
||||
existing = SimpleNamespace(call_type="aretrieve_batch", status="success", spend=0.25)
|
||||
prisma = _spend_logs_prisma(0, existing)
|
||||
monkeypatch.setenv("SPEND_LOGS_URL", "http://spend-logs.internal")
|
||||
|
||||
assert await _update_database_with(db_writer, prisma, _batch_cost_payload()) is False
|
||||
|
||||
assert prisma.spend_log_transactions == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_database_queues_only_the_claim_for_a_batch_it_could_not_write_with_logs_disabled():
|
||||
"""A refused claim is retried through the queue, so what it queues has to stay unlogged too."""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue