Skip to content

ref(node): Streamline mongoose instrumentation#21481

Open
logaretm wants to merge 9 commits into
developfrom
awad/js-2389-streamline-opentelemetryinstrumentation-mongoose
Open

ref(node): Streamline mongoose instrumentation#21481
logaretm wants to merge 9 commits into
developfrom
awad/js-2389-streamline-opentelemetryinstrumentation-mongoose

Conversation

@logaretm

@logaretm logaretm commented Jun 11, 2026

Copy link
Copy Markdown
Member

Streamlines the vendored mongoose instrumentation to use Sentry's span APIs instead of the OpenTelemetry tracing API, and removes the code paths that are dead in Sentry's context.

I also ported OTel's own integration tests of mongoose and added my own assertions to it.

I didn't fix the older semantic attributes as I believe that would be a breaking change right now.

Fixes #20737

logaretm added 7 commits June 11, 2026 15:29
Replace the OpenTelemetry tracing APIs in the vendored mongoose
instrumentation with Sentry primitives (startInactiveSpan, getActiveSpan,
span.setStatus) and bake the span origin in directly. Also drops the
config paths that are dead in Sentry's context (dbStatementSerializer,
requireParentSpan, suppressInternalInstrumentation) and the
SemconvStability dual-emission machinery, keeping only the OLD attribute
set. The OpenTelemetry instrumentation base/module-patching layer is kept.
With the OTel responseHook/dbStatementSerializer paths removed, the only
config the SDK uses is the base InstrumentationConfig. Collapse
MongooseInstrumentationConfig to that and drop the now-unused
SerializerPayload, DbStatementSerializer, ResponseInfo and
MongooseResponseCustomAttributesFunction types.
Remove ATTR_DB_STATEMENT (fed the removed dbStatementSerializer path) and
DB_SYSTEM_NAME_VALUE_MONGODB (only used by the dropped stable-semconv
branch). The remaining constants are the OLD attribute set the
instrumentation still emits.
Port the upstream OTel mongoose test coverage as unit tests that drive a
fake mongoose module through MongooseInstrumentation and assert the
produced Sentry spans. Covers save, query exec, aggregate, insertMany,
bulkWrite, document update methods (v8.21+), remove (v5/v6), error
status, parent-span linking and unpatch.
Type the vendored instrumentation's `module` parameter (MongooseModule /
MongooseModuleExports) instead of `any`, so the patch/unpatch logic is
checked against the mongoose shape. Adjust lint config accordingly.
…ion suite

Exercise more mongoose operations against the real mongodb-memory-server
so the live suite asserts span output (name, OLD attributes, op, origin)
for aggregate, insertMany and bulkWrite in addition to save/findOne.
@logaretm logaretm force-pushed the awad/js-2389-streamline-opentelemetryinstrumentation-mongoose branch from b56fa12 to f5972de Compare June 11, 2026 20:17
@linear-code

linear-code Bot commented Jun 11, 2026

Copy link
Copy Markdown

JS-2389

Comment thread .oxlintrc.base.json
Comment on lines +164 to +166
"typescript/no-explicit-any": "off",
"no-unsafe-member-access": "off",
"no-this-alias": "off"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are very common in instrumentations, so I wanted to remove the blanket eslint-disable we have and instead target the problematic rules.

@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size % Change Change
@sentry/browser 27.39 kB - -
@sentry/browser - with treeshaking flags 25.82 kB - -
@sentry/browser (incl. Tracing) 45.68 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 47.92 kB - -
@sentry/browser (incl. Tracing, Profiling) 50.48 kB - -
@sentry/browser (incl. Tracing, Replay) 84.9 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 74.51 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 89.6 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 102.28 kB - -
@sentry/browser (incl. Feedback) 44.55 kB - -
@sentry/browser (incl. sendFeedback) 32.19 kB - -
@sentry/browser (incl. FeedbackAsync) 37.3 kB - -
@sentry/browser (incl. Metrics) 28.46 kB - -
@sentry/browser (incl. Logs) 28.69 kB - -
@sentry/browser (incl. Metrics & Logs) 29.39 kB - -
@sentry/react 29.18 kB - -
@sentry/react (incl. Tracing) 47.98 kB - -
@sentry/vue 32.4 kB - -
@sentry/vue (incl. Tracing) 47.57 kB - -
@sentry/svelte 27.41 kB - -
CDN Bundle 29.78 kB - -
CDN Bundle (incl. Tracing) 48.16 kB - -
CDN Bundle (incl. Logs, Metrics) 31.32 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.48 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.61 kB - -
CDN Bundle (incl. Tracing, Replay) 85.51 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 86.75 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.34 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.6 kB - -
CDN Bundle - uncompressed 88.46 kB - -
CDN Bundle (incl. Tracing) - uncompressed 145.67 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 93.17 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 149.65 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 217.99 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 264.54 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 268.5 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 278.24 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 282.19 kB - -
@sentry/nextjs (client) 50.43 kB - -
@sentry/sveltekit (client) 46.1 kB - -
@sentry/core/server 76.08 kB - -
@sentry/core/browser 63.22 kB - -
@sentry/node-core 61.72 kB -0.01% -2 B 🔽
@sentry/node 129.97 kB -0.43% -557 B 🔽
@sentry/node - without tracing 74.11 kB +0.01% +1 B 🔺
@sentry/aws-serverless 86.29 kB +0.01% +1 B 🔺
@sentry/cloudflare (withSentry) - minified 173.69 kB - -
@sentry/cloudflare (withSentry) 433.85 kB - -

