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 aServiceLatencyRecord.
on_service_usage
Called for each usage report (STT, TTS, LLM). Receives aServiceUsageRecord.
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 monitorsMetricsFrame instances flowing through the pipeline. Each frame carries metrics from a service:
- Service completes work and emits a
MetricsFrame - Observer extracts each metric from the frame
- Observer converts metrics to structured records
- Observer emits
on_service_latencyoron_service_usageevents - Frame is marked as reported to avoid duplicates
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
MetricsFrameis reported once, even if relayed through multiple processors. - Requires metrics: Services must emit
MetricsFrameinstances (most do by default).