From cb43f3da29ba2332ebea41a3477a5df95beb4ba7 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 12 Aug 2026 11:31:33 +0200 Subject: [PATCH] ref(server-utils): Streamline mongoose integration --- packages/server-utils/src/index.ts | 1 - .../{mongoose.ts => mongoose/index.ts} | 18 +++++----- .../mongoose/mongoose-dc-subscriber.ts | 4 +-- .../mongoose/mongoose-legacy-span.ts | 0 packages/server-utils/src/mongoose/index.ts | 33 ------------------- 5 files changed, 11 insertions(+), 45 deletions(-) rename packages/server-utils/src/integrations/{mongoose.ts => mongoose/index.ts} (90%) rename packages/server-utils/src/{ => integrations}/mongoose/mongoose-dc-subscriber.ts (98%) rename packages/server-utils/src/{ => integrations}/mongoose/mongoose-legacy-span.ts (100%) delete mode 100644 packages/server-utils/src/mongoose/index.ts diff --git a/packages/server-utils/src/index.ts b/packages/server-utils/src/index.ts index c83a48ca8e15..043896b5cabe 100644 --- a/packages/server-utils/src/index.ts +++ b/packages/server-utils/src/index.ts @@ -1,7 +1,6 @@ export * from './exports'; // Exports using diagnostics channels -export { mongooseIntegration } from './mongoose'; export { mysql2Integration } from './mysql2'; export { instrumentPrisma, prismaIntegration } from './prisma'; export type { PrismaInstrumentationConfig, PrismaOptions } from './prisma'; diff --git a/packages/server-utils/src/integrations/mongoose.ts b/packages/server-utils/src/integrations/mongoose/index.ts similarity index 90% rename from packages/server-utils/src/integrations/mongoose.ts rename to packages/server-utils/src/integrations/mongoose/index.ts index 4d01001cdf70..a655f27d523e 100644 --- a/packages/server-utils/src/integrations/mongoose.ts +++ b/packages/server-utils/src/integrations/mongoose/index.ts @@ -1,15 +1,15 @@ import * as diagnosticsChannel from 'node:diagnostics_channel'; import type { IntegrationFn, Span } from '@sentry/core'; import { defineIntegration, getActiveSpan, waitForTracingChannelBinding } from '@sentry/core'; -import { subscribeMongooseDiagnosticChannels } from '../mongoose/mongoose-dc-subscriber'; -import type { MongooseLegacyCollection } from '../mongoose/mongoose-legacy-span'; -import { startMongooseLegacySpan } from '../mongoose/mongoose-legacy-span'; -import { CHANNELS } from '../orchestrion/channels'; -import { MONGOOSE_CONTEXT_CAPTURE_CHANNELS } from '../orchestrion/config/mongoose'; -import type { SentryTracingChannel } from '../tracing-channel'; -import { bindTracingChannelToSpan } from '../tracing-channel'; -import { mongooseModuleNames } from '../orchestrion/config/mongoose'; -import { invokeOrchestrionInstrumentation } from '../orchestrion/instrumentation'; +import { subscribeMongooseDiagnosticChannels } from './mongoose-dc-subscriber'; +import type { MongooseLegacyCollection } from './mongoose-legacy-span'; +import { startMongooseLegacySpan } from './mongoose-legacy-span'; +import { CHANNELS } from '../../orchestrion/channels'; +import { MONGOOSE_CONTEXT_CAPTURE_CHANNELS } from '../../orchestrion/config/mongoose'; +import type { SentryTracingChannel } from '../../tracing-channel'; +import { bindTracingChannelToSpan } from '../../tracing-channel'; +import { mongooseModuleNames } from '../../orchestrion/config/mongoose'; +import { invokeOrchestrionInstrumentation } from '../../orchestrion/instrumentation'; const INTEGRATION_NAME = 'Mongoose' as const; diff --git a/packages/server-utils/src/mongoose/mongoose-dc-subscriber.ts b/packages/server-utils/src/integrations/mongoose/mongoose-dc-subscriber.ts similarity index 98% rename from packages/server-utils/src/mongoose/mongoose-dc-subscriber.ts rename to packages/server-utils/src/integrations/mongoose/mongoose-dc-subscriber.ts index fa11fe8e87d3..262c4fc05587 100644 --- a/packages/server-utils/src/mongoose/mongoose-dc-subscriber.ts +++ b/packages/server-utils/src/integrations/mongoose/mongoose-dc-subscriber.ts @@ -16,8 +16,8 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startInactiveSpan, } from '@sentry/core'; -import { DEBUG_BUILD } from '../debug-build'; -import { bindTracingChannelToSpan } from '../tracing-channel'; +import { DEBUG_BUILD } from '../../debug-build'; +import { bindTracingChannelToSpan } from '../../tracing-channel'; // Channel names published by mongoose >= 9.7.0 (see mongoose `lib/tracing.js`, // `lib/query.js`, `lib/aggregate.js`, `lib/model.js` and `lib/cursor/*`). diff --git a/packages/server-utils/src/mongoose/mongoose-legacy-span.ts b/packages/server-utils/src/integrations/mongoose/mongoose-legacy-span.ts similarity index 100% rename from packages/server-utils/src/mongoose/mongoose-legacy-span.ts rename to packages/server-utils/src/integrations/mongoose/mongoose-legacy-span.ts diff --git a/packages/server-utils/src/mongoose/index.ts b/packages/server-utils/src/mongoose/index.ts deleted file mode 100644 index c16ee5811751..000000000000 --- a/packages/server-utils/src/mongoose/index.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { defineIntegration, type IntegrationFn, waitForTracingChannelBinding } from '@sentry/core'; -import * as dc from 'node:diagnostics_channel'; -import { subscribeMongooseDiagnosticChannels } from './mongoose-dc-subscriber'; - -export type { MongooseLegacyCollection, StartMongooseLegacySpanOptions } from './mongoose-legacy-span'; -export { startMongooseLegacySpan } from './mongoose-legacy-span'; - -const _mongooseIntegration = (() => { - return { - name: 'Mongoose', - setupOnce() { - // Bail on Node <= 18.18.0, where `tracingChannel` does not exist. - if (!dc.tracingChannel) { - return; - } - - // Subscribe to mongoose's native tracing channels (mongoose >= 9.7). - // This is a no-op on versions that don't publish to the channels, so it is always safe to call. - waitForTracingChannelBinding(() => { - subscribeMongooseDiagnosticChannels(dc.tracingChannel); - }); - }, - }; -}) satisfies IntegrationFn; - -/** - * Auto-instrument the [mongoose](https://www.npmjs.com/package/mongoose) library via its native - * `node:diagnostics_channel` tracing channels (mongoose >= 9.7). - * - * On older mongoose versions the channels are never published to, so this integration is inert and - * the IITM-based patcher (gated to `< 9.7.0`) handles instrumentation instead. - */ -export const mongooseIntegration = defineIntegration(_mongooseIntegration);