Skip to content

feat(instrumentation-mongoose): add instrumentation of static methods #2748

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
latest fixes and additional tests
  • Loading branch information
Vovcharaa committed May 19, 2025
commit 167682d42bab7dee9891b1b3ebf80b89c6c7c5e6
6 changes: 1 addition & 5 deletions plugins/node/instrumentation-mongoose/.tav.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
mongoose:
- versions:
include: ">=5.10.19 <6"
mode: latest-minors
commands: npm run test-v5-v6
- versions:
include: ">=6.7.5 <7"
include: ">=5.9.7 <7"
mode: latest-minors
commands: npm run test-v5-v6
- versions:
Expand Down
5 changes: 2 additions & 3 deletions plugins/node/instrumentation-mongoose/src/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
protected init(): InstrumentationModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(
'mongoose',
['>=5.10.19 <6', '>=6.7.5 <9'],
['>=5.9.7 <9'],
this.patch.bind(this),
this.unpatch.bind(this)
);
Expand Down Expand Up @@ -343,8 +343,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
) {
return original.apply(this, arguments);
}

if (options instanceof Function) {
if (typeof options === 'function') {
callback = options;
options = undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/node/instrumentation-mongoose/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ export function handleCallbackResponse(
let callbackArgumentIndex = 0;
if (args.length === 2) {
callbackArgumentIndex = 1;
} else if (args.length === 3){
callbackArgumentIndex = 2;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the fix for insertMany/bulkWrite with callback and options.
It also fixed issues with older versions, which I previously dropped from support.

}

args[callbackArgumentIndex] = (err: Error, response: any): any => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ describe('mongoose instrumentation [common]', () => {
},
});
await loadUsers();
await User.createIndexes();
instrumentation.enable();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ describe('mongoose instrumentation [v5/v6]', () => {
},
});
await loadUsers();
await User.createIndexes();
instrumentation.enable();
});

Expand Down Expand Up @@ -152,6 +151,61 @@ describe('mongoose instrumentation [v5/v6]', () => {
});
});

describe('when insertMany call has callback', async () => {
it('instrumenting insertMany operation with generic options and callback', done => {
const documents = [
{
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
},
{
firstName: 'Jane',
lastName: 'Doe',
email: '[email protected]',
},
];
// @ts-ignore - v7 removed callback support
// https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support
User.insertMany(documents, { ordered: true }, () => {
const spans = getTestSpans();
expect(spans.length).toBe(1);
assertSpan(spans[0] as ReadableSpan);
expect(spans[0].attributes[SEMATTRS_DB_OPERATION]).toBe('insertMany');
const statement = getStatement(spans[0] as ReadableSpan);
expect(statement.documents).toEqual(documents);
expect(statement.options.ordered).toEqual(true);
done();
});
});

it('instrumenting insertMany operation with only callback', done => {
const documents = [
{
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
},
{
firstName: 'Jane',
lastName: 'Doe',
email: '[email protected]',
},
];
// @ts-ignore - v7 removed callback support
// https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support
User.insertMany(documents, () => {
const spans = getTestSpans();
expect(spans.length).toBe(1);
assertSpan(spans[0] as ReadableSpan);
expect(spans[0].attributes[SEMATTRS_DB_OPERATION]).toBe('insertMany');
const statement = getStatement(spans[0] as ReadableSpan);
expect(statement.documents).toEqual(documents);
done();
});
});
});

describe('remove operation', () => {
it('instrumenting remove operation [deprecated]', async () => {
const user = await User.findOne({ email: '[email protected]' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ describe('mongoose instrumentation [v7/v8]', () => {
},
});
await loadUsers();
await User.createIndexes();
instrumentation.enable();
});

Expand Down
2 changes: 0 additions & 2 deletions plugins/node/instrumentation-mongoose/test/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
import { Schema, Document } from 'mongoose';
import * as mongoose from 'mongoose';
import { context } from '@opentelemetry/api';
import { suppressTracing } from '@opentelemetry/core';

export interface IUser extends Document {
email: string;
Expand Down