-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ref(node): Streamline mongoose instrumentation #21481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
2ab1d67
71ae92d
4a56a8e
2fdcf68
16fe819
d929a72
17871c0
6dcc222
cb3e94f
48b18e6
e2899b2
de8eb0e
cbb3a2c
bc8f876
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import mongoose from 'mongoose'; | ||
|
|
||
| async function run() { | ||
| await mongoose.connect(process.env.MONGO_URL || ''); | ||
|
|
||
| const BlogPostSchema = new mongoose.Schema({ | ||
| title: String, | ||
| body: String, | ||
| date: Date, | ||
| }); | ||
|
|
||
| const BlogPost = mongoose.model('BlogPost', BlogPostSchema); | ||
|
|
||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| const post = new BlogPost({ title: 'Test', body: 'Test body', date: new Date() }); | ||
|
|
||
| await post.save(); | ||
|
|
||
| await BlogPost.findOne({}); | ||
|
|
||
| await BlogPost.aggregate([{ $match: {} }]); | ||
|
|
||
| await BlogPost.insertMany([{ title: 'Insert', body: 'Insert body', date: new Date() }]); | ||
|
|
||
| await BlogPost.bulkWrite([{ insertOne: { document: { title: 'Bulk', body: 'Bulk body', date: new Date() } } }]); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| run(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { MongoMemoryServer } from 'mongodb-memory-server-global'; | ||
| import { afterAll, beforeAll, describe, expect } from 'vitest'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| // Pins mongoose 7 so the `contextCaptureFunctions7` version branch is exercised against a real mongoose. | ||
| describe('Mongoose v7 Test', () => { | ||
| let mongoServer: MongoMemoryServer; | ||
|
|
||
| beforeAll(async () => { | ||
| mongoServer = await MongoMemoryServer.create(); | ||
| process.env.MONGO_URL = mongoServer.getUri(); | ||
| }, 30000); | ||
|
|
||
| afterAll(async () => { | ||
| if (mongoServer) { | ||
| await mongoServer.stop(); | ||
| } | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| const expectedSpan = (operation: string) => | ||
| expect.objectContaining({ | ||
| data: expect.objectContaining({ | ||
| 'db.mongodb.collection': 'blogposts', | ||
| 'db.operation': operation, | ||
| 'db.system': 'mongoose', | ||
| }), | ||
| description: `mongoose.BlogPost.${operation}`, | ||
| op: 'db', | ||
| origin: 'auto.db.otel.mongoose', | ||
| }); | ||
|
|
||
| const EXPECTED_TRANSACTION = { | ||
| transaction: 'Test Transaction', | ||
| spans: expect.arrayContaining([ | ||
| expectedSpan('save'), | ||
| expectedSpan('findOne'), | ||
| expectedSpan('aggregate'), | ||
| expectedSpan('insertMany'), | ||
| expectedSpan('bulkWrite'), | ||
| ]), | ||
| }; | ||
|
|
||
| createEsmAndCjsTests( | ||
| __dirname, | ||
| 'scenario.mjs', | ||
| 'instrument.mjs', | ||
| (createTestRunner, test) => { | ||
| test('auto-instruments `mongoose` v7.', async () => { | ||
| await createTestRunner().expect({ transaction: EXPECTED_TRANSACTION }).start().completed(); | ||
| }); | ||
| }, | ||
| { additionalDependencies: { mongoose: '^7' } }, | ||
| ); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import mongoose from 'mongoose'; | ||
|
|
||
| async function run() { | ||
| await mongoose.connect(process.env.MONGO_URL || ''); | ||
|
|
||
| const BlogPostSchema = new mongoose.Schema({ | ||
| title: String, | ||
| body: String, | ||
| date: Date, | ||
| }); | ||
|
|
||
| const BlogPost = mongoose.model('BlogPost', BlogPostSchema); | ||
|
|
||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| const post = new BlogPost({ title: 'Test', body: 'Test body', date: new Date() }); | ||
|
|
||
| await post.save(); | ||
|
|
||
| await BlogPost.findOne({}); | ||
|
|
||
| // Document instance methods. On mongoose 8.21.0+ these return a lazy Query that the | ||
| // instrumentation must hand back un-executed (regression guard for the thenable trap). | ||
| await post.updateOne({ title: 'Updated' }); | ||
|
|
||
| // Verify the update actually persisted (i.e. the query executed exactly when awaited). | ||
| const updated = await BlogPost.findById(post._id); | ||
| if (!updated || updated.title !== 'Updated') { | ||
| throw new Error(`updateOne did not persist as expected, got: ${updated && updated.title}`); | ||
| } | ||
|
|
||
| // Lazy-Query guard: a document updateOne returns a lazy Query that only runs when awaited. | ||
| // Building it without awaiting must NOT execute it — if the instrumentation runs it (e.g. by | ||
| // calling `.then()` on the returned thenable), this premature write would change the document. | ||
| const lazyDoc = await new BlogPost({ title: 'Original', body: 'b', date: new Date() }).save(); | ||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| lazyDoc.updateOne({ title: 'PrematurelyExecuted' }); | ||
| await new Promise(resolve => setTimeout(resolve, 250)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed sleep invites flaky regressionLow Severity The lazy- Triggered by project rule: PR Review Guidelines for Cursor Bot Reviewed by Cursor Bugbot for commit cbb3a2c. Configure here. |
||
| const lazyCheck = await BlogPost.findById(lazyDoc._id); | ||
| if (!lazyCheck || lazyCheck.title !== 'Original') { | ||
| throw new Error( | ||
| `lazy updateOne was executed without being awaited (got title: ${lazyCheck && lazyCheck.title})`, | ||
| ); | ||
| } | ||
|
|
||
| await post.deleteOne(); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| run(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { MongoMemoryServer } from 'mongodb-memory-server-global'; | ||
| import { afterAll, beforeAll, describe, expect } from 'vitest'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| // Pins mongoose 8 (>= 8.21) so the document `updateOne`/`deleteOne` lazy-Query path is exercised | ||
| // against a real mongoose, guarding the thenable trap that mongoose 6 (the workspace version) can't hit. | ||
| describe('Mongoose v8 Test', () => { | ||
| let mongoServer: MongoMemoryServer; | ||
|
|
||
| beforeAll(async () => { | ||
| mongoServer = await MongoMemoryServer.create(); | ||
| process.env.MONGO_URL = mongoServer.getUri(); | ||
| }, 30000); | ||
|
|
||
| afterAll(async () => { | ||
| if (mongoServer) { | ||
| await mongoServer.stop(); | ||
| } | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| const EXPECTED_TRANSACTION = { | ||
| transaction: 'Test Transaction', | ||
| spans: expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| data: expect.objectContaining({ | ||
| 'db.mongodb.collection': 'blogposts', | ||
| 'db.operation': 'save', | ||
| 'db.system': 'mongoose', | ||
| }), | ||
| description: 'mongoose.BlogPost.save', | ||
| op: 'db', | ||
| origin: 'auto.db.otel.mongoose', | ||
| }), | ||
| expect.objectContaining({ | ||
| data: expect.objectContaining({ | ||
| 'db.mongodb.collection': 'blogposts', | ||
| 'db.operation': 'updateOne', | ||
| 'db.system': 'mongoose', | ||
| }), | ||
| description: 'mongoose.BlogPost.updateOne', | ||
| op: 'db', | ||
| origin: 'auto.db.otel.mongoose', | ||
| }), | ||
| expect.objectContaining({ | ||
| data: expect.objectContaining({ | ||
| 'db.mongodb.collection': 'blogposts', | ||
| 'db.operation': 'deleteOne', | ||
| 'db.system': 'mongoose', | ||
| }), | ||
| description: 'mongoose.BlogPost.deleteOne', | ||
| op: 'db', | ||
| origin: 'auto.db.otel.mongoose', | ||
| }), | ||
| ]), | ||
| }; | ||
|
|
||
| createEsmAndCjsTests( | ||
| __dirname, | ||
| 'scenario.mjs', | ||
| 'instrument.mjs', | ||
| (createTestRunner, test) => { | ||
| test('auto-instruments `mongoose` v8 document methods.', async () => { | ||
| await createTestRunner().expect({ transaction: EXPECTED_TRANSACTION }).start().completed(); | ||
| }); | ||
| }, | ||
| { additionalDependencies: { mongoose: '^8' } }, | ||
| ); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import mongoose from 'mongoose'; | ||
|
|
||
| async function run() { | ||
| await mongoose.connect(process.env.MONGO_URL || ''); | ||
|
|
||
| const BlogPostSchema = new mongoose.Schema({ | ||
| title: String, | ||
| body: String, | ||
| date: Date, | ||
| }); | ||
|
|
||
| const BlogPost = mongoose.model('BlogPost', BlogPostSchema); | ||
|
|
||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| const post = new BlogPost({ title: 'Test', body: 'Test body', date: new Date() }); | ||
|
|
||
| await post.save(); | ||
|
|
||
| await BlogPost.findOne({}); | ||
|
|
||
| await BlogPost.aggregate([{ $match: {} }]); | ||
|
|
||
| await BlogPost.insertMany([{ title: 'Insert', body: 'Insert body', date: new Date() }]); | ||
|
|
||
| await BlogPost.bulkWrite([{ insertOne: { document: { title: 'Bulk', body: 'Bulk body', date: new Date() } } }]); | ||
|
|
||
| // Document instance methods. On v9 these are not doc-method-patched (needsDocumentMethodPatch | ||
| // only matches 8.x) but are still instrumented via the patched Query.exec path. | ||
| const doc = await BlogPost.create({ title: 'DocMethod', body: 'b', date: new Date() }); | ||
| await doc.updateOne({ title: 'DocMethodUpdated' }); | ||
| await doc.deleteOne(); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| run(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { MongoMemoryServer } from 'mongodb-memory-server-global'; | ||
| import { afterAll, beforeAll, expect } from 'vitest'; | ||
| import { conditionalTest } from '../../../utils'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| // Pins mongoose 9 (top of our supported `>=5.9.7 <10` range) so the latest major is exercised | ||
| // against a real mongoose. mongoose 9 requires Node >=20.19, so this suite is skipped on older Node. | ||
| conditionalTest({ min: 20 })('Mongoose v9 Test', () => { | ||
| let mongoServer: MongoMemoryServer; | ||
|
|
||
| beforeAll(async () => { | ||
| mongoServer = await MongoMemoryServer.create(); | ||
| process.env.MONGO_URL = mongoServer.getUri(); | ||
| }, 30000); | ||
|
|
||
| afterAll(async () => { | ||
| if (mongoServer) { | ||
| await mongoServer.stop(); | ||
| } | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| const expectedSpan = (operation: string) => | ||
| expect.objectContaining({ | ||
| data: expect.objectContaining({ | ||
| 'db.mongodb.collection': 'blogposts', | ||
| 'db.operation': operation, | ||
| 'db.system': 'mongoose', | ||
| }), | ||
| description: `mongoose.BlogPost.${operation}`, | ||
| op: 'db', | ||
| origin: 'auto.db.otel.mongoose', | ||
| }); | ||
|
|
||
| const EXPECTED_TRANSACTION = { | ||
| transaction: 'Test Transaction', | ||
| spans: expect.arrayContaining([ | ||
| expectedSpan('save'), | ||
| expectedSpan('findOne'), | ||
| expectedSpan('aggregate'), | ||
| expectedSpan('insertMany'), | ||
| expectedSpan('bulkWrite'), | ||
| // Document instance methods are instrumented via Query.exec on v9 (no doc-method patch). | ||
| expectedSpan('updateOne'), | ||
| expectedSpan('deleteOne'), | ||
| ]), | ||
| }; | ||
|
|
||
| createEsmAndCjsTests( | ||
| __dirname, | ||
| 'scenario.mjs', | ||
| 'instrument.mjs', | ||
| (createTestRunner, test) => { | ||
| test('auto-instruments `mongoose` v9.', async () => { | ||
| await createTestRunner().expect({ transaction: EXPECTED_TRANSACTION }).start().completed(); | ||
| }); | ||
| }, | ||
| { additionalDependencies: { mongoose: '^9' } }, | ||
| ); | ||
| }); |


There was a problem hiding this comment.
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-disablewe have and instead target the problematic rules.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so far I removed the eslint-disable but also fully removed the exemption here (updating the instrumentations were necessary so it passes linting). on second thought might not be worth doing since we are porting to orchestrion soon, but I am not sure yet how much of the current instrumentation code is gonna stay. wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it depends, mongoose needs those off so I assume other similar instrumentations will. Once we switch to orchestrion across the board we can re-tighten this.
I realize that by saying that, someone has to remember to do that 😬 but then those rules would be harmless there since they won't be hiding anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes other instrumentations needed it too (I just updated them so they no longer need the exemptions). my question was more is this worth doing now or do we wait until we are close to the end state (i.e. after orchestrion port)? do you think these will mostly be noops once we move to orchestrion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea I think they will be noops unless orchestrion cares about types, which i don't think it does.