Skip to content
Merged
3 changes: 3 additions & 0 deletions litellm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def _dev_env_hot_reload_enabled() -> bool:
from litellm.types.integrations.newrelic import NewRelicInitParams
from litellm.litellm_core_utils.core_helpers import drop_params_env_flag
from litellm.types.integrations.pointfive import PointFiveInitParams
from litellm.types.integrations.zerobus import ZerobusInitParams
from litellm._logging import (
set_verbose,
_turn_on_debug,
Expand Down Expand Up @@ -157,6 +158,7 @@ def _dev_env_hot_reload_enabled() -> bool:
"deepeval",
"s3_v2",
"pointfive",
"zerobus",
"aws_sqs",
"vector_store_pre_call_hook",
"dotprompt",
Expand Down Expand Up @@ -441,6 +443,7 @@ def _dev_env_hot_reload_enabled() -> bool:
datadog_params: Optional[Union[DatadogInitParams, Dict]] = None
newrelic_params: Optional[Union[NewRelicInitParams, Dict]] = None
pointfive_params: Optional[Union[PointFiveInitParams, Mapping[str, object]]] = None
zerobus_params: Optional[Union[ZerobusInitParams, Mapping[str, object]]] = None
aws_sqs_callback_params: Optional[Dict] = None
generic_logger_headers: Optional[Dict] = None
default_key_generate_params: Optional[Dict] = None
Expand Down
39 changes: 39 additions & 0 deletions litellm/integrations/callback_configs.json
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,45 @@
},
"description": "PointFive Logging Integration"
},
{
"id": "zerobus",
"displayName": "Databricks Zerobus",
"logo": "databricks.svg",
"supports_key_team_logging": false,
"dynamic_params": {
"ZEROBUS_WORKSPACE_URL": {
"type": "text",
"ui_name": "Workspace URL",
"description": "Databricks workspace URL, e.g. https://dbc-a1b2c3d4-e5f6.cloud.databricks.com",
"required": true
},
"ZEROBUS_SERVER_ENDPOINT": {
"type": "text",
"ui_name": "Zerobus Endpoint",
"description": "Zerobus ingest endpoint, e.g. https://<workspace-id>.zerobus.<region>.cloud.databricks.com",
"required": true
},
"ZEROBUS_CLIENT_ID": {
"type": "text",
"ui_name": "Service Principal Client ID",
"description": "OAuth client id of a service principal with USE CATALOG, USE SCHEMA, SELECT and MODIFY on the table",
"required": true
},
"ZEROBUS_CLIENT_SECRET": {
"type": "password",
"ui_name": "Service Principal Client Secret",
"description": "OAuth client secret of the service principal",
"required": true
},
"ZEROBUS_TABLE_NAME": {
"type": "text",
"ui_name": "Table",
"description": "Fully qualified Unity Catalog table, catalog.schema.table, created with the LiteLLM trace schema",
"required": true
}
},
"description": "Databricks Zerobus Ingest Logging Integration"
},
{
"id": "s3",
"displayName": "S3",
Expand Down
5 changes: 5 additions & 0 deletions litellm/integrations/zerobus/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Databricks Zerobus logging integration for LiteLLM."""

from litellm.integrations.zerobus.logger import ZerobusLogger

__all__ = ("ZerobusLogger",)
161 changes: 161 additions & 0 deletions litellm/integrations/zerobus/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
"""
Writes rows to a Unity Catalog table through the Zerobus Ingest REST API.

Zerobus only accepts a Databricks OAuth token minted for its own resource and scoped to
the target table's privileges, so the client mints that token itself with the service
principal's client credentials and reuses it until shortly before it expires.
"""

import asyncio
import base64
import json
import time
from collections.abc import Callable, Mapping, Sequence
from typing import Final

import httpx
from pydantic import BaseModel, ValidationError

import litellm
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
from litellm.types.integrations.zerobus import (
RETRYABLE_INGEST_STATUS_CODES,
TOKEN_REFRESH_LEEWAY_SECONDS,
ZerobusAccessToken,
ZerobusConnection,
ZerobusIngestFailure,
)

TOKEN_PATH: Final = "/oidc/v1/token"
OAUTH_SCOPE: Final = "all-apis"


class _TokenResponse(BaseModel):
access_token: str
expires_in: float = 3600


class ZerobusIngestError(Exception):
"""A batch could not be written and the failure is worth retrying."""


def zerobus_resource(workspace_id: str) -> str:
return f"api://databricks/workspaces/{workspace_id}/zerobusDirectWriteApi"


def authorization_details(table_name: str) -> str:
"""The Unity Catalog privileges Zerobus requires the token to carry, as the token endpoint expects them."""
catalog, schema, _table = table_name.split(".", 2)
return json.dumps(
(
{
"type": "unity_catalog_privileges",
"privileges": ("USE CATALOG",),
"object_type": "CATALOG",
"object_full_path": catalog,
},
{
"type": "unity_catalog_privileges",
"privileges": ("USE SCHEMA",),
"object_type": "SCHEMA",
"object_full_path": f"{catalog}.{schema}",
},
{
"type": "unity_catalog_privileges",
"privileges": ("SELECT", "MODIFY"),
"object_type": "TABLE",
"object_full_path": table_name,
},
)
)


def insert_url(connection: ZerobusConnection) -> str:
return f"{connection.server_endpoint.rstrip('/')}/zerobus/v1/tables/{connection.table_name}/insert"


def token_url(connection: ZerobusConnection) -> str:
return f"{connection.workspace_url.rstrip('/')}{TOKEN_PATH}"


def _basic_auth(client_id: str, client_secret: str) -> str:
return "Basic " + base64.b64encode(f"{client_id}:{client_secret}".encode()).decode()


def _status_failure(what: str, error: httpx.HTTPStatusError) -> ZerobusIngestFailure:
status: Final = error.response.status_code
return ZerobusIngestFailure(
detail=f"{what} returned {status}: {error.response.text}"[:500],
retryable=status in RETRYABLE_INGEST_STATUS_CODES,
)


class ZerobusIngestClient:
def __init__(
self,
connection: ZerobusConnection,
http_client: AsyncHTTPHandler,
clock: Callable[[], float] = time.time,
) -> None:
self.connection: Final = connection
self.http_client: Final = http_client
self.clock: Final = clock
self._token: ZerobusAccessToken | None = None
self._token_lock: Final = asyncio.Lock()

async def insert(self, rows: Sequence[Mapping[str, object]]) -> ZerobusIngestFailure | None:
"""Write ``rows`` as one request. ``None`` means Zerobus accepted every row."""
token: Final = await self.access_token()
if isinstance(token, ZerobusIngestFailure):
return token
try:
await self.http_client.post(
insert_url(self.connection),
content=json.dumps([dict(row) for row in rows]).encode(),
headers={"Content-Type": "application/json", "Authorization": f"Bearer {token.value}"},
)
except httpx.HTTPStatusError as error:
if error.response.status_code == 401:
self._token = None
return ZerobusIngestFailure(detail="insert returned 401, token discarded", retryable=True)
return _status_failure("insert", error)
except (httpx.HTTPError, litellm.Timeout) as error:
return ZerobusIngestFailure(detail=f"insert failed: {error}", retryable=True)
return None

async def access_token(self) -> ZerobusAccessToken | ZerobusIngestFailure:
"""The cached token while it has more than the leeway left, otherwise a fresh one."""
async with self._token_lock:
cached: Final = self._token
if cached is not None and cached.expires_at - self.clock() > TOKEN_REFRESH_LEEWAY_SECONDS:
return cached
minted: Final = await self._mint_token()
if isinstance(minted, ZerobusAccessToken):
self._token = minted
return minted

async def _mint_token(self) -> ZerobusAccessToken | ZerobusIngestFailure:
connection: Final = self.connection
try:
response: Final = await self.http_client.post(
token_url(connection),
data={
"grant_type": "client_credentials",
"scope": OAUTH_SCOPE,
"resource": zerobus_resource(connection.workspace_id),
"authorization_details": authorization_details(connection.table_name),
},
headers={
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": _basic_auth(connection.client_id, connection.client_secret),
},
)
except httpx.HTTPStatusError as error:
return _status_failure("token request", error)
except (httpx.HTTPError, litellm.Timeout) as error:
return ZerobusIngestFailure(detail=f"token request failed: {error}", retryable=True)
try:
parsed: Final = _TokenResponse.model_validate_json(response.text)
except ValidationError as error:
return ZerobusIngestFailure(detail=f"token response was not understood: {error}", retryable=False)
return ZerobusAccessToken(value=parsed.access_token, expires_at=self.clock() + parsed.expires_in)
Loading
Loading