-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathusage_recorder.py
More file actions
302 lines (252 loc) · 8.32 KB
/
usage_recorder.py
File metadata and controls
302 lines (252 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import asyncio
import logging
import os
from datetime import datetime, timedelta
from dataclasses import dataclass
from typing import Any
from sqlalchemy import delete
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from proxy_app.db_models import UsageEvent
logger = logging.getLogger(__name__)
def get_usage_retention_days() -> int:
raw = os.getenv("USAGE_RETENTION_DAYS", "30")
try:
days = int(raw)
except ValueError:
days = 30
return max(1, days)
async def prune_usage_events(
session_maker: async_sessionmaker[AsyncSession],
*,
retention_days: int | None = None,
) -> int:
active_retention_days = retention_days or get_usage_retention_days()
cutoff = datetime.utcnow() - timedelta(days=active_retention_days)
async with session_maker() as session:
result = await session.execute(
delete(UsageEvent).where(UsageEvent.timestamp < cutoff)
)
await session.commit()
deleted = result.rowcount if result.rowcount is not None else 0
if deleted > 0:
logger.info(
"Pruned %d usage events older than %d days",
deleted,
active_retention_days,
)
return deleted
@dataclass(slots=True)
class UsageEventPayload:
user_id: int | None
api_key_id: int | None
endpoint: str
provider: str | None
model: str | None
request_id: str
status_code: int
prompt_tokens: int | None
completion_tokens: int | None
total_tokens: int | None
cost_usd: float | None
error_type: str | None
error_message: str | None
_SENTINEL = object()
def _maybe_int(value: Any) -> int | None:
if value is None:
return None
try:
return int(value)
except (TypeError, ValueError):
return None
def _maybe_float(value: Any) -> float | None:
if value is None:
return None
try:
return float(value)
except (TypeError, ValueError):
return None
def _actor_field(actor: Any, key: str) -> Any:
if actor is None:
return None
if isinstance(actor, dict):
return actor.get(key)
return getattr(actor, key, None)
class UsageRecorder:
def __init__(
self,
session_maker: async_sessionmaker[AsyncSession],
*,
queue_maxsize: int = 2000,
batch_size: int = 100,
flush_interval_seconds: float = 1.0,
):
self._session_maker = session_maker
self._queue: asyncio.Queue[UsageEventPayload | object] = asyncio.Queue(
maxsize=queue_maxsize
)
self._batch_size = batch_size
self._flush_interval_seconds = flush_interval_seconds
self._worker_task: asyncio.Task[None] | None = None
self._accepting = False
async def start(self) -> None:
if self._worker_task:
return
self._accepting = True
self._worker_task = asyncio.create_task(self._run_worker(), name="usage-recorder")
async def stop(self) -> None:
if not self._worker_task:
return
self._accepting = False
try:
self._queue.put_nowait(_SENTINEL)
except asyncio.QueueFull:
await self._queue.put(_SENTINEL)
await self._worker_task
self._worker_task = None
async def record_usage_event(
self,
*,
actor: Any,
endpoint: str,
model: str | None,
status_code: int,
usage: dict[str, Any] | None,
request_id: str,
error_type: str | None = None,
error_message: str | None = None,
) -> None:
if not self._accepting:
return
usage = usage or {}
payload = UsageEventPayload(
user_id=_maybe_int(_actor_field(actor, "user_id")),
api_key_id=_maybe_int(_actor_field(actor, "api_key_id")),
endpoint=endpoint,
provider=model.split("/", 1)[0] if model else None,
model=model,
request_id=request_id,
status_code=int(status_code),
prompt_tokens=_maybe_int(usage.get("prompt_tokens")),
completion_tokens=_maybe_int(usage.get("completion_tokens")),
total_tokens=_maybe_int(usage.get("total_tokens")),
cost_usd=_maybe_float(usage.get("cost_usd")),
error_type=error_type,
error_message=error_message,
)
try:
self._queue.put_nowait(payload)
except asyncio.QueueFull:
logger.warning("Usage recorder queue full; dropping event request_id=%s", request_id)
async def _run_worker(self) -> None:
batch: list[UsageEventPayload] = []
while True:
item = await self._queue.get()
if item is _SENTINEL:
if batch:
await self._flush_batch(batch)
batch = []
await self._drain_queue(batch)
if batch:
await self._flush_batch(batch)
return
batch.append(item)
if len(batch) >= self._batch_size:
await self._flush_batch(batch)
batch = []
continue
await self._collect_with_timeout(batch)
if batch:
await self._flush_batch(batch)
batch = []
async def _collect_with_timeout(self, batch: list[UsageEventPayload]) -> None:
while len(batch) < self._batch_size:
try:
item = await asyncio.wait_for(
self._queue.get(), timeout=self._flush_interval_seconds
)
except asyncio.TimeoutError:
return
if item is _SENTINEL:
try:
self._queue.put_nowait(_SENTINEL)
except asyncio.QueueFull:
await self._queue.put(_SENTINEL)
return
batch.append(item)
async def _drain_queue(self, batch: list[UsageEventPayload]) -> None:
while True:
try:
item = self._queue.get_nowait()
except asyncio.QueueEmpty:
return
if item is _SENTINEL:
continue
batch.append(item)
async def _flush_batch(self, batch: list[UsageEventPayload]) -> None:
if not batch:
return
rows = [
UsageEvent(
user_id=item.user_id,
api_key_id=item.api_key_id,
endpoint=item.endpoint,
provider=item.provider,
model=item.model,
request_id=item.request_id,
status_code=item.status_code,
prompt_tokens=item.prompt_tokens,
completion_tokens=item.completion_tokens,
total_tokens=item.total_tokens,
cost_usd=item.cost_usd,
error_type=item.error_type,
error_message=item.error_message,
)
for item in batch
]
try:
async with self._session_maker() as session:
session.add_all(rows)
await session.commit()
except Exception:
logger.exception("Failed to flush %d usage events", len(batch))
_usage_recorder: UsageRecorder | None = None
async def start_usage_recorder(
session_maker: async_sessionmaker[AsyncSession],
) -> UsageRecorder:
global _usage_recorder
recorder = UsageRecorder(session_maker)
await recorder.start()
_usage_recorder = recorder
return recorder
async def stop_usage_recorder() -> None:
global _usage_recorder
if _usage_recorder is None:
return
await _usage_recorder.stop()
_usage_recorder = None
def get_usage_recorder() -> UsageRecorder | None:
return _usage_recorder
async def record_usage_event(
*,
actor: Any,
endpoint: str,
model: str | None,
status_code: int,
usage: dict[str, Any] | None,
request_id: str,
error_type: str | None = None,
error_message: str | None = None,
) -> None:
recorder = get_usage_recorder()
if recorder is None:
return
await recorder.record_usage_event(
actor=actor,
endpoint=endpoint,
model=model,
status_code=status_code,
usage=usage,
request_id=request_id,
error_type=error_type,
error_message=error_message,
)