Trace Calls with OpenTelemetry
@micdrop/otel turns each call into an OpenTelemetry trace. The call is a span, each turn a span under it, and under each turn the transcription, the answer and the voice have their own, with the metrics of the turn as attributes. Any backend that reads OTLP shows them: Langfuse, Jaeger, Grafana Tempo, Honeycomb, Datadog.
A slow turn then reads at a glance: the span that stretches is the step to fix, and the answer span holds the model, its tokens and, if you allow it, the messages it was sent. The OpenTelemetry tutorial builds a traced agent from scratch, with Jaeger and Langfuse.
Installation
npm install @micdrop/otel @opentelemetry/api@micdrop/otel depends on the OpenTelemetry API alone. Spans go wherever the SDK you register sends them, and nowhere when there is none.
Trace a call
Call traceCall() right after creating the server:
import { MicdropServer } from '@micdrop/server'import { traceCall } from '@micdrop/otel'
wss.on('connection', (socket) => { const server = new MicdropServer(socket, { stt, agent, tts }) traceCall(server, { attributes: { 'user.id': userId }, })})It returns the call span, open until the call ends, should you want to add attributes as the call goes.
| Option | Default | What it does |
|---|---|---|
tracer | The tracer of the global provider | Tracer that creates the spans |
context | The active context | Context the call span starts in, to nest it under the span of the request that opened the call |
name | micdrop.call | Name of the call span |
attributes | None | Attributes of the call span |
recordContent | false | Records the messages sent to the model and its answer |
correlateLogs | true | Adds the ids of the call span to every log line of the call |
The spans
| Span | Starts | Ends | Main attributes |
|---|---|---|---|
micdrop.call | traceCall() | The end of the call | Providers, turns, interruptions, total usage, latency percentiles |
micdrop.turn | The user speaking, or the answer for a turn of the assistant | The answer heard, skipped or interrupted | micdrop.turn.index, micdrop.turn.interrupted, micdrop.latency.*_ms, usage |
micdrop.stt | The user speaking | The final transcript | micdrop.latency_ms, micdrop.stt.audio_seconds |
chat <model> | The agent asked for an answer | The text fully written | gen_ai.request.model, gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, micdrop.latency_ms, a first_token event |
micdrop.tts | The first text given to the voice | The last chunk of voice | micdrop.latency_ms, micdrop.tts.characters |
micdrop.realtime | The turn closing | The last chunk of voice | micdrop.latency_ms, tokens of the model |
The answer span follows the GenAI conventions of OpenTelemetry, so backends built for language models, Langfuse among them, show it as a generation with its model and token counts.
Record what was said
The messages are personal data, so they stay out of the trace unless you ask:
traceCall(server, { recordContent: true })Each answer span then holds gen_ai.input.messages, the conversation the model read, system prompt included, and gen_ai.output.messages, its answer and the tools it called. This is how you check what context reached the model when an answer surprises you.
Send the traces to a backend
Register the OpenTelemetry SDK once, when the process starts, before any call:
npm install @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-httpLangfuse
Langfuse receives OTLP over HTTP and authenticates with your project keys:
import { NodeSDK } from '@opentelemetry/sdk-node'import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
const auth = Buffer.from( `${process.env.LANGFUSE_PUBLIC_KEY}:${process.env.LANGFUSE_SECRET_KEY}`).toString('base64')
new NodeSDK({ serviceName: 'voice-agent', traceExporter: new OTLPTraceExporter({ url: 'https://cloud.langfuse.com/api/public/otel/v1/traces', headers: { Authorization: `Basic ${auth}` }, }),}).start()Use the address of your region, or of your own instance, in place of cloud.langfuse.com.
Any OTLP backend
The exporter reads the standard environment variables, so the same code serves every backend:
import { NodeSDK } from '@opentelemetry/sdk-node'import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
new NodeSDK({ serviceName: 'voice-agent', traceExporter: new OTLPTraceExporter(),}).start()OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318OTEL_EXPORTER_OTLP_HEADERS=x-api-key=...To look at traces on your machine, Jaeger listens on that port:
docker run --rm -p 16686:16686 -p 4318:4318 jaegertracing/jaeger:latestThen open http://localhost:16686 and pick the voice-agent service.
Timing
A turn span is written once the turn is over, with the timestamps the server recorded, so its steps sit where they happened even though they reach the exporter together. The call span ends with the call. A process that stops before then loses it, so call shutdown() on the SDK when your server shuts down to flush what is pending.