OpenTelemetry in Node.js services: which spans you still have to instrument by hand
When we added OpenTelemetry to one of our Node.js services, the expectation was reasonable: install the auto-instrumentation package, point the exporter at our tracing backend, and get end-to-end visibility without touching business logic. For the HTTP side and the database side, that is exactly what happened. For everything that ran after the HTTP request had already finished -a job on a queue, a chain of in-process calculations with no network call in sight- the trace dashboard stayed silent, not because anything was broken, but because it had never been instrumented in the first place.
What auto-instrumentation covers, and why it covers exactly that
@opentelemetry/auto-instrumentations-node does not instrument "the code": it instruments known libraries, by patching their entry and exit points. When the service starts, the package scans the runtime for installed versions of http, express, pg, ioredis, graphql, the AWS SDK, and for each one it patches the functions that represent an I/O boundary: the incoming HTTP request, the SQL query going out to Postgres, the command going out to Redis. Each of those calls becomes a span because the library executing it has, inside the auto-instrumentation package, a module that knows exactly where to intercept it.
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const sdk = new NodeSDK({
traceExporter: exporter,
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
With this in place, an HTTP request to an endpoint that runs a Postgres query already produces a complete trace: a root span for the incoming request, a child span for the query, with the SQL, the duration and the destination visible without a single line of instrumentation written in the handler. That is genuinely useful, and it is the reason it is worth installing the package from day one of any new service. But the mechanism that makes it possible -patching known libraries- is exactly what defines its limits: anything that does not go through one of those libraries produces no span, no matter how long it takes or how much it matters for understanding what happened.
Where the trail breaks: the jump into a queue
Our service receives an HTTP request, validates the payload, and queues a job on Redis with BullMQ to process it asynchronously. A separate worker -same Node.js process, a different BullMQ Worker- picks the job off the queue and processes it minutes later. The HTTP request gets instrumented automatically, with its root span and its I/O child spans. The job processing, when it happens, starts with no trace context at all: BullMQ has no idea OpenTelemetry exists, and the worker picking up the job has no way of knowing which trace the originating request belonged to, because that information never traveled along with the job's data.
The reason is not that BullMQ is poorly designed. Context propagation in OpenTelemetry is built for transports that already have a natural place for metadata to travel alongside the payload -the headers of an HTTP request, the metadata of a gRPC message. A job on a Redis queue is, as far as OpenTelemetry is concerned, an arbitrary data payload with no such reserved slot. If we want the trace context to travel with the job, we have to serialize it ourselves into the payload, and deserialize it ourselves on the other end.
const { propagation, context, trace } = require('@opentelemetry/api');
// When queuing: serialize the active context into the job payload
async function queueJob(queue, data) {
const carrier = {};
propagation.inject(context.active(), carrier);
await queue.add('process-order', { ...data, traceContext: carrier });
}
// When processing: rebuild the context before opening the worker's span
worker.on('job', async (job) => {
const parentContext = propagation.extract(context.active(), job.data.traceContext);
const tracer = trace.getTracer('order-worker');
await context.with(parentContext, async () => {
await tracer.startActiveSpan('process-order', async (span) => {
await processOrder(job.data);
span.end();
});
});
});
Without this manual bridge, what we see in the tracing backend are two disconnected traces: one for the HTTP request, which ends the moment the job gets queued, and another -if we even bothered to instrument anything on the worker's side- that starts from scratch with no link to the first one. To debug an order that took longer than expected, that means manually correlating two traces by a business ID, exactly the work a distributed trace exists to save you from.
In-process business logic stays invisible even inside an instrumented span
There is a second, subtler limit that has nothing to do with jumping between processes. Inside the same HTTP handler, instrumented end-to-end automatically, we have a function that computes an order's final price: it applies discount rules, rounding, taxes, all in memory, with no network call and no database call. That function can take longer than expected -for instance, if one of the discount rules iterates over a list that is larger than usual- and the complete trace will not show it, because there is no instrumented library for that function to call into. The HTTP handler's span measures the total time, but that total time shows up as an opaque block between the last I/O child span and the response, with no indication of what happened inside it.
There is no shortcut here: that logic has to be wrapped in a manual span.
const tracer = trace.getTracer('orders');
async function computeFinalPrice(order) {
return tracer.startActiveSpan('compute-final-price', (span) => {
try {
const discounted = applyDiscounts(order);
const taxed = applyTaxes(discounted);
span.setAttribute('order.discount_rules_applied', discounted.rulesApplied.length);
return taxed;
} finally {
span.end();
}
});
}
We do not instrument every function in the service with this pattern, because doing that would turn every trace into noise: hundreds of one-millisecond spans that help diagnose nothing. The rule we use is simpler: we hand-instrument the logic that, from experience or from suspicion, is a real candidate for the bottleneck when something gets slow and the automatic trace does not explain why. Calculations over collections that can grow, data transformations with significant volume, any stretch that has already shown up as suspicious in a prior manual profiling pass.
What we check before trusting a trace as a complete diagnosis
Before assuming an OpenTelemetry trace tells the complete story of a request, we confirm that no stretch of the flow crosses a boundary auto-instrumentation does not cover -a queue, a worker in another process, a message to another service over a transport with no instrumentation of its own- without us having added the manual context-propagation bridge. We check that any in-process business logic we already know is expensive has its own span, instead of staying hidden inside a parent span's total time. And when a trace shows an unexplained block of time between two I/O spans, we treat that as a signal that manual instrumentation is missing in that stretch, not as a complete trace that simply found nothing to report.