diff --git a/plugins/node/opentelemetry-instrumentation-hapi/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-hapi/src/instrumentation.ts index 91bda4397e..b3e619b3b8 100644 --- a/plugins/node/opentelemetry-instrumentation-hapi/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-hapi/src/instrumentation.ts @@ -267,9 +267,13 @@ export class HapiInstrumentation extends InstrumentationBase { private _wrapRegisterHandler(plugin: Hapi.Plugin): void { const instrumentation: HapiInstrumentation = this; const pluginName = getPluginName(plugin); - const oldHandler = plugin.register; + const oldRegister = plugin.register; const self = this; - const newRegisterHandler = function (server: Hapi.Server, options: T) { + const newRegisterHandler = function ( + this: typeof plugin, + server: Hapi.Server, + options: T + ) { self._wrap(server, 'route', original => { return instrumentation._getServerRoutePatch.bind(instrumentation)( original, @@ -287,7 +291,7 @@ export class HapiInstrumentation extends InstrumentationBase { pluginName ); }); - return oldHandler(server, options); + return oldRegister.call(this, server, options); }; plugin.register = newRegisterHandler; } diff --git a/plugins/node/opentelemetry-instrumentation-hapi/test/hapi-plugin.test.ts b/plugins/node/opentelemetry-instrumentation-hapi/test/hapi-plugin.test.ts index b2737b8b1d..a9cbe7dd26 100644 --- a/plugins/node/opentelemetry-instrumentation-hapi/test/hapi-plugin.test.ts +++ b/plugins/node/opentelemetry-instrumentation-hapi/test/hapi-plugin.test.ts @@ -81,12 +81,13 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => { name: 'simplePlugin', version: '1.0.0', multiple: true, + value: 42, register: async function (server: hapi.Server, options: any) { server.route({ method: 'GET', path: '/hello', - handler: function (request, h) { - return `hello, world, ${options.name}`; + handler: (request, h) => { + return `hello, world, ${this.value} ${options.name}`; }, }); },