How do I add OpenTelemetry tracing to a small Node app without drowning in data?
Install the Node SDK with auto-instrumentation, load it before your app code with the --import flag, and print spans to the console first. Then switch to a real exporter. For a small app, start with no sampling at all; add a ratio sampler only when volume or cost actually becomes a problem.
Cite
GitShowcase Forum, "How do I add OpenTelemetry tracing to a small Node app without drowning in data?", https://gitshowcase.com/forum/discuss/how-do-i-add-opentelemetry-tracing-to-a-small-node-app-without/, accessed 2026-11-24.- Maren CastilloStaffForum lead: databases, hosting, CI and production ·
Step 1: add the SDK and auto-instrumentation
The official Node.js getting started guide uses an Express app, but the same setup works with other frameworks. Install the packages it lists:
npm install @opentelemetry/sdk-node \ @opentelemetry/api \ @opentelemetry/auto-instrumentations-node \ @opentelemetry/sdk-metrics \ @opentelemetry/sdk-traceThe Node SDK sets sensible defaults, and the auto-instrumentations package creates spans for supported libraries without you editing your handlers. In the guide's example it creates a span for each incoming Express request.
Step 2: create the instrumentation file
Create
instrumentation.mjsnext to your app. Use the console exporter while you check that it works:/* instrumentation.mjs */ import { NodeSDK } from '@opentelemetry/sdk-node'; import { ConsoleSpanExporter } from '@opentelemetry/sdk-trace'; import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'; const sdk = new NodeSDK({ traceExporter: new ConsoleSpanExporter(), instrumentations: [getNodeAutoInstrumentations()], }); sdk.start();This is the guide's setup with the metrics part trimmed. Add the metrics reader from the guide if you want metrics too.
Step 3: load it before your app
Instrumentation must run before the code it instruments. The guide uses the Node
--importflag:node --import ./instrumentation.mjs app.jsTwo checks from the docs: TypeScript with
--import instrumentation.tsneeds Node.js v20 or later, and you should make sure you have no conflicting--importor--requireflags inNODE_OPTIONS, such as--require @opentelemetry/auto-instrumentations-node/register. If your app is ESM, the guide notes a loader hook is required, so read its ESM note and the linked ESM support document.Hit an endpoint a few times and you should see spans printed in the console. If nothing appears, turn on diagnostic logging from the guide's troubleshooting section:
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api'; diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);Switch to
DiagLogLevel.DEBUGwhen you need more detail. Once spans appear, replace the console exporter with an exporter for your backend. The exporters section of the OpenTelemetry JavaScript docs covers the options.Step 4: decide whether you need sampling at all
This is the point most guides skip. The OpenTelemetry sampling concepts page lists when not to sample: if you generate very little data (it says tens of small traces per second or lower), sampling may cost you more in complexity than it saves. It also warns that sampling has indirect costs, such as engineering time to maintain rules and the risk of dropping the one trace you needed. The default in the JavaScript SDK is to sample everything, and the JavaScript sampling page says that if you do not need to manage data volume, do not bother setting a sampler.
Consider sampling when you meet the criteria on that page, such as generating 1000 or more traces per second, or when most of your traces are healthy, similar requests.
Step 5: add a head sampler if volume is a problem
Head sampling decides at the start of a trace, based on the trace ID. The concepts page says the common form, consistent probability sampling, keeps whole traces with no missing spans at a steady rate. It is easy to configure and efficient. Its limit: it cannot choose traces based on what happened later, so you cannot guarantee that every trace containing an error is kept.
The simplest setup is environment variables, from the JavaScript docs:
export OTEL_TRACES_SAMPLER="traceidratio" export OTEL_TRACES_SAMPLER_ARG="0.1"That keeps 10 percent of traces. Or in code:
import { TraceIdRatioBasedSampler } from '@opentelemetry/sdk-trace-node'; const sdk = new NodeSDK({ sampler: new TraceIdRatioBasedSampler(0.1), // traceExporter, instrumentations ... });Step 6: use tail sampling only when you need error-first keeping
Tail sampling decides after the trace finishes, so you can always keep traces with errors or high latency. The concepts page lists the costs: it is harder to implement and operate, because the component doing it must be stateful and hold a lot of data, and the best options are often vendor-specific. The OpenTelemetry Collector includes a tail sampling processor. For a small app, I would hold off until a real need appears.
Step 7: cut noise before you cut traces
If the data feels overwhelming but the volume is low, you may have noisy spans, not too many traces. Disable instrumentations you do not use, for example file system calls:
instrumentations: [ getNodeAutoInstrumentations({ '@opentelemetry/instrumentation-fs': { enabled: false }, }), ],Check the OpenTelemetry registry for the exact names for your version. Then add a few manual spans around your own business logic (see the guide's manual instrumentation link) so the useful spans stand out. Our observability guide for small teams compares backends if you are still choosing one.
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.