Skip to content

[Enhancement] Add OpenTelemetry instrumentation for request tracing and latency metrics #719

@deepgram-robot

Description

@deepgram-robot

Summary

Add optional OpenTelemetry instrumentation to the Python SDK that automatically creates spans and records metrics for all Deepgram API calls — STT (REST + WebSocket), TTS (REST + WebSocket), Voice Agent sessions, and Audio Intelligence requests.

Problem it solves

Enterprise teams deploying Deepgram in production need distributed tracing and metrics to monitor performance, debug latency issues, and track usage across their application stack. Currently, developers must manually wrap every SDK call with OpenTelemetry instrumentation, which is error-prone, inconsistent, and misses internal SDK timing (WebSocket connection setup, audio chunking, response parsing). A built-in instrumentation layer provides consistent, zero-effort observability.

Proposed API

# Opt-in via instrumentor (standard OTEL pattern)
from deepgram.instrumentation.opentelemetry import DeepgramInstrumentor

DeepgramInstrumentor().instrument()

# All subsequent SDK calls automatically create spans and record metrics
client = DeepgramClient(api_key="...")
response = client.listen.rest.v("1").transcribe_file(payload, options)
# → Span: "deepgram.stt.rest" with attributes: model, language, duration_ms, audio_duration_s
# → Metrics: deepgram.stt.latency (histogram), deepgram.stt.requests (counter)

# WebSocket streaming sessions create parent span + child spans per result
async with client.listen.websocket.v("1").transcribe_stream(options) as ws:
    # → Span: "deepgram.stt.websocket" (parent)
    # → Metric: deepgram.stt.ttfb_ms (time to first result)
    ...

Metrics emitted:

  • deepgram.stt.latency_ms (histogram) — request duration
  • deepgram.stt.ttfb_ms (histogram) — time to first byte/result for streaming
  • deepgram.tts.latency_ms (histogram) — TTS request duration
  • deepgram.agent.session_duration_ms (histogram) — voice agent session length
  • deepgram.requests (counter) — total requests by API and status
  • deepgram.errors (counter) — errors by type

Acceptance criteria

  • Optional dependency: pip install deepgram-sdk[opentelemetry]
  • Follows OpenTelemetry Python instrumentation conventions
  • Creates spans for REST calls, WebSocket sessions, and voice agent sessions
  • Records latency histograms and request counters
  • Captures TTFB for streaming WebSocket sessions
  • Adds request_id, model, and language as span attributes
  • Zero performance impact when not instrumented
  • Documented with usage example
  • Compatible with existing API

Raised by the DX intelligence system.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions