Node.js OpenTelemetry Tracing Guide
Add OpenTelemetry distributed tracing to a Node.js application.
HEAL collects two independent kinds of telemetry from a Node.js application: out-of-the-box (OOB) metrics, scraped by the Component Agent from an endpoint the application exposes, and OpenTelemetry distributed traces, exported as spans to a HEAL Collector. This guide covers the tracing half — placing the heal-nodejs-tracing.js module, preloading it with NODE_OPTIONS, starting the application under a process manager, and working through the validation ladder.
Metrics are configured separately. OOB metrics use their own instrumentation module, their own HTTP endpoint and their own initialization call in the application entry file. That procedure is documented in Node.js — Prerequisites. Nothing in this guide changes it. If metrics are already working, leave that configuration exactly as it is.
How metrics and tracing coexist#
The two mechanisms are loaded at different points in the Node.js lifecycle and never interact. Metrics are pulled from the application; traces are pushed by it.
| Telemetry | What it produces | How it is loaded | Direction |
|---|---|---|---|
OOB metrics module + prom-client@14.0.1 |
Prometheus-format Node.js runtime and application metrics | Required and initialized explicitly from the application entry file | Component Agent scrapes the endpoint |
heal-nodejs-tracing.js |
OpenTelemetry spans generated from application activity by automatic instrumentation | Preloaded by the Node.js runtime before the entry file, via NODE_OPTIONS=-r ./heal-nodejs-tracing.js |
Application exports OTLP to the HEAL Collector |
Because the paths are separate, metrics can work while traces do not, and the reverse. Diagnose each one on its own evidence.
Before you start#
Collect the following:
- Shell access to the Node.js application server and write access to the application source directory.
- The name of the application entry file — for example
app.js,server.jsorindex.js. - The host and port the application listens on.
- The
heal-nodejs-tracing.jsbuild supplied by HEAL. It is delivered asheal-nodejs-tracing.js.txt. - The OTLP endpoint approved for the environment, supplied by the HEAL team.
- The service name to publish as
OTEL_SERVICE_NAME. This is the name you will search for in the trace UI, so agree it before you start. - Knowledge of how the application is actually started — direct
node, an npm script, PM2, systemd, Docker or Nomad. The environment variables must reach the real application process, not just your login shell. - A controlled restart window. Enabling the preload requires a restart of the application.
Target application layout#
Both HEAL modules sit at the application root, beside the entry file:
<application-root>/
├── package.json
├── package-lock.json
├── app.js # Existing application entry file
├── <metrics-module>.js # Existing OOB metrics module — see Node.js — Prerequisites
└── heal-nodejs-tracing.js # New OpenTelemetry tracing module
The tracing module must be at the application root because the preload path used later, ./heal-nodejs-tracing.js, is resolved relative to the process working directory.
Step 1 — Back up the current configuration#
Take a timestamped copy of the entry file and the package files before you change anything.
cd <application-root>
cp -p app.js app.js.pre-heal-otel.$(date +%Y%m%d%H%M%S)
cp -p package.json package.json.pre-heal-otel.$(date +%Y%m%d%H%M%S) 2>/dev/null || true
cp -p package-lock.json package-lock.json.pre-heal-otel.$(date +%Y%m%d%H%M%S) 2>/dev/null || true
Also record the current startup command or process-manager configuration, so the rollback at the end of this guide can restore it exactly.
Step 2 — Place and rename the tracing file#
-
Copy the build output into the application root.
cp build/heal-nodejs-tracing.js.txt <application-root>/ -
Check whether a copy is already present before overwriting it.
cd <application-root> ls -l heal-nodejs-tracing.js.txt heal-nodejs-tracing.js 2>/dev/null -
Rename the file, dropping the
.txtextension.mv heal-nodejs-tracing.js.txt heal-nodejs-tracing.js -
Verify the result.
ls -l heal-nodejs-tracing.js file heal-nodejs-tracing.js
The file must be readable by the operating-system user that runs the Node.js process. If the application runs as a service account, check ownership and permissions now rather than after the restart.
Step 3 — Do not add a require() for the tracing module#
Do not add require('./heal-nodejs-tracing') to the entry file. With the NODE_OPTIONS preload method described here, the Node.js runtime loads the tracing module before it loads the entry file. Because the runtime has already loaded it, an explicit require() in the entry file is redundant. Only add one if HEAL supplies a different integration method for your specific build.
The entry file therefore keeps its existing OOB metrics initialization and gains nothing new:
const express = require('express');
const app = express();
// Existing HEAL OOB metrics initialization — unchanged.
// See "Node.js — Prerequisites" for the exact module name and wiring.
// Existing application routes and middleware
// ...
app.listen(3000, () => {
console.log('Application started on port 3000');
});
No OpenTelemetry statement appears anywhere in the application source. Tracing is switched on entirely through environment variables.
Step 4 — Set the environment variables#
Set these in the same runtime environment that starts the application:
export OTEL_EXPORTER_OTLP_ENDPOINT="<COLLECTOR_IP>:14250"
export OTEL_SERVICE_NAME="<YOUR_SERVICE_NAME>"
export NODE_OPTIONS="-r ./heal-nodejs-tracing.js"
| Variable | Purpose | Notes |
|---|---|---|
OTEL_EXPORTER_OTLP_ENDPOINT |
Address of the HEAL Collector that receives OTLP trace data. | Use the endpoint and port approved for the environment. Do not carry example values between environments. |
OTEL_SERVICE_NAME |
Logical service name attached to every span. | This is the name you search for in the trace UI. Keep it stable — changing it splits the service history. |
NODE_OPTIONS |
Preloads the tracing module before the entry file. | The path is relative to the process working directory. If the process manager starts the application from a different directory, use an absolute path. |
OTEL_LOG_LEVEL |
Optional. Raises OpenTelemetry diagnostic logging during initial troubleshooting. | Set to debug only while diagnosing. Not every build honours it; if yours does not, use the logging mechanism documented for that build. |
Confirm the values are set:
echo "OTEL_EXPORTER_OTLP_ENDPOINT=$OTEL_EXPORTER_OTLP_ENDPOINT"
echo "OTEL_SERVICE_NAME=$OTEL_SERVICE_NAME"
echo "NODE_OPTIONS=$NODE_OPTIONS"
If you already set HEAL_TARGET_APP for OOB metrics, leave it as it is. It belongs to the metrics path and is unrelated to tracing.
Step 5 — Restart the application#
The variables must reach the actual Node.js process. Exporting them in an interactive shell has no effect on a service started by PM2, systemd or a container runtime.
Direct node start#
cd <application-root>
export OTEL_EXPORTER_OTLP_ENDPOINT="<COLLECTOR_IP>:14250"
export OTEL_SERVICE_NAME="<YOUR_SERVICE_NAME>"
export NODE_OPTIONS="-r ./heal-nodejs-tracing.js"
HEAL_TARGET_APP="<YOUR_SERVICE_NAME>" node app.js
PM2#
The variables must reach the application process itself, not just your login shell. Declare the preload with node_args and the variables in the env block of your ecosystem file:
module.exports = {
apps: [{
name: "<YOUR_SERVICE_NAME>",
script: "./app.js",
node_args: "-r './heal-nodejs-tracing.js'",
env: {
OTEL_EXPORTER_OTLP_ENDPOINT: "<COLLECTOR_IP>:14250",
OTEL_SERVICE_NAME: "<YOUR_SERVICE_NAME>"
}
}]
};
Apply the ecosystem file using your normal PM2 procedure, and confirm afterwards that the running process actually carries the variables — see Confirm the preload took effect below.
Docker and Nomad#
Bake heal-nodejs-tracing.js into the image or mount it into the application directory, then declare the variables in the container or job environment block. Because the container working directory may differ from the source layout, prefer an absolute preload path — for example NODE_OPTIONS=-r /usr/src/app/heal-nodejs-tracing.js. Remember that a rebuilt image without the file silently reverts tracing.
Other process managers#
An npm script, systemd, or any other supervisor works the same way: the three variables and the preload have to reach the actual application process. Set them wherever that supervisor defines the process environment, then use Confirm the preload took effect below to prove they arrived.
Step 6 — Verify the preload actually took effect#
This is the single most common failure point: the file is in place, the variables look right in your shell, and the running process never saw either. Confirm against the process itself before you go looking for traces.
-
Check the environment of the running process, not your shell.
ps -ef | grep '[n]ode' tr '\0' '\n' < /proc/<pid>/environ | grep -E 'NODE_OPTIONS|OTEL_'The value of
NODE_OPTIONSmust contain-r ./heal-nodejs-tracing.js. -
Confirm the tracing file is readable by that user.
ls -lh ./heal-nodejs-tracing.js test -r ./heal-nodejs-tracing.js && echo "Tracing file is readable" || echo "Tracing file is NOT readable"
If the process environment does not carry the variables, fix the process manager configuration and restart. Nothing below will produce a useful result until this check passes.
Validation ladder#
Work through these in order. Each rung assumes the one before it passed, and each one narrows where a fault can be. You can complete rungs 1 to 5 entirely on the application side, before the downstream HEAL ingestion pipeline is ready.
-
Metrics still work.
Call the OOB metrics endpoint you registered when you configured metrics — see Node.js — Prerequisites for the exact path.
curl -v http://<application-host>:<application-port>/<metrics-endpoint-path>Pass: a successful HTTP response containing Prometheus-format metrics, and no application error logged for the endpoint. This confirms the restart did not disturb the metrics path.
-
Tracing initialized cleanly.
grep -iE "opentelemetry|telemetry|trace|span|export|instrument" <application-log>The supplied bundle logs a success message when automatic instrumentation initializes, and an explicit error indicating the application is not instrumented if SDK initialization fails. Pass: no initialization failure, the application starts normally with
NODE_OPTIONSset, and existing APIs still respond. -
The collector endpoint is reachable.
nc -vz <COLLECTOR_IP> 14250If
ncis not installed:timeout 5 bash -c '</dev/tcp/<COLLECTOR_IP>/14250' \ && echo "Port reachable" \ || echo "Port NOT reachable"A successful TCP connection proves network reachability only. It does not prove that the collector accepted a span. The socket can open while the exporter is still rejected on protocol, port type or authentication. Treat this rung as ruling out firewall and routing problems, nothing more — span acceptance is confirmed at rungs 5 and 6.
-
Generate real application traffic.
Auto-instrumentation only produces spans when the application actually handles requests. An idle application generates nothing, however correct the configuration is.
curl -v http://<application-host>:<application-port>/<actual-api> for i in {1..10}; do curl -s http://<application-host>:<application-port>/<actual-api> >/dev/null doneUse a real business API, not the metrics endpoint.
-
No exporter errors after traffic.
grep -iE "opentelemetry|otlp|export|span|trace|error|failed" <application-log>At this point the strongest application-side evidence is the combination of all four: initialization succeeded, requests produced instrumentation activity, the collector endpoint is reachable, and no export errors appear. That is enough to sign off the application-side integration while the downstream pipeline is still being built.
-
End-to-end trace visible in the UI.
Once the collector, Logstash, OpenSearch and Jaeger pipeline is configured, generate an API request again and search the trace UI for the value of
OTEL_SERVICE_NAME. A complete trace shows the trace ID, span IDs, operation name, duration, HTTP URL and related tags. See Creating Jaeger and OpenSearch Dashboards and Backend Configuration for OTEL Data Ingestion.
End-to-end data flow#
Client
|
| HTTP/API request
v
Node.js application
|
+--> OOB metrics module
| |
| +--> metrics endpoint --> scraped by Component Agent
|
+--> heal-nodejs-tracing.js (preloaded by NODE_OPTIONS)
|
+--> OpenTelemetry auto-instrumentation
|
+--> spans generated from application activity
|
+--> OTLP export
|
v
HEAL Collector / trace endpoint
|
v
HEAL ingestion pipeline
|
v
OpenSearch
|
v
Jaeger / Grafana UI
Metrics leave the application by being pulled; traces leave by being pushed. A break in one branch says nothing about the other.
Troubleshooting#
| Symptom | Likely cause | Action |
|---|---|---|
| The metrics endpoint returns 404 | Metrics module not initialized, or the route was never registered. | Confirm the metrics module is present at the application root and that its initialization is still in the entry file. Follow Node.js — Prerequisites; this is not a tracing fault. |
Application fails to start after adding NODE_OPTIONS |
Tracing file missing, unreadable, wrong path, or a runtime/dependency incompatibility. | Read the startup log. Temporarily unset NODE_OPTIONS and restart to confirm the application itself is healthy, then reintroduce the preload with an absolute path. |
| OpenTelemetry initialization error in the log | Tracing bundle incompatible with the installed Node.js runtime, or a dependency issue. | Capture the exact initialization error and validate the supplied build against the target Node.js version with the HEAL team. |
| Collector port unreachable | Network, firewall or routing. | Re-run the TCP check from the application host. Raise a firewall or routing request if it fails; the endpoint must be reachable from the application host specifically, not just from a jump box. |
| Application runs, no trace appears in the UI | Downstream pipeline not yet configured, or an exporter/collector fault. | Check the application exporter log first. If it is clean, move downstream to the collector, then Logstash, OpenSearch and Jaeger. See Otel Collector Issues. |
| Metrics work but traces do not | Expected — the two telemetry paths are independent. | Validate the tracing path on its own: preload present in the process environment, initialization log clean, real API traffic generated, endpoint reachable, exporter log clean. |
| Traces appear under the wrong or a default service name | OTEL_SERVICE_NAME did not reach the process. |
Read the variable from /proc/<pid>/environ rather than the shell, and set it in the process-manager configuration. |
Rollback#
If the application misbehaves after tracing is enabled, remove the tracing variables and restore the previous startup configuration using the existing approved restart method.
unset NODE_OPTIONS
unset OTEL_EXPORTER_OTLP_ENDPOINT
unset OTEL_SERVICE_NAME
unset OTEL_LOG_LEVEL
If the variables were set in a process manager, remove them there too — unsetting them in a shell will not affect a PM2, systemd or container process.
If the tracing file itself has to be removed, move it aside rather than deleting it, so it can be restored without another build request:
mv heal-nodejs-tracing.js heal-nodejs-tracing.js.disabled.$(date +%Y%m%d%H%M%S)
Leave OOB metrics alone during a tracing rollback. Do not remove the metrics module or its initialization unless the fault has independently been traced to metrics.
Implementation checklist#
Preparation
- Application root identified.
- Application entry file identified.
- Actual startup mechanism identified (direct
node, npm script, PM2, systemd, Docker, Nomad). - Entry file and package files backed up.
- Existing OOB metrics configuration confirmed working and left unchanged.
Configuration
heal-nodejs-tracing.js.txtcopied to the application root.- File renamed to
heal-nodejs-tracing.jsand readable by the application user. - No
require()for the tracing module added to the entry file. OTEL_EXPORTER_OTLP_ENDPOINTset to the approved collector endpoint.OTEL_SERVICE_NAMEset to the agreed service name.NODE_OPTIONSset to-r ./heal-nodejs-tracing.js(absolute path where the working directory differs).
Validation
- Application restarted successfully.
NODE_OPTIONSand the OTEL variables confirmed in the running process environment.- OOB metrics endpoint still returns metrics.
- OpenTelemetry initialization messages checked, no failures.
- TCP connectivity to the collector endpoint validated.
- Real API traffic generated.
- Application and exporter logs checked for trace/export errors.
- End-to-end trace confirmed in the Jaeger/HEAL UI once the downstream pipeline is configured.
See also: Node.js — Prerequisites (OOB metrics setup) · Node.js (what HEAL monitors) · Supporting Node.js Component · NGINX OpenTelemetry Web Tracing Guide · Apache & IBM HTTP Server OpenTelemetry Web Tracing Guide