DEV Community

Cover image for Turn off Sentry tracing in Node and keep only the errors
Amorizz
Amorizz

Posted on

Turn off Sentry tracing in Node and keep only the errors

I wanted one small Node service to send errors to Sentry and nothing else. No traces, no spans. So I wrote tracesSampleRate: 0, a line I've put in more than one setup guide, and moved on. Then I pointed the SDK at a stub ingest and logged what came out. Zero is not off.

TL;DR

  • With tracesSampleRate: 0, @sentry/node 11.14.0 still created 103 spans for 52 requests, sent client_report envelopes saying it threw them away, and told every downstream service sampled=false.
  • A downstream service with tracesSampleRate: 1 sent 0 spans when the caller had tracesSampleRate: 0. With the key removed from the caller, it sent 102.
  • Errors-only means deleting tracesSampleRate and tracesSampler, and checking that SENTRY_TRACES_SAMPLE_RATE isn't set in your environment.

The lab

Sentry's own docs say it plainly on the Node tracing page: "Setting it to 0 will not disable tracing, it will simply never send any traces to Sentry." I wanted to see what "not disabled" costs on the wire, so I built the smallest thing that shows it.

Versions from run-0-versions.txt (Node, npm, then the two SDK packages):

v24.22.1
11.21.1
@sentry/node 11.14.0
@sentry/core 11.14.0
Enter fullscreen mode Exit fullscreen mode

Three processes on localhost:

  • stub.js on :9999 plays Sentry ingest and logs every envelope item type, plus any client_report. On :9998 it plays a downstream API and logs the sentry-trace and baggage headers it receives.
  • app.js on :9997 is the service under test. Every request does one fetch to the downstream API, and /boom throws and calls captureException.
  • load.js is a separate process that sends 50 GET /ok, one GET /boom, then reads span counters from /stats. That's 52 incoming requests per run.

The only thing that changes between runs is the tracing config:

  const opts = { dsn: 'http://[email protected]:9999/1', clientReportFlushInterval: 1000 };
  if (MODE === 'zero') opts.tracesSampleRate = 0;
  if (MODE === 'rate01') opts.tracesSampleRate = 0.1;
  if (MODE === 'rate1') opts.tracesSampleRate = 1;
  if (MODE === 'sampler0') opts.tracesSampler = () => 0;
  if (MODE === 'unset_noprop') opts.tracePropagationTargets = [];
Enter fullscreen mode Exit fullscreen mode

I set clientReportFlushInterval: 1000 so client reports land inside a short run. The default is 60 seconds, so in production you'd see one report a minute instead of two per run.

Span counts come from the client hooks:

  c.on('spanStart', s => { started++; if (s.isRecording()) recording++; });
Enter fullscreen mode Exit fullscreen mode

What each config put on the wire

Config Spans started Recording Items at ingest Downstream sentry-trace flag
key not set 0 0 1 event none (51 calls)
tracesSampleRate: 0 103 0 1 event, 2 client_report 0 on 51 of 51
tracesSampler: () => 0 103 0 1 event, 2 client_report 0 on 51 of 51
tracesSampleRate: 0.1 103 6 1 event, 3 span, 2 client_report 0 on 48, 1 on 3
tracesSampleRate: 1 103 103 1 event, 52 span 1 on 51 of 51

103 is two spans per request (the incoming HTTP server span and the outgoing fetch) for 51 requests, plus the server span for /stats. With the key unset, the hook never fired once.

Here is the zero run, run-2-zero.txt:

{"MODE":"zero","spans_started":103,"spans_recording":0,"spans_ended":51,"rss_mb":118,"N":50,"ms_for_N":190}
## stub saw:
envelope items: {"client_report":2,"event":1}
client_report discards: [{"reason":"sample_rate","category":"span","quantity":22},{"reason":"sample_rate","category":"span","quantity":81}]
Enter fullscreen mode Exit fullscreen mode

22 + 81 = 103. The SDK built every span, decided not to keep it, and then sent a report about it. Nothing is stored as a trace, but the instrumentation runs on every request and your ingest receives extra envelopes it has to answer.

And the run with the key removed, run-1-unset.txt:

{"MODE":"unset","spans_started":0,"spans_recording":0,"spans_ended":0,"rss_mb":114,"N":50,"ms_for_N":161}
## stub saw:
envelope items: {"event":1}
client_report discards: []
Enter fullscreen mode Exit fullscreen mode

One error event, nothing else. That's the errors-only setup I actually wanted.

I also logged wall time for the 50 requests (ms_for_N). It moved between 161 and 193 ms across runs, including the run with no Sentry at all (171 ms). On a loopback test that small, the difference is noise, so I'm not claiming a latency cost from this lab.

The downstream service loses its traces

The sampled=false flag is what changed my mind about zero being harmless. Here is the header the downstream API received from the zero run:

sample baggage: sentry-environment=production,sentry-public_key=devkey,sentry-trace_id=e753752272ee408280dde03c408dbfcb,sentry-sampled=false,sentry-sample_rand=0.9553548709390874,sentry-sample_rate=0
Enter fullscreen mode Exit fullscreen mode

A service that receives sampled=false inherits that decision. To check it, I added service-b.js, a second Sentry-instrumented service with tracesSampleRate: 1 and its own project, and pointed the app at it. Same 50 + 1 requests, three configs on the caller:

