JavaScript and TypeScript
@ctxflow/otel does for Node.js what ctxflow-otel does for Python: it reads the CTXFLOW_* variables, puts the conversation and the user on every span, and exports the spans to ctxflow over OTLP/HTTP. Node.js 22.15 or later.
Install
Section titled “Install”npm install @ctxflow/otel@0.2.2It installs @ctxflow/semconv (the conventions) too. Both packages are published from GitHub Actions with provenance: npm audit signatures verifies them.
@opentelemetry/api (version 1.10, >=1.10.0 <1.11.0) is a peer dependency. npm installs it for you. With pnpm or yarn, add it yourself: pnpm add @opentelemetry/api@1.10 (or yarn add).
Start it
Section titled “Start it”Set the exporter and run your app with the register hook. This needs no code change:
export OTEL_SERVICE_NAME=my-agentexport OTEL_EXPORTER_OTLP_ENDPOINT=https://app.ctxflow.sh # or your collector, http://localhost:4318export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20<your key>" # not needed with a collectornode --import @ctxflow/otel/register agent.mjsThe SDK adds /v1/traces to the endpoint. Run ctxflow-otel check with the same variables to test the key (it is a Python tool: the curl check on that page works without Python).
In code instead of --import:
import { conversation, init } from "@ctxflow/otel";
const sdk = init(); // idempotent; throws on an invalid CTXFLOW_* value
await conversation("conv-1", { userId: "u1", turn: 0 }, async () => { // every span started in here, also after an await, carries the identity});
await sdk.shutdown(); // flush the batch before the process exitsconversation(id, options?, fn) returns what fn returns. Values must be non-empty, without surrounding white space, at most 256 characters. turn is an integer from 0 up. A bad value throws a TypeError that names the argument.
--importflushes when the event loop is empty, and onSIGTERMorSIGINTonly when your app has no handler of its own for that signal. If it has one, callawait sdk.shutdown()in it.process.exit()and a crash lose the last batch: callawait sdk.shutdown()first.- Export errors (ctxflow down, wrong key) are logged, never thrown into your code.
- If another tracer provider is already registered,
init()throwsProviderConflictError: callinit()first. The--importstart never stops your app: on an error it logs one warning and runs without ctxflow. - Keep
baggageinOTEL_PROPAGATORS(the default).
Wire your LLM library
Section titled “Wire your LLM library”The SDK does not wrap LLM libraries. The instrumentation of your library sends its spans through it, so start the SDK first:
- OpenInference (for example
@arizeai/openinference-instrumentation-openai). In ES modules patch the class by hand:new OpenAIInstrumentation().manuallyInstrument(OpenAI). A turn is a span of kindAGENTorCHAINaround the calls, with the answer inoutput.value. See Agent turns. - Vercel AI SDK 7: install
@ai-sdk/oteland callregisterTelemetry(new OpenTelemetry())once. With AI SDK 6 and older, passexperimental_telemetry: { isEnabled: true }to each call (not tested). - OpenLLMetry (Traceloop) (not tested): register the instrumentation packages (for example
@traceloop/instrumentation-openai) withregisterInstrumentationsafter the SDK starts. Do not calltraceloop.initialize(): it installs its own provider. - MCP:
@arizeai/openinference-instrumentation-mcpcarries the trace context and the identity in MCP calls.
Retrieval spans follow Retrieval and source identity.
Variables: Configuration. Next: Troubleshooting.