Skip to content

Commit

Permalink
ref: Streamline fastify wrapping
Browse files Browse the repository at this point in the history
Align this with how other instrumentations wrap.
  • Loading branch information
mydea committed Aug 10, 2023
1 parent d052a1b commit cbffbc7
Showing 1 changed file with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
),
];
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit cbffbc7

Please sign in to comment.