Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .oxlintrc.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@
"**/integrations/tracing/fastify/vendored/**/*.ts"
],
"rules": {
"typescript/no-explicit-any": "off"
"typescript/no-explicit-any": "off",
"no-unsafe-member-access": "off",
"no-this-alias": "off"
Comment on lines +164 to +166

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.

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.

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?

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 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.

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.

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?

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.

Yea I think they will be noops unless orchestrion cares about types, which i don't think it does.

}
},
{
Expand Down
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));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fixed sleep invites flaky regression

Low Severity

The lazy-updateOne guard waits with setTimeout(..., 250) instead of a concrete completion signal. Slow CI or delayed writes can let a premature execution finish after the sleep, so the scenario may pass even when instrumentation incorrectly runs the query early.

Fix in Cursor Fix in Web

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' } },
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ async function run() {
await post.save();

await BlogPost.findOne({});

// Callback form (mongoose 5/6 only): the callback is passed as the sole argument, so it must
// be forwarded in the correct position. Reject if the callback doesn't receive the saved doc.
await new Promise((resolve, reject) => {
new BlogPost({ title: 'Callback', body: 'cb', date: new Date() }).save((err, doc) => {
if (err) {
reject(err);
} else if (!doc || doc.title !== 'Callback') {
reject(new Error('save(callback) did not receive the saved document'));
} else {
resolve();
}
});
});

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() } } }]);

// Failing operation: a save that violates required-field validation should still produce a
// span, marked with an error status.
const RequiredSchema = new Schema({ requiredField: { type: String, required: true } });
const RequiredDoc = mongoose.model('RequiredDoc', RequiredSchema);
await new RequiredDoc({}).save().catch(() => undefined);
},
);
}
Expand Down
Loading
Loading