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