Skip to content

Commit

Permalink
fix: allow hapi plugin from array to be registered as argument (open-…
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu authored Oct 30, 2022
1 parent 55c1a98 commit 1db3b7e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class HapiInstrumentation extends InstrumentationBase {
if (Array.isArray(pluginInput)) {
for (const pluginObj of pluginInput) {
instrumentation._wrapRegisterHandler(
pluginObj.plugin?.plugin ?? pluginObj.plugin
pluginObj.plugin?.plugin ?? pluginObj.plugin ?? pluginObj
);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,64 @@ describe('Hapi Instrumentation - Hapi.Plugin Tests', () => {
);
});

it('should create spans for routes across multiple plugins declared in attribute level', async () => {
const rootSpan = tracer.startSpan('rootSpan');

await server.register([packagePlugin, simplePlugin]);
await server.start();
assert.strictEqual(memoryExporter.getFinishedSpans().length, 0);

await context.with(
trace.setSpan(context.active(), rootSpan),
async () => {
const res1 = await server.inject({
method: 'GET',
url: '/package',
});
assert.strictEqual(res1.statusCode, 200);
const res2 = await server.inject({
method: 'GET',
url: '/hello',
});
assert.strictEqual(res2.statusCode, 200);

rootSpan.end();

assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 3);

const firstHandlerSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'plugin-by-package: route - /package');
assert.notStrictEqual(firstHandlerSpan, undefined);
assert.strictEqual(
firstHandlerSpan?.attributes[AttributeNames.HAPI_TYPE],
HapiLayerType.PLUGIN
);
assert.strictEqual(
firstHandlerSpan?.attributes[AttributeNames.PLUGIN_NAME],
'plugin-by-package'
);
const secondHandlerSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'simplePlugin: route - /hello');
assert.notStrictEqual(secondHandlerSpan, undefined);
assert.strictEqual(
secondHandlerSpan?.attributes[AttributeNames.HAPI_TYPE],
HapiLayerType.PLUGIN
);
assert.strictEqual(
secondHandlerSpan?.attributes[AttributeNames.PLUGIN_NAME],
'simplePlugin'
);

const exportedRootSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'rootSpan');
assert.notStrictEqual(exportedRootSpan, undefined);
}
);
});

it('should instrument multiple versions of the same plugin just once', async () => {
const rootSpan = tracer.startSpan('rootSpan');

Expand Down

0 comments on commit 1db3b7e

Please sign in to comment.