🎤Micdrop

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

Terminal window
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.

OptionDefaultWhat it does
tracerThe tracer of the global providerTracer that creates the spans
contextThe active contextContext the call span starts in, to nest it under the span of the request that opened the call
namemicdrop.callName of the call span
attributesNoneAttributes of the call span
recordContentfalseRecords the messages sent to the model and its answer
correlateLogstrueAdds the ids of the call span to every log line of the call

The spans

SpanStartsEndsMain attributes
micdrop.calltraceCall()The end of the callProviders, turns, interruptions, total usage, latency percentiles
micdrop.turnThe user speaking, or the answer for a turn of the assistantThe answer heard, skipped or interruptedmicdrop.turn.index, micdrop.turn.interrupted, micdrop.latency.*_ms, usage
micdrop.sttThe user speakingThe final transcriptmicdrop.latency_ms, micdrop.stt.audio_seconds
chat <model>The agent asked for an answerThe text fully writtengen_ai.request.model, gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, micdrop.latency_ms, a first_token event
micdrop.ttsThe first text given to the voiceThe last chunk of voicemicdrop.latency_ms, micdrop.tts.characters
micdrop.realtimeThe turn closingThe last chunk of voicemicdrop.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:

Terminal window
npm install @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-http

Langfuse

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()
Terminal window
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_EXPORTER_OTLP_HEADERS=x-api-key=...

To look at traces on your machine, Jaeger listens on that port:

Terminal window
docker run --rm -p 16686:16686 -p 4318:4318 jaegertracing/jaeger:latest

Then 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.