Skip to content

Commit f6ded2e

Browse files
authored
fix(instrumentation-hapi): fix this binding for plugin register method (#2625)
1 parent 3bab134 commit f6ded2e

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,13 @@ export class HapiInstrumentation extends InstrumentationBase {
267267
private _wrapRegisterHandler<T>(plugin: Hapi.Plugin<T>): void {
268268
const instrumentation: HapiInstrumentation = this;
269269
const pluginName = getPluginName(plugin);
270-
const oldHandler = plugin.register;
270+
const oldRegister = plugin.register;
271271
const self = this;
272-
const newRegisterHandler = function (server: Hapi.Server, options: T) {
272+
const newRegisterHandler = function (
273+
this: typeof plugin,
274+
server: Hapi.Server,
275+
options: T
276+
) {
273277
self._wrap(server, 'route', original => {
274278
return instrumentation._getServerRoutePatch.bind(instrumentation)(
275279
original,
@@ -287,7 +291,7 @@ export class HapiInstrumentation extends InstrumentationBase {
287291
pluginName
288292
);
289293
});
290-
return oldHandler(server, options);
294+
return oldRegister.call(this, server, options);
291295
};
292296
plugin.register = newRegisterHandler;
293297
}

plugins/node/opentelemetry-instrumentation-hapi/test/hapi-plugin.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => {
8181
name: 'simplePlugin',
8282
version: '1.0.0',
8383
multiple: true,
84+
value: 42,
8485
register: async function (server: hapi.Server, options: any) {
8586
server.route({
8687
method: 'GET',
8788
path: '/hello',
88-
handler: function (request, h) {
89-
return `hello, world, ${options.name}`;
89+
handler: (request, h) => {
90+
return `hello, world, ${this.value} ${options.name}`;
9091
},
9192
});
9293
},

0 commit comments

Comments
 (0)