forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjourneys.py
More file actions
408 lines (336 loc) · 14.7 KB
/
Copy pathjourneys.py
File metadata and controls
408 lines (336 loc) · 14.7 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""Closed, privacy-safe outcome metrics for real product traffic journeys."""
from __future__ import annotations
import asyncio
import logging
import threading
from collections.abc import AsyncIterable, AsyncIterator, Callable, Mapping
from datetime import datetime, timezone
from time import monotonic
from typing import Literal, TypeVar, cast
from starlette.requests import ClientDisconnect
from utils.metrics import (
OMI_CAPTURE_FINALIZATION_RECONCILIATIONS_TOTAL,
OMI_CLIENT_JOURNEY_ACCEPTED_TOTAL,
OMI_CLIENT_JOURNEY_DURATION_SECONDS,
OMI_CLIENT_JOURNEY_ISSUES_TOTAL,
OMI_CLIENT_JOURNEY_TERMINAL_TOTAL,
OMI_JOURNEY_ACCEPTED_TOTAL,
OMI_JOURNEY_LATENCY_SECONDS,
OMI_JOURNEY_TERMINAL_TOTAL,
)
from utils.journey_metrics_contract import (
ClientJourneyIssueClass,
ClientJourneyName,
ClientJourneyOutcome,
ClientKind,
bounded_client_journey,
bounded_client_journey_issue_class,
bounded_client_journey_outcome,
bounded_client_kind,
resolve_client_kind,
)
JourneyName = Literal['chat_response', 'pusher_session', 'capture_finalization']
JourneyOutcome = Literal['success', 'failure', 'cancelled', 'stale']
ReconciliationOutcome = Literal['requeued', 'enqueue_failed']
_JOURNEYS = frozenset({'chat_response', 'pusher_session', 'capture_finalization'})
_OUTCOMES = frozenset({'success', 'failure', 'cancelled', 'stale'})
_RECONCILIATION_OUTCOMES = frozenset({'requeued', 'enqueue_failed'})
def _journey(value: str) -> JourneyName:
if value not in _JOURNEYS:
raise ValueError(f'unknown journey: {value}')
return cast(JourneyName, value)
def _outcome(value: str) -> JourneyOutcome:
if value not in _OUTCOMES:
raise ValueError(f'unknown journey outcome: {value}')
return cast(JourneyOutcome, value)
def record_journey_accepted(journey: JourneyName) -> None:
"""Record an accepted journey after its authoritative durable/protocol boundary."""
OMI_JOURNEY_ACCEPTED_TOTAL.labels(journey=_journey(journey)).inc()
def record_journey_terminal(journey: JourneyName, outcome: JourneyOutcome, elapsed_seconds: float) -> None:
"""Record exactly one terminal outcome and its elapsed time for one journey."""
labels = {'journey': _journey(journey), 'outcome': _outcome(outcome)}
OMI_JOURNEY_TERMINAL_TOTAL.labels(**labels).inc()
OMI_JOURNEY_LATENCY_SECONDS.labels(**labels).observe(max(0.0, elapsed_seconds))
class JourneyAttempt:
"""In-process accepted journey with a one-shot terminal outcome."""
def __init__(self, journey: JourneyName) -> None:
self.journey: JourneyName = _journey(journey)
self.started_at = monotonic()
self._finished = False
record_journey_accepted(self.journey)
@property
def finished(self) -> bool:
return self._finished
def finish(self, outcome: JourneyOutcome) -> None:
if self._finished:
return
self._finished = True
record_journey_terminal(self.journey, outcome, monotonic() - self.started_at)
_StreamItem = TypeVar('_StreamItem')
class _ObservedClientJourneyStream(AsyncIterator[_StreamItem]):
"""Lazily start an observed stream and close abandoned attempts."""
def __init__(
self,
attempt: 'ClientJourneyAttempt',
iterator_factory: Callable[[], AsyncIterator[_StreamItem]],
) -> None:
self._attempt = attempt
self._iterator_factory = iterator_factory
self._iterator: AsyncIterator[_StreamItem] | None = None
def __aiter__(self) -> '_ObservedClientJourneyStream[_StreamItem]':
return self
async def __anext__(self) -> _StreamItem:
if self._iterator is None:
self._iterator = self._iterator_factory()
return await self._iterator.__anext__()
async def aclose(self) -> None:
if self._iterator is None:
self._attempt.abandon_stream()
return
close = getattr(self._iterator, 'aclose', None)
if close is not None:
await close()
if not self._attempt.finished:
self._attempt.abandon_stream()
def __del__(self) -> None:
attempt = getattr(self, '_attempt', None)
if attempt is not None and not attempt.finished:
try:
attempt.abandon_stream()
except Exception:
# Object finalizers may run during interpreter shutdown.
pass
logger = logging.getLogger(__name__)
_RECORDER_WARNING_INTERVAL_SECONDS = 60.0
_recorder_warning_lock = threading.Lock()
_last_recorder_warning_at = 0.0
def _record_fail_open(what: str, record: Callable[[], None]) -> None:
"""Run a metric write that must never propagate into a product request.
Observability is not allowed to break the thing it observes. It is equally
not allowed to fail silently: a recorder that quietly stops writing is
indistinguishable from a healthy path with nothing to report, which is the
failure mode this whole metric family exists to make visible. Failures are
swallowed for the caller and logged at a bounded rate for the operator.
"""
global _last_recorder_warning_at
try:
record()
except Exception:
now = monotonic()
with _recorder_warning_lock:
due = now - _last_recorder_warning_at >= _RECORDER_WARNING_INTERVAL_SECONDS
if due:
_last_recorder_warning_at = now
if due:
logger.warning('client_journey_metric_record_failed what=%s', what, exc_info=True)
def record_client_journey_accepted(journey: object, client_kind: object) -> None:
"""Record a bounded acceptance event without creating labels from raw input."""
_record_fail_open(
'accepted',
lambda: OMI_CLIENT_JOURNEY_ACCEPTED_TOTAL.labels(
journey=bounded_client_journey(journey),
client_kind=bounded_client_kind(client_kind),
).inc(),
)
def record_client_journey_terminal(
journey: object,
client_kind: object,
outcome: object,
elapsed_seconds: float,
*,
issue_class: object | None = None,
) -> None:
"""Record one bounded terminal event plus duration and optional issue detail.
This function is also the cross-process API: a durable worker can pass an
elapsed duration derived from persisted acceptance time. These counters are
not queue state and must never be subtracted to infer in-flight work.
"""
def _record() -> None:
journey_label = bounded_client_journey(journey)
client_kind_label = bounded_client_kind(client_kind)
outcome_label = bounded_client_journey_outcome(outcome)
OMI_CLIENT_JOURNEY_TERMINAL_TOTAL.labels(
journey=journey_label,
client_kind=client_kind_label,
outcome=outcome_label,
).inc()
OMI_CLIENT_JOURNEY_DURATION_SECONDS.labels(
journey=journey_label,
outcome=outcome_label,
).observe(max(0.0, elapsed_seconds))
if outcome_label in {'failure', 'degraded', 'unknown'}:
OMI_CLIENT_JOURNEY_ISSUES_TOTAL.labels(
journey=journey_label,
client_kind=client_kind_label,
issue_class=bounded_client_journey_issue_class(issue_class),
).inc()
_record_fail_open('terminal', _record)
class ClientJourneyAttempt:
"""Fail-closed client journey attempt with streaming-aware completion.
A normal context exit without an explicit terminal is a failure. Attaching
a stream transfers terminal ownership to the returned iterator so a
``StreamingResponse`` can consume it after the context has exited.
"""
def __init__(
self,
journey: ClientJourneyName,
client_kind: ClientKind,
*,
clock: Callable[[], float] = monotonic,
) -> None:
self.journey = bounded_client_journey(journey)
self.client_kind = bounded_client_kind(client_kind)
self._clock = clock
self.started_at = clock()
self._outcome: ClientJourneyOutcome | None = None
self._issue_class: ClientJourneyIssueClass | None = None
self._stream_active = False
self._stream_attached = False
self._stream_success_requested = False
record_client_journey_accepted(self.journey, self.client_kind)
@property
def finished(self) -> bool:
return self._outcome is not None
@property
def outcome(self) -> ClientJourneyOutcome | None:
return self._outcome
@property
def issue_class(self) -> ClientJourneyIssueClass | None:
return self._issue_class
def __enter__(self) -> 'ClientJourneyAttempt':
return self
def __exit__(self, exc_type: type[BaseException] | None, _exc: BaseException | None, _tb: object) -> bool:
if not self.finished and exc_type is not None:
self.fail('unknown')
elif not self.finished and not self._stream_attached:
self.fail('incomplete_attempt')
return False
def finish(self, outcome: object, *, issue_class: object | None = None) -> None:
outcome_label = bounded_client_journey_outcome(outcome)
if outcome_label == 'success' and self._stream_attached:
self._stream_success_requested = True
return
if self.finished:
return
self._outcome = outcome_label
self._issue_class = (
bounded_client_journey_issue_class(issue_class)
if outcome_label in {'failure', 'degraded', 'unknown'}
else None
)
record_client_journey_terminal(
self.journey,
self.client_kind,
outcome_label,
self._clock() - self.started_at,
issue_class=self._issue_class,
)
def succeed(self) -> None:
self.finish('success')
def fail(self, issue_class: object = 'unknown') -> None:
self.finish('failure', issue_class=issue_class)
def degrade(self, issue_class: object) -> None:
self.finish('degraded', issue_class=issue_class)
def cancel(self) -> None:
self.finish('cancelled')
def abandon_stream(self) -> None:
"""Terminalize an attached stream that was not exhausted."""
self._stream_active = False
self._stream_attached = False
self.cancel()
def observe_stream(
self,
source: AsyncIterable[_StreamItem],
*,
success_when: Callable[[_StreamItem], bool],
failure_when: Callable[[_StreamItem], bool],
failure_class: object = 'provider_error',
missing_success_class: object = 'empty_answer',
) -> AsyncIterator[_StreamItem]:
"""Yield a stream and terminalize only after its semantic contract is known.
``failure_when`` terminalizes immediately, so an in-band error frame
wins over a later success marker. Seeing a success item is necessary but
not sufficient: clean exhaustion is also required, so a later source
exception cannot leave a success.
"""
if self._stream_attached:
raise RuntimeError('a stream is already attached to this journey attempt')
self._stream_attached = True
async def observed() -> AsyncIterator[_StreamItem]:
success_observed = False
self._stream_active = True
try:
async for item in source:
if not self.finished:
if failure_when(item):
self.fail(failure_class)
elif success_when(item):
success_observed = True
yield item
except (asyncio.CancelledError, GeneratorExit, ClientDisconnect, OSError):
self.abandon_stream()
raise
except BaseException:
self._stream_active = False
self._stream_attached = False
self.fail(failure_class)
raise
else:
self._stream_active = False
self._stream_attached = False
if not self.finished:
if success_observed or self._stream_success_requested:
self.succeed()
else:
self.fail(missing_success_class)
finally:
self._stream_active = False
self._stream_attached = False
if not self.finished:
self.fail('incomplete_stream')
return _ObservedClientJourneyStream(self, observed)
def record_conversation_finalization_client_terminal(
outcome: object,
job: Mapping[str, object],
*,
client_kind: object | None = None,
issue_class: object | None = None,
) -> None:
"""Terminalize a client journey only when its originating client is known."""
def _record() -> None:
resolved_client_kind = client_kind
if resolved_client_kind is None:
if 'client_platform' not in job:
return
resolved_client_kind = resolve_client_kind(x_app_platform=job.get('client_platform'), user_agent=None)
accepted_at = job.get('created_at')
if not isinstance(accepted_at, datetime):
logger.warning('Conversation finalization client metric missing accepted timestamp')
return
if accepted_at.tzinfo is None:
accepted_at = accepted_at.replace(tzinfo=timezone.utc)
record_client_journey_terminal(
'conversation_finalization',
bounded_client_kind(resolved_client_kind),
outcome,
(datetime.now(timezone.utc) - accepted_at).total_seconds(),
issue_class=issue_class,
)
_record_fail_open('conversation finalization terminal', _record)
def record_capture_finalization_terminal(outcome: JourneyOutcome, accepted_at: datetime | None) -> None:
"""Terminalize a durable capture job using its persisted acceptance time."""
if accepted_at is None:
# Legacy jobs can predate created_at. Keep the outcome visible without
# fabricating a latency value.
labels = {'journey': _journey('capture_finalization'), 'outcome': _outcome(outcome)}
OMI_JOURNEY_TERMINAL_TOTAL.labels(**labels).inc()
return
if accepted_at.tzinfo is None:
accepted_at = accepted_at.replace(tzinfo=timezone.utc)
elapsed_seconds = (datetime.now(timezone.utc) - accepted_at).total_seconds()
record_journey_terminal('capture_finalization', outcome, elapsed_seconds)
def record_capture_finalization_reconciliation(outcome: ReconciliationOutcome) -> None:
"""Record a bounded reconciliation event for stale capture work."""
if outcome not in _RECONCILIATION_OUTCOMES:
raise ValueError(f'unknown finalization reconciliation outcome: {outcome}')
OMI_CAPTURE_FINALIZATION_RECONCILIATIONS_TOTAL.labels(outcome=outcome).inc()