Skip to content

Mongo integration serializes every command even when the span is not recorded (high CPU/memory on large writes) #26812

Done

Description

@a-ledu

Is there an existing issue for this?

How do you use Sentry?

Self-hosted/on-premise

Which SDK are you using?

@sentry/node

SDK Version

11.11.0

Framework Version

Express 5.3

Link to Sentry event

No response

Reproduction Example/SDK Setup

Sentry.init({
  dsn: '...',
  tracesSampleRate: 0, // same result with 0.1 and 1
  enableRuntimeChannelInjection: true,
  traceLifecycle: 'static',
  dataCollection: { databaseQueryData: false },
});

(Sentry is initialised before mongoose/mongodb are loaded.)

Steps to Reproduce

  1. Init Sentry as above, with tracesSampleRate: 0.
  2. With mongoose (or the mongodb driver), run bulkWrite / updateOne commands whose update documents are several MB each (deeply nested objects), 5 in parallel, in a loop.
  3. Sample process.memoryUsage().rss and take a CPU profile (--cpu-prof).
  4. Repeat with integrations: (d) => d.filter((i) => i.name !== 'Mongo').

Expected Result

With tracesSampleRate: 0 (no span can be recorded) and dataCollection.databaseQueryData: false, the Mongo integration should not serialize the command. Memory and CPU should stay close to running without Sentry.

Actual Result

The Mongo integration serializes every MongoDB command, even when the span will not be recorded.

In @sentry/server-utils integrations/mongodb, subscribeV4Command calls getV4SpanAttributes() on every Connection.command. That calls serializeDbStatement(), which runs scrubStatement() over the whole command and then JSON.stringify. This happens before startInactiveSpan({ onlyIfParent: true }), so the work is done even when there is no sampled parent span and the span is dropped. It also ignores dataCollection.databaseQueryData: false.

scrubStatement rebuilds the whole command as Object.entries/map/reduce arrays. On update/bulkWrite commands carrying multi-megabyte documents, each command allocates several copies of its payload.

Measured on a job that bulk-writes multi-MB documents, 5 in parallel (Node 26.13):

Setup Peak RSS Time per round
Sentry off 1283 MB 6.5 s
Sentry 11, tracesSampleRate 0 3623 MB 15.7 s
Sentry 11, tracesSampleRate 0.1 2537 MB 15.6 s
Sentry 11 without the Mongo integration 1200 MB 6.5 s
Sentry 10.53 (OTel mongodb instrumentation), tracesSampleRate 1 ~1.1 GB —

CPU profile with tracesSampleRate 0: 51% of the time is in scrubStatement (mongodb-span.js), plus 11% in GC.

Additional Context

Same on Node 22 and Node 26, so it isn't the runtime. In production the containers crashed with "Fatal process out of memory" while the JS heap was only 200–300 MB.

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Activity

  1. linear-code commented on Nov 24, 2026

    @linear-code
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Nov 24, 2026
  3. JPeer264 commented on Nov 24, 2026

    @JPeer264
    Member

    Thanks a lot for writing in. First off, there is a difference between setting tracesSampleRate: 0 and not setting it at all as setting it to 0 enables tracing, but just doesn't send anything. I'm quite surprised by the memory consumption between v10 and v11 of the SDK though, that should be checked out too. I'll check out if we can improve performance and memory allocation there.

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Nov 24, 2026
  5. self-assigned this
    on Nov 24, 2026
  6. JPeer264 commented on Nov 24, 2026

    @JPeer264
    Member

    I did some further benchmarks between v10 and v11, and it seems the regression is already fixed with the opened PR

  7. added a commit that references this issue on Nov 24, 2026
    92aed06
  8. github-actions commented on Nov 25, 2026

    @github-actions
    Contributor

    A PR closing this issue has just been released 🚀

    This issue was referenced by PR #26826, which was included in the 11.18.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions