diff --git a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts index f2f51e6885..4ef2e9b87a 100644 --- a/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts @@ -75,7 +75,12 @@ export class FastifyInstrumentation extends InstrumentationBase { ['^3.0.0', '^4.0.0'], (moduleExports, moduleVersion) => { this._diag.debug(`Applying patch for fastify@${moduleVersion}`); - return this._patchConstructor(moduleExports); + const wrappedFastify = this._wrapFastify(); + + this._wrap(moduleExports, 'fastify', wrappedFastify); + this._wrap(moduleExports, 'default', wrappedFastify); + + return moduleExports; } ), ]; @@ -197,25 +202,22 @@ export class FastifyInstrumentation extends InstrumentationBase { }; } - private _patchConstructor( - moduleExports: { fastify: () => FastifyInstance }, - ): () => FastifyInstance { + private _wrapFastify(): ( + original: () => FastifyInstance + ) => () => FastifyInstance { const instrumentation = this; - this._diag.debug('Patching fastify constructor function'); - function fastify(this: FastifyInstance, ...args: any) { - const app: FastifyInstance = moduleExports.fastify.apply(this, args); - app.addHook('onRequest', instrumentation._hookOnRequest()); - app.addHook('preHandler', instrumentation._hookPreHandler()); + return function (original): () => FastifyInstance { + return function wrappedFastify(this: FastifyInstance, ...args: any) { + const app: FastifyInstance = original.apply(this, args); + app.addHook('onRequest', instrumentation._hookOnRequest()); + app.addHook('preHandler', instrumentation._hookPreHandler()); - instrumentation._wrap(app, 'addHook', instrumentation._wrapAddHook()); + instrumentation._wrap(app, 'addHook', instrumentation._wrapAddHook()); - return app; - } - - fastify.fastify = fastify; - fastify.default = fastify; - return fastify; + return app; + }; + }; } private _patchSend() {