|
| 1 | +--- |
| 2 | +date: 2025-10-12 |
| 3 | +id: nodejs-selective-instrumentation |
| 4 | +title: Enable/Disable OpenTelemetry Instrumentation in Node.js |
| 5 | +tags: [SigNoz Cloud, Self-Host] |
| 6 | +--- |
| 7 | + |
| 8 | +import GetHelp from '@/components/shared/get-help.md' |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +The OpenTelemetry auto-instrumentation for Node.js automatically includes a lot of libraries and frameworks, which can result in a lot of noise and irrelevant telemetry data. |
| 13 | + |
| 14 | +This guide helps you to use **environment variables** and **programmatic configuration** to control which libraries are enabled/disabled for auto-instrumentation. By limiting instrumented libraries, we can reduce noise and only have relevant telemetry data. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +Before you begin, ensure the following: |
| 19 | + |
| 20 | +- You have a Node.js (version 18.0 or higher) application already instrumented with OpenTelemetry. |
| 21 | +- The OpenTelemetry SDK is installed and configured. |
| 22 | + |
| 23 | +## Exclude Specific Instrumentation Libraries |
| 24 | + |
| 25 | +### Use Case |
| 26 | + |
| 27 | +By default all [supported libraries](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/packages/auto-instrumentations-node/README.md#supported-instrumentations) are automatically instrumented. However, in some cases you might only want telemetry for specific frameworks: |
| 28 | + |
| 29 | +- Reducing telemetry noise from irrelevant libraries |
| 30 | +- Debugging specific parts of your application |
| 31 | + |
| 32 | +### Using Environment Variables |
| 33 | + |
| 34 | +You can control which instrumentation libraries are enabled or disabled by setting environment variables. |
| 35 | + |
| 36 | +This is the simplest way to customize OpenTelemetry instrumentation without making any code changes. |
| 37 | + |
| 38 | +#### Enable or Disable Specific Libraries |
| 39 | + |
| 40 | +Use the following environment variables: |
| 41 | + |
| 42 | +- `OTEL_NODE_ENABLED_INSTRUMENTATIONS`: to specify which libraries to enable. |
| 43 | +- `OTEL_NODE_DISABLED_INSTRUMENTATIONS`: to specify which libraries to disable. |
| 44 | + |
| 45 | +Both variables accept a comma-separated list of library names without the `@opentelemetry/instrumentation-` prefix. |
| 46 | + |
| 47 | +For example: |
| 48 | + |
| 49 | +```bash |
| 50 | +# Enable only HTTP and Express instrumentations |
| 51 | +export OTEL_NODE_ENABLED_INSTRUMENTATIONS="http,express" |
| 52 | + |
| 53 | +# Disable file system (fs) and gRPC instrumentations |
| 54 | +export OTEL_NODE_DISABLED_INSTRUMENTATIONS="fs,grpc" |
| 55 | +``` |
| 56 | + |
| 57 | +Explore full list of supported libraries [here](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/packages/auto-instrumentations-node/README.md#supported-instrumentations). |
| 58 | + |
| 59 | +<Admonition> |
| 60 | + If both variables are set, `OTEL_NODE_ENABLED_INSTRUMENTATIONS` is applied first, and |
| 61 | + `OTEL_NODE_DISABLED_INSTRUMENTATIONS` is applied afterwards. So, if a library appears in both, it |
| 62 | + will be disabled. |
| 63 | +</Admonition> |
| 64 | + |
| 65 | +Then run your application with: |
| 66 | + |
| 67 | +```bash {5-6} |
| 68 | +export OTEL_TRACES_EXPORTER="otlp" |
| 69 | +export OTEL_NODE_RESOURCE_DETECTORS="env,host,os" |
| 70 | +export OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.<region>.signoz.cloud:443" |
| 71 | +export OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=<your-ingestion-key>" |
| 72 | +export OTEL_NODE_ENABLED_INSTRUMENTATIONS="http,express" |
| 73 | +export OTEL_NODE_DISABLED_INSTRUMENTATIONS="fs,grpc" |
| 74 | +export OTEL_SERVICE_NAME="<service_name>" |
| 75 | +export NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register" |
| 76 | +node app.js |
| 77 | +``` |
| 78 | + |
| 79 | +For Self-Hosted SigNoz, set `OTEL_EXPORTER_OTLP_ENDPOINT` to your OTLP endpoint and omit `OTEL_EXPORTER_OTLP_HEADERS`. |
| 80 | + |
| 81 | +Example: |
| 82 | + |
| 83 | +```bash |
| 84 | +export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318" |
| 85 | +``` |
| 86 | + |
| 87 | +<Admonition> |
| 88 | +These environment variables work **only with auto-instrumentation setups**, where instrumentation is enabled using `NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"`. You can follow this [guide](https://signoz.io/docs/instrumentation/opentelemetry-javascript/#send-traces-directly-to-signoz-cloud---no-code-automatic-instrumentation-recommended) for step-by-step guide on no-code automatic instrumentation for a JavaScript application with OpenTelemetry. |
| 89 | +</Admonition> |
| 90 | + |
| 91 | +If you're using code based auto instrumentation with the OpenTelemetry SDK, use the programmatic configuration method described below instead. |
| 92 | + |
| 93 | +### Using Programmatic Configuration |
| 94 | + |
| 95 | +You can also enable/disable libraries programmatically in your application code, modify your existing `instrumentation.js` or `instrumentation.ts` where you have initialized OpenTelemetry SDK. |
| 96 | + |
| 97 | +For more details, follow [this](https://signoz.io/docs/instrumentation/opentelemetry-javascript/) guide for code level automatic instrumentation. |
| 98 | + |
| 99 | +#### What to change |
| 100 | + |
| 101 | +In your existing file: |
| 102 | + |
| 103 | +1. Import `getNodeAutoInstrumentations` from `@opentelemetry/auto-instrumentations-node`. |
| 104 | +2. Use it to specify which instrumentations to enable/disable. |
| 105 | +3. Keep the rest of your SDK and exporter configuration unchanged. |
| 106 | + |
| 107 | +#### Before |
| 108 | + |
| 109 | +```javascript:instrumentation.js |
| 110 | +// instrumentation.js (existing setup) |
| 111 | +const { NodeSDK } = require('@opentelemetry/sdk-node') |
| 112 | +const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http') |
| 113 | + |
| 114 | +const traceExporter = new OTLPTraceExporter({ |
| 115 | + url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://localhost:4318/v1/traces', |
| 116 | +}) |
| 117 | + |
| 118 | +const sdk = new NodeSDK({ |
| 119 | + traceExporter, |
| 120 | +}) |
| 121 | + |
| 122 | +sdk.start() |
| 123 | +``` |
| 124 | + |
| 125 | +#### After |
| 126 | + |
| 127 | +```javascript:instrumentation.js {6-12} |
| 128 | +// instrumentation.js (modified) |
| 129 | +const { NodeSDK } = require('@opentelemetry/sdk-node'); |
| 130 | +const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http'); |
| 131 | +const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node'); |
| 132 | + |
| 133 | +const instrumentations = getNodeAutoInstrumentations({ |
| 134 | + '@opentelemetry/instrumentation-fs': { enabled: false }, |
| 135 | + '@opentelemetry/instrumentation-http': { enabled: true }, |
| 136 | + '@opentelemetry/instrumentation-express': { enabled: true }, |
| 137 | + '@opentelemetry/instrumentation-dns': { enabled: false }, |
| 138 | + '@opentelemetry/instrumentation-grpc': { enabled: false }, |
| 139 | +}); |
| 140 | + |
| 141 | +const traceExporter = new OTLPTraceExporter({ |
| 142 | + url: 'https://ingest.<region>.signoz.cloud:443/v1/traces', |
| 143 | + headers: { |
| 144 | + 'signoz-ingestion-key': '<your-ingestion-key>' |
| 145 | + } |
| 146 | +}); |
| 147 | + |
| 148 | +const sdk = new NodeSDK({ |
| 149 | + traceExporter, |
| 150 | + instrumentations: [instrumentations], |
| 151 | +}); |
| 152 | + |
| 153 | +sdk.start(); |
| 154 | +``` |
| 155 | + |
| 156 | +Then require this file before any application code: |
| 157 | + |
| 158 | +```javascript:app.js |
| 159 | +require('./instrumentation.js'); |
| 160 | +const express = require('express'); |
| 161 | +const app = express(); |
| 162 | + |
| 163 | +// ... rest of your application |
| 164 | +``` |
| 165 | + |
| 166 | +Or run your application with: |
| 167 | + |
| 168 | +```bash |
| 169 | +export OTEL_TRACES_EXPORTER="otlp" |
| 170 | +export OTEL_NODE_RESOURCE_DETECTORS="env,host,os" |
| 171 | +export OTEL_EXPORTER_OTLP_ENDPOINT="https://ingest.<region>.signoz.cloud:443" |
| 172 | +export OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=<your-ingestion-key>" |
| 173 | +export OTEL_SERVICE_NAME="<service_name>" |
| 174 | +node -r ./instrumentation.js app.js |
| 175 | +``` |
| 176 | + |
| 177 | +For Self-Hosted SigNoz, set `OTEL_EXPORTER_OTLP_ENDPOINT` to your OTLP endpoint and omit `OTEL_EXPORTER_OTLP_HEADERS`. |
| 178 | + |
| 179 | +Example: |
| 180 | + |
| 181 | +```bash |
| 182 | +export OTEL_EXPORTER_OTLP_ENDPOINT="http://localhost:4318" |
| 183 | +``` |
| 184 | + |
| 185 | +## Troubleshooting |
| 186 | + |
| 187 | +If your application is not sending traces as expected or if you want to see verbose OpenTelemetry logs, you can enable verbose logs. |
| 188 | + |
| 189 | +Set the following environment variable before running the application: |
| 190 | + |
| 191 | +```bash |
| 192 | +export OTEL_LOG_LEVEL=debug |
| 193 | +``` |
| 194 | + |
| 195 | +This enables verbose logging and prints detailed information about the OpenTelemetry SDK configuration, exporters, and any errors encountered during exporting of traces. |
| 196 | + |
| 197 | +<Admonition> |
| 198 | + Debug mode is useful for verifying configuration and diagnosing missing spans. It's not |
| 199 | + recommended to use in production. |
| 200 | +</Admonition> |
| 201 | + |
| 202 | +### Common Issues |
| 203 | + |
| 204 | +1. No traces appearing in SigNoz |
| 205 | + |
| 206 | +Ensure `OTEL_EXPORTER_OTLP_ENDPOINT` and `OTEL_EXPORTER_OTLP_HEADERS` variables are set properly. |
| 207 | + |
| 208 | +2. Missing or incomplete spans |
| 209 | + |
| 210 | +- Verify that the relevant instrumentation libraries are set via the environment variable `OTEL_NODE_ENABLED_INSTRUMENTATIONS`. |
| 211 | +- Make sure `NODE_OPTIONS="--require @opentelemetry/auto-instrumentations-node/register"` is set before your application starts. |
| 212 | + |
| 213 | +3. Invalid or missing ingestion key |
| 214 | + |
| 215 | +- Double-check your ingestion key in `OTEL_EXPORTER_OTLP_HEADERS`. |
| 216 | +- The format should be: |
| 217 | + |
| 218 | +```bash |
| 219 | +export OTEL_EXPORTER_OTLP_HEADERS="signoz-ingestion-key=<your-ingestion-key>" |
| 220 | +``` |
| 221 | + |
| 222 | +## What's Next |
| 223 | + |
| 224 | +Now that you have instrumented Node.js selectively, explore related topics to enhance Node.js setup. |
| 225 | + |
| 226 | +- [Node.js - Manual Instrumentation](https://signoz.io/docs/instrumentation/manual-instrumentation/javascript/opentelemetry-nodejs/) |
| 227 | +- [Instrument JavaScript](https://signoz.io/docs/instrumentation/opentelemetry-javascript/) |
| 228 | + |
| 229 | +## Get Help |
| 230 | + |
| 231 | +<GetHelp /> |
0 commit comments