litellm/litellm/telemetry/http_exporter.py
devin-ai-integration[bot] 596c51a5fa
feat(telemetry): add AggregatingSink, fixed-bucket histograms, Report and HttpExporter (#45487)
* feat(telemetry): add AggregatingSink, fixed-bucket histograms, Report and HttpExporter

* fix(telemetry): add records to rows in place, guard concurrent flushes, cap restored rows and count bad histogram values

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(telemetry): give the shared row helper its lint reasons

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(telemetry): retry the same statuses as the proxy's should_retry (408, 409, 429, 5xx)

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(telemetry): cover populated block_types and header_keys in report JSON

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: nate <nate@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-10-13 18:33:01 -07:00

37 lines
1.3 KiB
Python

import json
from typing import Final
import httpx
from litellm.telemetry.report import Report, report_to_json
from litellm.telemetry.sink import ExportOutcome
_RETRYABLE_STATUS: Final = frozenset({408, 409, 429})
def _outcome_for(status_code: int) -> ExportOutcome:
if 200 <= status_code < 300:
return ExportOutcome.SENT
if status_code in _RETRYABLE_STATUS or status_code >= 500:
return ExportOutcome.RETRY
return ExportOutcome.REJECTED
class HttpExporter:
"""POSTs each report as JSON; failures come back as an ``ExportOutcome``, never as an exception"""
def __init__(self, client: httpx.AsyncClient, endpoint: str) -> None:
self._client: Final = client
self._endpoint: Final = endpoint
async def export(self, report: Report) -> ExportOutcome:
body: Final = json.dumps(report_to_json(report), separators=(",", ":")).encode()
try:
response: Final = await self._client.post(
self._endpoint, content=body, headers={"content-type": "application/json"}
)
except (httpx.InvalidURL, httpx.UnsupportedProtocol):
return ExportOutcome.REJECTED
except httpx.HTTPError:
return ExportOutcome.RETRY
return _outcome_for(response.status_code)