Skip to main content
The ServiceMetricsObserver reports each metric a service publishes as its own record, turning service measurements into structured data for logging, monitoring, or analytics. It tracks both latency (time to first byte, first audio, first answer token) and usage (audio seconds, characters, token counts).

Features

  • Emits one record per measurement (never summed)
  • Tracks service latency: TTFB, TTFA, TTFAT
  • Tracks service usage: STT audio seconds, TTS characters, LLM token counts
  • Includes processor name, model, and timestamp with each record
  • Records survive process crashes (not held in memory)
  • Supports custom time sources for testing

Usage

Basic Usage

Add the observer to your pipeline and handle latency and usage events:

Logging to JSON

Emit each record as JSON for external consumption:

Aggregating by Session

Records arrive individually, allowing flexible grouping:

Event Handlers

on_service_latency

Called for each latency measurement (TTFB, TTFA, TTFAT). Receives a ServiceLatencyRecord.
ServiceLatencyRecord fields:

on_service_usage

Called for each usage report (STT, TTS, LLM). Receives a ServiceUsageRecord.
ServiceUsageRecord fields:

Configuration

Constructor Parameters

Callable[[], float]
default:"time.time"
Reads the current time in seconds. Supply a custom function for testing to control timestamps without waiting.

How It Works

The observer monitors MetricsFrame instances flowing through the pipeline. Each frame carries metrics from a service:
  1. Service completes work and emits a MetricsFrame
  2. Observer extracts each metric from the frame
  3. Observer converts metrics to structured records
  4. Observer emits on_service_latency or on_service_usage events
  5. Frame is marked as reported to avoid duplicates
Metrics are reported individually per piece of work. A turn that triggers two LLM inferences reports two on_service_usage events.

Notes

  • No aggregation: Records are never summed. Group them yourself as needed.
  • What’s excluded: Processing time, text aggregation latency, and smart-turn predictions are deliberately absent. They describe internal behavior rather than what users wait for.
  • Duplicate prevention: Each MetricsFrame is reported once, even if relayed through multiple processors.
  • Requires metrics: Services must emit MetricsFrame instances (most do by default).