Skip to content

Commit 770130a

Browse files
onurtemizkantrentm
andauthored
feat(instrumentation-mongoose): Support v7 and v8 (#2353)
Co-authored-by: Trent Mick <[email protected]>
1 parent 60a99c9 commit 770130a

File tree

8 files changed

+546
-269
lines changed

8 files changed

+546
-269
lines changed

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
'mongoose':
2-
# Test all the latest minor versions in the range ">=5.9.7 <7".
3-
versions: "5.9.7 || 5.9.29 || 5.10.19 || 5.11.20 || 5.12.15 || 5.13.21 || 6.0.15 || 6.1.10 || 6.2.11 || 6.3.9 || 6.4.7 || 6.5.5 || 6.6.7 || 6.7.5 || 6.8.4 || 6.9.3 || 6.10.5 || 6.11.6 || ^6.12.3"
4-
commands:
5-
- npm run test
1+
mongoose:
2+
- versions:
3+
include: ">=5.9.7 <7"
4+
mode: latest-minors
5+
commands: npm run test-v5-v6
6+
- versions:
7+
include: ">=7 <8"
8+
mode: latest-minors
9+
node: '>=14.20.1'
10+
commands: npm run test-v7-v8
11+
- versions:
12+
include: ">=8 <9"
13+
mode: latest-minors
14+
node: '>=16.20.1'
15+
commands: npm run test-v7-v8

plugins/node/instrumentation-mongoose/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ npm install --save @opentelemetry/instrumentation-mongoose
1717

1818
## Supported Versions
1919

20-
- [`mongoose`](https://www.npmjs.com/package/mongoose) versions `>=5.9.7 <7`
20+
- [`mongoose`](https://www.npmjs.com/package/mongoose) versions `>=5.9.7 <9`
2121

2222
## Usage
2323

plugins/node/instrumentation-mongoose/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
"repository": "open-telemetry/opentelemetry-js-contrib",
88
"scripts": {
99
"docker:start": "docker run -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=127.0.0.1 -p 27017:27017 --rm mongo",
10-
"test": "ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/*.test.ts'",
10+
"test": "npm run test-v5-v6",
11+
"test-v5-v6": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/mongoose-common.test.ts' 'test/**/mongoose-v5-v6.test.ts'",
12+
"test-v7-v8": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/mongoose-common.test.ts' 'test/**/mongoose-v7-v8.test.ts'",
1113
"test-all-versions": "tav",
1214
"tdd": "npm run test -- --watch-extensions ts --watch",
1315
"clean": "rimraf build/*",
@@ -53,7 +55,7 @@
5355
"@types/node": "18.6.5",
5456
"expect": "29.2.0",
5557
"mocha": "7.2.0",
56-
"mongoose": "6.12.3",
58+
"mongoose": "6.13.0",
5759
"nyc": "15.1.0",
5860
"rimraf": "5.0.5",
5961
"test-all-versions": "6.1.0",

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

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,56 @@ import {
3434
SEMATTRS_DB_SYSTEM,
3535
} from '@opentelemetry/semantic-conventions';
3636

37-
const contextCaptureFunctions = [
38-
'remove',
37+
const contextCaptureFunctionsCommon = [
3938
'deleteOne',
4039
'deleteMany',
4140
'find',
4241
'findOne',
4342
'estimatedDocumentCount',
4443
'countDocuments',
45-
'count',
4644
'distinct',
4745
'where',
4846
'$where',
4947
'findOneAndUpdate',
5048
'findOneAndDelete',
5149
'findOneAndReplace',
50+
];
51+
52+
const contextCaptureFunctions6 = [
53+
'remove',
54+
'count',
55+
'findOneAndRemove',
56+
...contextCaptureFunctionsCommon,
57+
];
58+
const contextCaptureFunctions7 = [
59+
'count',
5260
'findOneAndRemove',
61+
...contextCaptureFunctionsCommon,
5362
];
63+
const contextCaptureFunctions8 = [...contextCaptureFunctionsCommon];
64+
65+
function getContextCaptureFunctions(
66+
moduleVersion: string | undefined
67+
): string[] {
68+
/* istanbul ignore next */
69+
if (!moduleVersion) {
70+
return contextCaptureFunctionsCommon;
71+
} else if (moduleVersion.startsWith('6.') || moduleVersion.startsWith('5.')) {
72+
return contextCaptureFunctions6;
73+
} else if (moduleVersion.startsWith('7.')) {
74+
return contextCaptureFunctions7;
75+
} else {
76+
return contextCaptureFunctions8;
77+
}
78+
}
79+
80+
function instrumentRemove(moduleVersion: string | undefined): boolean {
81+
return (
82+
(moduleVersion &&
83+
(moduleVersion.startsWith('5.') || moduleVersion.startsWith('6.'))) ||
84+
false
85+
);
86+
}
5487

5588
// when mongoose functions are called, we store the original call context
5689
// and then set it as the parent for the spans created by Query/Aggregate exec()
@@ -65,7 +98,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
6598
protected init(): InstrumentationModuleDefinition {
6699
const module = new InstrumentationNodeModuleDefinition(
67100
'mongoose',
68-
['>=5.9.7 <7'],
101+
['>=5.9.7 <9'],
69102
this.patch.bind(this),
70103
this.unpatch.bind(this)
71104
);
@@ -87,11 +120,14 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
87120
// so we need to apply the same logic after instrumenting the save function.
88121
moduleExports.Model.prototype.$save = moduleExports.Model.prototype.save;
89122

90-
this._wrap(
91-
moduleExports.Model.prototype,
92-
'remove',
93-
this.patchOnModelMethods('remove', moduleVersion)
94-
);
123+
if (instrumentRemove(moduleVersion)) {
124+
this._wrap(
125+
moduleExports.Model.prototype,
126+
'remove',
127+
this.patchOnModelMethods('remove', moduleVersion)
128+
);
129+
}
130+
95131
this._wrap(
96132
moduleExports.Query.prototype,
97133
'exec',
@@ -103,6 +139,8 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
103139
this.patchAggregateExec(moduleVersion)
104140
);
105141

142+
const contextCaptureFunctions = getContextCaptureFunctions(moduleVersion);
143+
106144
contextCaptureFunctions.forEach((funcName: string) => {
107145
this._wrap(
108146
moduleExports.Query.prototype,
@@ -115,11 +153,20 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
115153
return moduleExports;
116154
}
117155

118-
private unpatch(moduleExports: typeof mongoose): void {
156+
private unpatch(
157+
moduleExports: typeof mongoose,
158+
moduleVersion: string | undefined
159+
): void {
160+
const contextCaptureFunctions = getContextCaptureFunctions(moduleVersion);
161+
119162
this._unwrap(moduleExports.Model.prototype, 'save');
120163
// revert the patch for $save which we applied by aliasing it to patched `save`
121164
moduleExports.Model.prototype.$save = moduleExports.Model.prototype.save;
122-
this._unwrap(moduleExports.Model.prototype, 'remove');
165+
166+
if (instrumentRemove(moduleVersion)) {
167+
this._unwrap(moduleExports.Model.prototype, 'remove');
168+
}
169+
123170
this._unwrap(moduleExports.Query.prototype, 'exec');
124171
this._unwrap(moduleExports.Aggregate.prototype, 'exec');
125172

0 commit comments

Comments
 (0)