Skip to content
GitShowcase Forum

How do I add OpenTelemetry tracing to a small Node app without drowning in data?

Short answer

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.

Written by Maren Castillo · Forum lead: databases, hosting, CI and production ·
CiteGitShowcase 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.
  1. Forum 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-trace
    

    The 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.mjs next 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 --import flag:

    node --import ./instrumentation.mjs app.js
    

    Two checks from the docs: TypeScript with --import instrumentation.ts needs Node.js v20 or later, and you should make sure you have no conflicting --import or --require flags in NODE_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.DEBUG when 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.

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

GitShowcase Forum. Questions and answers on shipping developer projects: dev tools, APIs, infrastructure and portfolios.