Skip to content

Commit 72c0f52

Browse files
committed
latest fixes and additional tests
1 parent 979415c commit 72c0f52

File tree

7 files changed

+60
-13
lines changed

7 files changed

+60
-13
lines changed

plugins/node/instrumentation-mongoose/.tav.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
mongoose:
22
- versions:
3-
include: ">=5.10.19 <6"
4-
mode: latest-minors
5-
commands: npm run test-v5-v6
6-
- versions:
7-
include: ">=6.7.5 <7"
3+
include: ">=5.9.7 <7"
84
mode: latest-minors
95
commands: npm run test-v5-v6
106
- versions:

plugins/node/instrumentation-mongoose/src/mongoose.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
9999
protected init(): InstrumentationModuleDefinition {
100100
const module = new InstrumentationNodeModuleDefinition(
101101
'mongoose',
102-
['>=5.10.19 <6', '>=6.7.5 <9'],
102+
['>=5.9.7 <9'],
103103
this.patch.bind(this),
104104
this.unpatch.bind(this)
105105
);
@@ -343,8 +343,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
343343
) {
344344
return original.apply(this, arguments);
345345
}
346-
347-
if (options instanceof Function) {
346+
if (typeof options === 'function') {
348347
callback = options;
349348
options = undefined;
350349
}

plugins/node/instrumentation-mongoose/src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ export function handleCallbackResponse(
105105
let callbackArgumentIndex = 0;
106106
if (args.length === 2) {
107107
callbackArgumentIndex = 1;
108+
} else if (args.length === 3){
109+
callbackArgumentIndex = 2;
108110
}
109111

110112
args[callbackArgumentIndex] = (err: Error, response: any): any => {

plugins/node/instrumentation-mongoose/test/mongoose-common.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ describe('mongoose instrumentation [common]', () => {
7575
},
7676
});
7777
await loadUsers();
78-
await User.createIndexes();
7978
instrumentation.enable();
8079
});
8180

plugins/node/instrumentation-mongoose/test/mongoose-v5-v6.test.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ describe('mongoose instrumentation [v5/v6]', () => {
7272
},
7373
});
7474
await loadUsers();
75-
await User.createIndexes();
7675
instrumentation.enable();
7776
});
7877

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

154+
describe('when insertMany call has callback', async () => {
155+
it('instrumenting insertMany operation with generic options and callback', done => {
156+
const documents = [
157+
{
158+
firstName: 'John',
159+
lastName: 'Doe',
160+
161+
},
162+
{
163+
firstName: 'Jane',
164+
lastName: 'Doe',
165+
166+
},
167+
];
168+
// @ts-ignore - v7 removed callback support
169+
// https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support
170+
User.insertMany(documents, { ordered: true }, () => {
171+
const spans = getTestSpans();
172+
expect(spans.length).toBe(1);
173+
assertSpan(spans[0] as ReadableSpan);
174+
expect(spans[0].attributes[SEMATTRS_DB_OPERATION]).toBe('insertMany');
175+
const statement = getStatement(spans[0] as ReadableSpan);
176+
expect(statement.documents).toEqual(documents);
177+
expect(statement.options.ordered).toEqual(true);
178+
done();
179+
});
180+
});
181+
182+
it('instrumenting insertMany operation with only callback', done => {
183+
const documents = [
184+
{
185+
firstName: 'John',
186+
lastName: 'Doe',
187+
188+
},
189+
{
190+
firstName: 'Jane',
191+
lastName: 'Doe',
192+
193+
},
194+
];
195+
// @ts-ignore - v7 removed callback support
196+
// https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support
197+
User.insertMany(documents, () => {
198+
const spans = getTestSpans();
199+
expect(spans.length).toBe(1);
200+
assertSpan(spans[0] as ReadableSpan);
201+
expect(spans[0].attributes[SEMATTRS_DB_OPERATION]).toBe('insertMany');
202+
const statement = getStatement(spans[0] as ReadableSpan);
203+
expect(statement.documents).toEqual(documents);
204+
done();
205+
});
206+
});
207+
});
208+
155209
describe('remove operation', () => {
156210
it('instrumenting remove operation [deprecated]', async () => {
157211
const user = await User.findOne({ email: '[email protected]' });

plugins/node/instrumentation-mongoose/test/mongoose-v7-v8.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ describe('mongoose instrumentation [v7/v8]', () => {
6969
},
7070
});
7171
await loadUsers();
72-
await User.createIndexes();
7372
instrumentation.enable();
7473
});
7574

plugins/node/instrumentation-mongoose/test/user.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
import { Schema, Document } from 'mongoose';
1717
import * as mongoose from 'mongoose';
18-
import { context } from '@opentelemetry/api';
19-
import { suppressTracing } from '@opentelemetry/core';
2018

2119
export interface IUser extends Document {
2220
email: string;

0 commit comments

Comments
 (0)