Skip to content

tracing

Integrates OpenTelemetry distributed tracing into the ergo pipeline. Extracts W3C trace context from incoming request headers, starts a root ergo.pipeline span, and returns span metadata on the domain accumulator for downstream correlation (e.g., log enrichment via logger).

When no OTEL SDK is registered, all operations are no-ops — zero overhead for the default case.

Pipeline stage: Cross-cutting (before logger)

import { tracing } from "@centralping/ergo";
OptionTypeDefaultDescription
serviceNamestring'@centralping/ergo'OTEL instrumentation scope name
versionstringInstrumentation scope version
tracerTracerPre-configured tracer instance (overrides serviceName/version)
attributesobjectStatic span attributes added to every pipeline span
perStagebooleanfalseEnable per-stage child spans. When true, domainAcc.trace.tracer is populated so compose creates child spans per middleware step.

Returns span metadata stored at the accumulator path (e.g., acc.trace):

{
span, // Active root span (ended by handler/auto-wrap after send)
tracer, // Resolved tracer (only when perStage: true)
activeContext, // Context with span set (only when perStage: true)
parentContext, // Extracted parent context from request headers
traceId: "abc123...",
spanId: "def456...",
}

The span is NOT ended by this middleware — it is ended by handler or ergo-router’s auto-wrap after send() completes, capturing the full request lifecycle.

None. Tracing is observability-only.

import http from "node:http";
import {
handler, compose, tracing, logger,
} from "@centralping/ergo";
const pipeline = compose(
{fn: tracing(), setPath: "trace"},
{fn: logger(), setPath: "log"},
// ... remaining middleware
);
const server = http.createServer(handler(pipeline));
server.listen(3000);

See the auto-generated tracing API docs.