## A MODE=unset -> B tracesSampleRate: 1
service B (tracesSampleRate: 1) sent spans: 102; client_report discards: []
## A MODE=zero -> B tracesSampleRate: 1
service B (tracesSampleRate: 1) sent spans: 0; client_report discards: [{"reason":"sample_rate","category":"span","quantity":102}]
## A MODE=unset_noprop -> B tracesSampleRate: 1
service B (tracesSampleRate: 1) sent spans: 102; client_report discards: []
Enter fullscreen mode Exit fullscreen mode

Service B was configured to trace everything and sent nothing, because the caller in front of it said "not sampled". Picture an API gateway where someone turned tracing "off" with a zero, and a team debugging latency in a service behind it. That team gets empty traces, and nothing in their own config explains it. The only hint is a sample_rate discard count in their client reports.

With the key removed, the caller still sends sentry-trace and baggage, but without a sampled flag (sampled flag {"(none)":51} in run-1-unset.txt), so B makes its own decision.

The environment variable trap

tracesSampleRate has an env var, SENTRY_TRACES_SAMPLE_RATE (listed on the Node options page). I ran the key-not-set config again with SENTRY_TRACES_SAMPLE_RATE=0 in the environment, run-8-unset-env-zero.txt:

## MODE=unset SENTRY_TRACES_SAMPLE_RATE=0
{"MODE":"unset","spans_started":103,"spans_recording":0,"spans_ended":51,"rss_mb":118,"N":50,"ms_for_N":192}
## stub saw:
envelope items: {"client_report":2,"event":1}
Enter fullscreen mode Exit fullscreen mode

Identical to the zero run. So you can delete the key from the code and still have tracing on, because someone set the variable to 0 in a deploy template "to be safe". Check printenv | grep SENTRY_TRACES on the box, not only the code.

The errors-only config I use now

// instrument-errors-only.js: load this first (node --require ./instrument-errors-only.js app.js)
const Sentry = require('@sentry/node');

Sentry.init({
  dsn: process.env.SENTRY_DSN,
  // no tracesSampleRate, no tracesSampler: tracing stays off
  tracePropagationTargets: [], // optional: no sentry-trace/baggage on outgoing calls
});
Enter fullscreen mode Exit fullscreen mode

run-10-recipe.txt loads exactly this file through NODE_OPTIONS=--require=./instrument-errors-only.js:

{"MODE":"file","spans_started":0,"spans_recording":0,"spans_ended":0,"rss_mb":115,"N":50,"ms_for_N":185}
## stub saw:
envelope items: {"event":1}
client_report discards: []
error events: [{"message":"checkout failed","trace_id":"ff038e0d31824c7dac48d8df4c23b083"}]
downstream calls: 51, with sentry-trace: 0 (sampled flag {}), with baggage: 0, with traceparent: 0
Enter fullscreen mode Exit fullscreen mode

The error event still carries a trace_id in its trace context, so turning tracing off doesn't strip that field. tracePropagationTargets: [] is the documented way to stop propagation ("If you want to disable trace propagation, you can set this option to []"). Leave it out if other services rely on your trace IDs to join their own errors.

Check your own service with a 17-line stub

The full lab is six small files, but you only need a fake ingest to check your own app. Save this as mini-stub.js:

// mini-stub.js: fake ingest on :9999. Prints item types and client_report discards for every envelope.
const zlib = require('zlib');
require('http').createServer((req, res) => {
  const chunks = [];
  req.on('data', c => chunks.push(c));
  req.on('end', () => {
    let buf = Buffer.concat(chunks);
    if (req.headers['content-encoding'] === 'gzip') buf = zlib.gunzipSync(buf);
    const lines = buf.toString().split('\n');
    for (let i = 1; i < lines.length && lines[i]; i += 2) {
      const type = JSON.parse(lines[i]).type;
      const extra = type === 'client_report' ? ' ' + JSON.stringify(JSON.parse(lines[i + 1]).discarded_events) : '';
      console.log(type + extra);
    }
    res.end('{}');
  });
}).listen(9999, () => console.log('listening on :9999'));
Enter fullscreen mode Exit fullscreen mode

Run node mini-stub.js, start your service with SENTRY_DSN=http://[email protected]:9999/1, send it a few requests and one error, then stop it so the SDK flushes. Here is what it printed for my app with tracesSampleRate: 0 and 5 requests, run-11-mini-stub-zero.txt:

listening on :9999
client_report [{"reason":"sample_rate","category":"span","quantity":12}]
event
client_report [{"reason":"sample_rate","category":"span","quantity":1}]
Enter fullscreen mode Exit fullscreen mode

And with the key removed, run-11-mini-stub-unset.txt:

listening on :9999
event
Enter fullscreen mode Exit fullscreen mode

If you see a client_report line with "category":"span", something in your setup still turns tracing on.

Checklist before you call it errors-only

  1. grep -rn "tracesSampleRate\|tracesSampler" . finds nothing in your Sentry init. A 0 counts as a hit.
  2. SENTRY_TRACES_SAMPLE_RATE is unset in every environment, including CI and your container platform's variables.
  3. The stub above shows no client_report with "reason":"sample_rate","category":"span" for your service.
  4. Downstream services that do trace still get spans when called through you. A sampled=false on their incoming sentry-trace header means a caller is still at zero.
  5. Before you set tracePropagationTargets: [], check that no downstream team joins their errors to yours by trace_id.
  6. You've rerun the stub for each SDK you ship. I only tested plain @sentry/node 11.14.0 on Node 24.22.1. Next.js and the browser SDK weren't part of this lab.

If you run more than one service, is any caller in your stack still set to tracesSampleRate: 0?

Top comments (0)