Telemetry¶
Observability and tracing for RoomKit applications. See the Telemetry guide for usage examples.
Core¶
TelemetryProvider ¶
Bases: ABC
Abstract base class for telemetry providers.
Providers collect span and metric data from RoomKit operations.
The default NoopTelemetryProvider has zero overhead.
start_span
abstractmethod
¶
start_span(kind, name, *, parent_id=None, attributes=None, room_id=None, session_id=None, channel_id=None)
Start a new telemetry span.
Returns:
| Type | Description |
|---|---|
str
|
A unique span ID string. |
end_span
abstractmethod
¶
End a previously started span.
set_attribute
abstractmethod
¶
Set an attribute on an active span.
record_metric
abstractmethod
¶
Record a metric value.
get_span_context ¶
Return an opaque context object for the given span.
Used to propagate backend-specific parent context (e.g. OTel Context)
through :func:set_current_span for robust parent linking across
async boundaries.
Returns None by default. Override in providers that carry
backend-specific context (e.g. OpenTelemetryProvider).
inject_context ¶
The fields that name span_id to another process, for its spans
to continue the same trace: what a turn's request carries for an
executor elsewhere (RFC §10.6.9). For OpenTelemetryProvider, what
the application's propagator writes: the W3C trace context
(traceparent, tracestate) and the baggage by default.
Empty by default, for a provider whose spans stay in its process.
extract_context ¶
The backend context the fields of :meth:inject_context name, for
:func:~roomkit.telemetry.context.set_current_span: a provider whose
:meth:start_span reads it back
(:func:~roomkit.telemetry.context.get_current_telemetry_ctx)
starts its spans in the other process's trace. None by default,
and when carrier names no span.
flush ¶
Flush pending spans/metrics without closing the provider.
Called after ending long-lived spans (e.g. VOICE_SESSION) to ensure they are exported promptly rather than waiting for shutdown.
span ¶
Context manager for span lifecycle.
Yields the span ID. Automatically ends the span on exit, recording
error status if an exception occurs: its class, or its text when the
kit's record_sensitive_data asks for it (:func:span_error).
TelemetryConfig
dataclass
¶
TelemetryConfig(provider=None, sample_rate=1.0, enabled_spans=None, metadata=dict(), record_sensitive_data=False, suppressed_hook_triggers=(lambda: {'on_input_audio_level', 'on_output_audio_level', 'on_vad_audio_level'})())
Configuration for telemetry collection.
Attributes:
| Name | Type | Description |
|---|---|---|
provider |
TelemetryProvider | None
|
The telemetry provider to use. Defaults to
|
sample_rate |
float
|
Fraction of spans to record (0.0 to 1.0). Default 1.0 records all spans. |
enabled_spans |
set[SpanKind] | None
|
If set, only these span kinds are recorded.
|
record_sensitive_data |
bool
|
Whether spans carry what can hold personal
data: a sender's address ( |
Span
dataclass
¶
Span(id=(lambda: hex[:16])(), kind=CUSTOM, name='', parent_id=None, attributes=dict(), start_time=(lambda: now(UTC))(), end_time=None, status='ok', error_message=None, room_id=None, session_id=None, channel_id=None)
Represents a telemetry span.
Attr ¶
Well-known attribute key constants for telemetry spans and metrics.
SpanKind ¶
Bases: StrEnum
Span classifications for telemetry.
Built-in Providers¶
ConsoleTelemetryProvider ¶
Bases: TelemetryProvider
Logs span start/end and metrics to roomkit.telemetry logger.
Zero external dependencies — uses only Python's built-in logging. Useful for development and debugging.
Example::
import logging
logging.basicConfig(level=logging.INFO)
from roomkit import RoomKit
from roomkit.telemetry import ConsoleTelemetryProvider
kit = RoomKit(telemetry=ConsoleTelemetryProvider())
OpenTelemetryProvider ¶
OpenTelemetryProvider(*, tracer_provider=None, meter_provider=None, service_name='roomkit', metadata=None, shutdown_flush_timeout=4.0)
Bases: TelemetryProvider
Bridges RoomKit telemetry to OpenTelemetry SDK.
Requires opentelemetry-api and opentelemetry-sdk::
pip install opentelemetry-api opentelemetry-sdk
Example::
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
from roomkit import RoomKit
from roomkit.telemetry import OpenTelemetryProvider
kit = RoomKit(telemetry=OpenTelemetryProvider(tracer_provider=provider))
Exporting never runs on the event loop. force_flush exports in the
thread that calls it, behind the exporter's own retries, and the SDK does
not honour its timeout: called from the loop, a slow collector froze every
task on it. :meth:flush (the end of a voice session) hands it to a thread
and returns at once; :meth:close waits for it at most
shutdown_flush_timeout seconds, then gives up the spans not yet out.
get_span_context ¶
Return an OTel Context with the given span set as current.
Overrides :meth:TelemetryProvider.get_span_context to return a
native OTel Context for robust parent linking across async
boundaries.
inject_context ¶
span_id's trace context, written by the application's
propagator: W3C traceparent and tracestate, and the ambient
baggage, by default (OTEL_PROPAGATORS=tracecontext keeps the
baggage in its process).
extract_context ¶
The OpenTelemetry context the application's propagator reads from
carrier; None when it names no span.
NoopTelemetryProvider ¶
Bases: TelemetryProvider
Default telemetry provider that does nothing.
All methods are no-ops with zero overhead. This is the default when no telemetry provider is configured.
MockTelemetryProvider ¶
Bases: TelemetryProvider
Records all spans and metrics in lists for test assertions.
Example::
telemetry = MockTelemetryProvider()
kit = RoomKit(telemetry=telemetry)
# ... run operations ...
assert len(telemetry.spans) > 0
stt_spans = telemetry.get_spans(SpanKind.STT_TRANSCRIBE)
assert stt_spans[0].attributes["stt.text_length"] > 0