Skip to content

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.

Terminal window
npm install @ctxflow/otel@0.2.2

It 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).

Set the exporter and run your app with the register hook. This needs no code change:

Terminal window
export OTEL_SERVICE_NAME=my-agent
export OTEL_EXPORTER_OTLP_ENDPOINT=https://app.ctxflow.sh # or your collector, http://localhost:4318
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20<your key>" # not needed with a collector
node --import @ctxflow/otel/register agent.mjs

The 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 exits

conversation(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.

  • --import flushes when the event loop is empty, and on SIGTERM or SIGINT only when your app has no handler of its own for that signal. If it has one, call await sdk.shutdown() in it. process.exit() and a crash lose the last batch: call await sdk.shutdown() first.
  • Export errors (ctxflow down, wrong key) are logged, never thrown into your code.
  • If another tracer provider is already registered, init() throws ProviderConflictError: call init() first. The --import start never stops your app: on an error it logs one warning and runs without ctxflow.
  • Keep baggage in OTEL_PROPAGATORS (the default).

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 kind AGENT or CHAIN around the calls, with the answer in output.value. See Agent turns.
  • Vercel AI SDK 7: install @ai-sdk/otel and call registerTelemetry(new OpenTelemetry()) once. With AI SDK 6 and older, pass experimental_telemetry: { isEnabled: true } to each call (not tested).
  • OpenLLMetry (Traceloop) (not tested): register the instrumentation packages (for example @traceloop/instrumentation-openai) with registerInstrumentations after the SDK starts. Do not call traceloop.initialize(): it installs its own provider.
  • MCP: @arizeai/openinference-instrumentation-mcp carries the trace context and the identity in MCP calls.

Retrieval spans follow Retrieval and source identity.

Variables: Configuration. Next: Troubleshooting.