39
39
SpanAttributeEnum ,
40
40
call_answer_latency ,
41
41
call_cutoff_latency ,
42
+ call_stt_complete_latency ,
42
43
gauge_set ,
43
44
tracer ,
44
45
)
@@ -233,6 +234,17 @@ async def _commit_answer(
233
234
if wait :
234
235
await last_chat
235
236
237
+ async def _compute_stt_metrics () -> None :
238
+ """
239
+ Report the recognition latency.
240
+ """
241
+ start = time .monotonic ()
242
+ await stt_complete_gate .wait ()
243
+ gauge_set (
244
+ metric = call_stt_complete_latency ,
245
+ value = time .monotonic () - start ,
246
+ )
247
+
236
248
async def _response_callback (_retry : bool = False ) -> None :
237
249
"""
238
250
Triggered when the audio buffer needs to be processed.
@@ -243,6 +255,9 @@ async def _response_callback(_retry: bool = False) -> None:
243
255
nonlocal answer_start
244
256
answer_start = time .monotonic ()
245
257
258
+ # Report the STT metrics
259
+ stt_metrics_task = asyncio .create_task (_compute_stt_metrics ())
260
+
246
261
# Wait the complete recognition for 50ms maximum
247
262
try :
248
263
await asyncio .wait_for (stt_complete_gate .wait (), timeout = 0.05 )
@@ -277,8 +292,11 @@ async def _response_callback(_retry: bool = False) -> None:
277
292
)
278
293
)
279
294
280
- # Process the response
281
- await _commit_answer (wait = True )
295
+ # Process the response and wait for latency metrics
296
+ await asyncio .gather (
297
+ _commit_answer (wait = False ),
298
+ stt_metrics_task ,
299
+ )
282
300
283
301
# First call
284
302
if len (call .messages ) <= 1 :
0 commit comments