Installation

Install and initialize the Sophonz Node.js SDK — programmatically with init() or via the opentelemetry-instrument preload binary.

Install @sophonz/node-sdk and initialize it before the rest of your application loads. You can either call init() at the top of your entry file or use the preload binary so instrumentation is applied before any module is imported.

Initializing the Node.js SDK and wiring middleware

Quick start

Enter the collector URL and app name below to generate the initialization code from those values. Advanced options are behind the "Advanced options" toggle.

// instrumentation.ts — load before your app: node --import ./instrumentation.ts
import { init } from '@sophonz/node-sdk';

// These can also come from the environment:
//   OTEL_EXPORTER_OTLP_ENDPOINT  collector base URL (/v1/traces etc. appended)
//   OTEL_SERVICE_NAME            service.name resource attribute
//   SOPHONZ_API_KEY              sent as the OTLP Authorization header
//   OTEL_PROPAGATORS             defaults to tracecontext,baggage
process.env.OTEL_EXPORTER_OTLP_ENDPOINT ??= 'https://in.sophonz.ai';
process.env.OTEL_PROPAGATORS ??= 'tracecontext,baggage';

await init({
  service: process.env.OTEL_SERVICE_NAME ?? 'next-sample',
  apiKey: process.env.SOPHONZ_API_KEY,
});

Install

bun add @sophonz/node-sdk
# or
pnpm add @sophonz/node-sdk
# or
npm install @sophonz/node-sdk

TIP — Get your API key

Log in to the Sophonz console, register an app, and copy the issued key. Provide it as the apiKey option or the SOPHONZ_API_KEY environment variable. Without an API key (and no OTEL_EXPORTER_OTLP_HEADERS), the SDK logs a warning and skips initialization.

Programmatic initialization

Call init() as early as possible — ideally as the first import in your entry file — so it runs before frameworks and database clients load.

// instrument.ts (imported first)
import { init } from '@sophonz/node-sdk';
 
init({
  service: 'my-app',
  apiKey: process.env.SOPHONZ_API_KEY,
});
// index.ts
import './instrument';
import express from 'express';
 
const app = express();
// ...

init() enables Sophonz's recommended defaults on top of the base SDK: console capture, experimental exception capture, Sentry integration, and programmatic re-patching of instrumentations. To take explicit control of every option, use initSDK() instead — see Configuration.

Graceful shutdown

By default the SDK flushes telemetry on SIGTERM / SIGINT. If you disable that with stopOnTerminationSignals: false, flush manually before exit:

import { shutdown } from '@sophonz/node-sdk';
 
process.on('SIGTERM', async () => {
  await shutdown();
  process.exit(0);
});

Preload binary (zero-code)

To instrument without touching application code, preload the SDK with Node's -r flag:

node -r @sophonz/node-sdk/build/bin/opentelemetry-instrument.js app.js

Or use the bundled CLI:

opentelemetry-instrument app.js

With this approach, configuration comes from environment variables (SOPHONZ_API_KEY, OTEL_SERVICE_NAME, and the rest documented in Configuration).

Next steps

  • Configuration — every SDKConfig option and environment variable.
  • Middleware — enrich request spans in Express and Koa.
  • API Reference — exported functions and the bundled official OpenTelemetry API.
  • Examples — complete Express and Koa server examples.

TIP — This is just the beginning

Now ship your app. Sophonz surfaces the problems real users hit — so your app evolves on its own.