View base workflow run

@logaretm logaretm marked this pull request as ready for review June 11, 2026 20:57
@logaretm logaretm requested a review from a team as a code owner June 11, 2026 20:57
@logaretm logaretm requested review from JPeer264 and andreiborza and removed request for a team June 11, 2026 20:57
attributes,
parentSpan,
);
const span = self._startSpan(this._model.collection, this._model?.modelName, 'aggregate', parentSpan);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we possibly rewrite this to use startSpan(..., () => callback) or a similar thing? Using our active span wrapper has some benefits as that has built in error handling etc 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed the initial implementation used inactive spans so I didn't want to change semantics here and opted to use the startInactiveSpans to keep things as they are, minimal changes and all of that.

Now, I don't really see a lot of spans getting affected by switching to startSpan. The only thing that would run inside mongoose spans would be a mongodb driver calls, so I believe that is a good thing for them to be parented to it.

I will try switching to a mix of startSpan and startSpanManual (for callback-based paths) and see if that works.

@logaretm logaretm Jun 12, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It changes more than I expected, so we change some error messages (which we can redecorate) but then there is the thenable issue....

Some methods, like 8.21+ document's updateOne/deleteOne don't return a real Promise, they return a lazy Query. The instrumentation has to hand that Query back to the caller un-executed (we just tag it so the eventual .exec() isn't instrumented twice).

This interacts badly with startSpan/startSpanManual as handleCallbackErrors calls .then() on anything thenable, and for a mongoose Query, .then() would execute it. So wrapping these in startSpan runs the query prematurely.

I tested this against mongoose 8.21 and it did produce this issue, so it seems like we can't do that. I will add tests for these cases since our CI wouldn't have caught it.

For parenting I could just do withActiveSpan but that's just a single call change.

@nicohrubec nicohrubec self-requested a review June 12, 2026 10:59
Wrap the mongoose operation in `withActiveSpan` so the span is active while
the underlying driver call runs. This parents the mongodb driver spans under
the corresponding mongoose span (e.g. mongoose.User.save -> mongodb insert)
instead of leaving them as siblings.

`withActiveSpan` returns the callback result untouched, so it does not call
`.then()` on lazy mongoose Query thenables (unlike startSpan/startSpanManual,
which would execute them prematurely). The active window is synchronous-only,
so unrelated spans are not parented to the mongoose span.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 66ac64a. Configure here.

kind: SpanKind.CLIENT,
attributes,
parentSpan,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing mongoose span op

Medium Severity

Mongoose instrumentation creates spans via startInactiveSpan with origin and DB attributes but never sets sentry.op. After switching from OpenTelemetry spans (where the exporter inferred db from semantic conventions), finished spans and transaction children no longer get op: 'db', which breaks integration expectations and Sentry span categorization.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit 66ac64a. Configure here.

Add integration suites pinning mongoose 7, 8 and 9 (alongside the existing
v6 suite) so every supported version branch is exercised against a real
mongoose: contextCaptureFunctions7 (v7), the 8.21+ document-method path (v8)
and the latest major (v9).

The v8 suite exercises document `updateOne`/`deleteOne`, whose lazy Query
must be handed back un-executed. A unit test additionally guards this
directly: it asserts the instrumentation never calls `.then()` on the
returned thenable Query (which `startSpan`/`startSpanManual` would do,
executing the query prematurely).
@logaretm logaretm force-pushed the awad/js-2389-streamline-opentelemetryinstrumentation-mongoose branch from ee989f4 to d0fb03f Compare June 12, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streamline @opentelemetry/instrumentation-mongoose

2 participants