Skip to content

Commit 94f564c

Browse files
committed
Use different InstrumentationNodeModuleDefinition for each version
1 parent 3aa1c9e commit 94f564c

File tree

1 file changed

+51
-35
lines changed

1 file changed

+51
-35
lines changed

plugins/node/opentelemetry-instrumentation-express/src/instrumentation.ts

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -59,49 +59,65 @@ export class ExpressInstrumentation extends InstrumentationBase<ExpressInstrumen
5959
return [
6060
new InstrumentationNodeModuleDefinition(
6161
'express',
62-
['>=4.0.0 <6'],
62+
['>=4.0.0 <5'],
6363
moduleExports => {
64-
const isExpressV5 = 'Route' in moduleExports.Router;
65-
const routerProto = isExpressV5
66-
? moduleExports.Router.prototype
67-
: moduleExports.Router;
68-
// patch express.Router.route
69-
if (isWrapped(routerProto.route)) {
70-
this._unwrap(routerProto, 'route');
71-
}
72-
this._wrap(routerProto, 'route', this._getRoutePatch());
73-
// patch express.Router.use
74-
if (isWrapped(routerProto.use)) {
75-
this._unwrap(routerProto, 'use');
76-
}
77-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
78-
this._wrap(routerProto, 'use', this._getRouterUsePatch() as any);
79-
// patch express.Application.use
80-
if (isWrapped(moduleExports.application.use)) {
81-
this._unwrap(moduleExports.application, 'use');
82-
}
83-
this._wrap(
84-
moduleExports.application,
85-
'use',
86-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87-
this._getAppUsePatch(isExpressV5) as any
88-
);
89-
return moduleExports;
64+
this._setup(moduleExports, false);
65+
},
66+
moduleExports => {
67+
this._tearDown(moduleExports, false);
68+
}
69+
),
70+
new InstrumentationNodeModuleDefinition(
71+
'express',
72+
['>=5 <6'],
73+
moduleExports => {
74+
this._setup(moduleExports, true);
9075
},
9176
moduleExports => {
92-
if (moduleExports === undefined) return;
93-
const isExpressV5 = 'Route' in moduleExports.Router;
94-
const routerProto = isExpressV5
95-
? moduleExports.Router.prototype
96-
: moduleExports.Router;
97-
this._unwrap(routerProto, 'route');
98-
this._unwrap(routerProto, 'use');
99-
this._unwrap(moduleExports.application, 'use');
77+
this._tearDown(moduleExports, true);
10078
}
10179
),
10280
];
10381
}
10482

83+
private _setup(moduleExports: any, isExpressV5: boolean) {
84+
const routerProto = isExpressV5
85+
? moduleExports.Router.prototype
86+
: moduleExports.Router;
87+
// patch express.Router.route
88+
if (isWrapped(routerProto.route)) {
89+
this._unwrap(routerProto, 'route');
90+
}
91+
this._wrap(routerProto, 'route', this._getRoutePatch());
92+
// patch express.Router.use
93+
if (isWrapped(routerProto.use)) {
94+
this._unwrap(routerProto, 'use');
95+
}
96+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
97+
this._wrap(routerProto, 'use', this._getRouterUsePatch() as any);
98+
// patch express.Application.use
99+
if (isWrapped(moduleExports.application.use)) {
100+
this._unwrap(moduleExports.application, 'use');
101+
}
102+
this._wrap(
103+
moduleExports.application,
104+
'use',
105+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
106+
this._getAppUsePatch(isExpressV5) as any
107+
);
108+
return moduleExports;
109+
}
110+
111+
private _tearDown(moduleExports: any, isExpressV5: boolean) {
112+
if (moduleExports === undefined) return;
113+
const routerProto = isExpressV5
114+
? moduleExports.Router.prototype
115+
: moduleExports.Router;
116+
this._unwrap(routerProto, 'route');
117+
this._unwrap(routerProto, 'use');
118+
this._unwrap(moduleExports.application, 'use');
119+
}
120+
105121
/**
106122
* Get the patch for Router.route function
107123
*/

0 commit comments

Comments
 (0)