Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
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));
Comment thread
cursor[bot] marked this conversation as resolved.
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,44 @@ 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() } } }]);

// `remove` is a real document method (deprecated in 6, removed in 7), only patched for v5/6.
const toRemove = await BlogPost.create({ title: 'Remove', body: 'r', date: new Date() });
await toRemove.remove();

// Cross-context parent: a query built inside one span but executed after it ends should still
// be parented to the span it was built in (via _STORED_PARENT_SPAN), not the active span at exec.
let pendingQuery;
Sentry.startSpan({ name: 'query-builder' }, () => {
pendingQuery = BlogPost.findOne({ title: 'Test' });
});
await pendingQuery;

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