✨ Overview
@bymax-one/nest-telemetry starts the OpenTelemetry SDK before your application loads, redacts secrets from spans by default (URL query strings, Redis arguments, the process command line), keeps probe paths out of your traces and flushes on shutdown. It ships as a single npm package with subpath exports: an SDK part that works from plain Node, a node --import preload, and a NestJS module.
Every @opentelemetry/* package is an optional peer dependency, and a boot with telemetry disabled loads none of them. The library is the mechanism; the host application keeps the policy: whether tracing is on, which paths are probes, what the service is called.
Support matrix
| Part |
Supported |
| Node.js |
24 or newer |
NestJS (./nest only) |
11 |
| Applications |
CommonJS and native ES modules |
| OpenTelemetry generation |
The one in OpenTelemetry Versions |
Native ES modules. Auto-instrumentation patches CommonJS modules as they load, and native ES modules only through a loader hook. The preload registers that hook, and it needs the optional peer @opentelemetry/instrumentation. Without that package, a native-ESM application gets no auto-instrumentation at all; CommonJS code is unaffected, and the start warns (BYMAX_TELEMETRY_ESM_HOOK_UNAVAILABLE) so the gap is not silent. Measured on real consumers: an ESM application with the SDK started and no hook exported no HTTP span; with the hook it did.
Why nest-telemetry?
- 💤 Zero cost when disabled — A boot with
OTEL_SDK_DISABLED=true registers no loader hook and loads no OpenTelemetry package, @opentelemetry/api included; this is checked from the packed tarball in ESM and CommonJS, with a positive control that proves the check can fail.
- 🔒 Secret-safe by default — Span data is a leak surface, so URL query strings, Redis arguments and the process command line are redacted unless an explicit option, which also warns, turns one off. No environment variable turns a redaction off.
- 🚦 Preload ordering done right — The SDK has to start before the libraries it instruments load, and
main.ts is too late; a node --import preload runs first and takes its settings from environment variables.
- 🔌 Your policy, your rules — Enabling, probe paths, service name and version, extra redacted parameters and the structured logger are passed in.
Install it with the OpenTelemetry peers you use: the command is in Quick Start.
🔥 Features
🚦 Boot and lifecycle
- ✅
node --import preload — Registers the ES-module loader hook, starts the SDK from the environment variables and flushes on a natural exit
- ✅ One SDK per process — The handle lives under a
Symbol.for key on globalThis, so an ES-module preload and a CommonJS application share it, and a second start returns the one already running
- ✅ Graceful Nest shutdown —
TelemetryModule awaits the flush as part of the application's own shutdown, under the stable failure key TRACING_FLUSH_FAILED
- ✅ Never takes the host down — Each failure the preload can meet is a warning and the application still starts; a flush the collector refuses is reported by the Nest module, never thrown into your shutdown path
🔐 Redaction
- ✅ Span-end query redaction —
url.query is replaced and the query is cut from url.full, http.url and http.target, on spans from any instrumentation
- ✅ Outgoing-URL credential list — A list of credential-bearing parameters, checked in a test against the SDK's own default list; a host can add names and never drop them
- ✅ Parse errors do not quote their input — the part of a request body that a
JSON.parse message repeats is removed from the span status and exception events; redactExceptionText removes every exception message
- ✅ Redis commands reduced to their name — for
ioredis and node-redis (redis), arguments and keys never leave the process, EVAL and EVALSHA payloads included
- ✅ Command line kept out of the resource —
process.command_args, process.command_line and process.command are dropped
- ✅ Explicit, loud opt-outs — Each off switch is a literal
unsafe.*: true and emits a SecurityWarning
📦 Subpath Exports
One package, separate entry points. Import only what your app needs:
| Subpath |
Import |
Purpose |
Dependencies |
| SDK |
@bymax-one/nest-telemetry |
startTelemetry, shutdownTelemetry, ignorePaths, DEFAULT_REDACTED_QUERY_PARAMS, types |
OpenTelemetry peers |
| Register |
@bymax-one/nest-telemetry/register |
The preload for node --import; importing it runs it, and it exports nothing |
OpenTelemetry peers |
| Nest |
@bymax-one/nest-telemetry/nest |
TelemetryModule, TelemetryShutdownHook, TELEMETRY_LOGGER, TELEMETRY_FLUSH_FAILED_KEY, TELEMETRY_SHUTDOWN_TIMEOUT_KEY, TELEMETRY_EXPORT_FAILED_KEY, ITelemetryErrorLogger (type) |
NestJS 11 |
| Observability |
@bymax-one/nest-telemetry/observability |
TelemetryMetrics, SPAN_EXPORT_FAILURES_METRIC |
NestJS 11, @bymax-one/nest-core, prom-client |
The SDK entry and ./register import no @nestjs/* package; only ./nest and ./observability do. ./observability is optional: it needs @bymax-one/nest-core and prom-client, which are optional peers, and the other entries never load them. Requiring ./register from CommonJS throws and names node --import.
🚀 Quick Start
NestJS
Install the package and the OpenTelemetry peers you use:
pnpm add @bymax-one/nest-telemetry \
@opentelemetry/api @opentelemetry/sdk-node @opentelemetry/sdk-trace @opentelemetry/resources \
@opentelemetry/exporter-trace-otlp-http @opentelemetry/auto-instrumentations-node \
@opentelemetry/instrumentation
Name your service and point the exporter at a collector, using standard variables:
export OTEL_SERVICE_NAME=billing
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
Start the application with the preload:
node --import @bymax-one/nest-telemetry/register dist/main.js
Import the module, so Nest flushes the SDK when the application closes, and enable Nest's shutdown hooks so a signal runs them:
import { Module } from '@nestjs/common'
import { TelemetryModule } from '@bymax-one/nest-telemetry/nest'
@Module({ imports: [TelemetryModule] })
export class AppModule {}
TelemetryModule is global, and Nest closes global modules last, in the reverse of the order it registers them. List it first in the application module's imports, before any module that is global or that imports a global module, so it closes after them: a span that another global module, such as a queue drain or a database module, ends in its own shutdown hook is exported only if TelemetryModule was registered first. Measured with Nest 11: listed first, the span arrives whether the other global module is imported directly or through a feature module; listed after a feature module that imports a global module, or after the global module itself, the span is dropped.
Things to know before you rely on it:
- Traces only, by default. Metrics and logs stay off unless
OTEL_METRICS_EXPORTER or OTEL_LOGS_EXPORTER names an exporter. This departs from the OpenTelemetry default, which exports both through OTLP, and it does so on purpose: otherwise a host that already ships metrics and logs another way would send them twice.
- Name your service. With neither
OTEL_SERVICE_NAME nor a serviceName option, the start warns once (BYMAX_TELEMETRY_SERVICE_NAME_MISSING) and spans are exported as unknown_service:node, which groups unrelated services under one name. A service.name set only in OTEL_RESOURCE_ATTRIBUTES is honoured and the spans carry it while the env or service resource detector is enabled, as every detector is by default, but the warning still fires, because the library looks only at OTEL_SERVICE_NAME and the option; set OTEL_SERVICE_NAME to silence it. The start is never refused.
- Signals. The preload flushes when the process runs out of work. It installs no signal handler, because a handler in the preload would race the application's own shutdown. In Nest, call
app.enableShutdownHooks() so SIGTERM closes the application, and the module flushes the SDK. In plain Node, call shutdownTelemetry() yourself.
Plain Node
The SDK part never imports @nestjs/*. Start it from your own preload, or from the first lines of your entry point if nothing instrumented has been imported yet:
import { ignorePaths, startTelemetry } from '@bymax-one/nest-telemetry'
const telemetry = await startTelemetry({
serviceName: 'billing',
serviceVersion: '1.4.2',
ignoreIncomingRequest: ignorePaths([/^\/health\/(live|ready)\/?$/, /^\/metrics\/?$/])
})
// ... run your application ...
// In your own graceful-shutdown path, after your resources are closed:
await telemetry.shutdown()
This package installs no signal handler and shows none, because handling signals belongs to the application: a SIGTERM listener replaces Node's default termination, so it has to end the process itself once its work is done. startTelemetry resolves to a handle with enabled and shutdown(). Starting twice returns the handle already running (unless enabled: false, which returns a disabled handle), including across two copies of the module (an ES-module preload and a CommonJS application share the handle through globalThis). A later call that passes options is told they were ignored, and enabled: false against a running SDK is told it does not stop it: shutdownTelemetry() does.
⚙️ Configuration
startTelemetry(options)
Every field is the host's policy, and none is required.