Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as Sentry from '@sentry/node';
import mongoose from 'mongoose';

async function run() {
await mongoose.connect(process.env.MONGO_URL || '');

const BlogPost = mongoose.model('BlogPost', new mongoose.Schema({ title: String }));

await Sentry.startSpan(
{
name: 'Test Transaction',
op: 'transaction',
},
async () => {
// An unrecognized aggregation stage makes mongodb reject the operation. The rejection must flag
// the mongoose channel span as errored (the SDK marks the span but does not swallow the error).
await BlogPost.aggregate([{ $notAValidStage: {} }]).catch(() => {
// swallowed here so the scenario's transaction still completes and is flushed
});
},
);
}

run();
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,47 @@ conditionalTest({ min: 20 })('Mongoose tracing channel Test', () => {
.start()
.completed();
});

test('omits db.query.text for the empty-filter cursor and does not treat the cursor batchSize as a batch', async () => {
await createTestRunner()
.expect({
transaction: event => {
const spans = event.spans || [];
// the `.find().cursor()` iteration runs with no filter, so there is no query text to emit
const cursorFind = spans.find(span => span.description === 'mongoose.blogposts.find');
expect(cursorFind).toBeDefined();
expect(cursorFind?.data?.['db.query.text']).toBeUndefined();
// a cursor's `batchSize` is a fetch-tuning option, not a batch-operation size
expect(cursorFind?.data?.['db.operation.batch.size']).toBeUndefined();
},
})
.start()
.completed();
});
},
{ additionalDependencies: { mongoose: '^9.7' } },
);

// A failed operation must flag the mongoose channel span as errored. mongodb error statuses map to
// `internal_error` through the OTel pipeline (same as the postgres/redis suites).
createEsmAndCjsTests(
__dirname,
'scenario-error.mjs',
'instrument.mjs',
(createTestRunner, test) => {
test('flags the mongoose channel span as errored when the operation fails', async () => {
await createTestRunner()
.expect({
transaction: event => {
const spans = event.spans || [];
const aggregateSpan = spans.find(span => span.description === 'mongoose.blogposts.aggregate');
expect(aggregateSpan).toBeDefined();
expect(aggregateSpan?.status).toBe('internal_error');
},
})
.start()
.completed();
});
},
{ additionalDependencies: { mongoose: '^9.7' } },
);
Expand Down
Loading
Loading