Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(hapi): fix instrumentation of ESM-imported hapi
  • Loading branch information
aptomaKetil committed May 13, 2024
commit 78a5f9b54c50e90ab6f9896430de511bbde94961
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@hapi/hapi": "21.3.3",
"@opentelemetry/api": "^1.3.0",
"@opentelemetry/context-async-hooks": "^1.8.0",
"@opentelemetry/contrib-test-utils": "^0.39.0",
"@opentelemetry/sdk-trace-base": "^1.8.0",
"@opentelemetry/sdk-trace-node": "^1.8.0",
"@types/mocha": "7.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export class HapiInstrumentation extends InstrumentationBase {
return new InstrumentationNodeModuleDefinition(
HapiComponentName,
['>=17 <22'],
(moduleExports: typeof Hapi) => {
(module: any) => {
const moduleExports: typeof Hapi =
module[Symbol.toStringTag] === 'Module' ? module.default : module;
if (!isWrapped(moduleExports.server)) {
this._wrap(
moduleExports,
Expand All @@ -75,7 +77,9 @@ export class HapiInstrumentation extends InstrumentationBase {
}
return moduleExports;
},
(moduleExports: typeof Hapi) => {
(module: any) => {
const moduleExports: typeof Hapi =
module[Symbol.toStringTag] === 'Module' ? module.default : module;
this._massUnwrap([moduleExports], ['server', 'Server']);
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Use hapi from an ES module:
// node --experimental-loader=@opentelemetry/instrumentation/hook.mjs use-hapi.mjs

import { createTestNodeSdk } from '@opentelemetry/contrib-test-utils';

import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { HapiInstrumentation } from '../../build/src/index.js';

const sdk = createTestNodeSdk({
serviceName: 'use-hapi',
instrumentations: [
new HttpInstrumentation(),
new HapiInstrumentation()
]
})
sdk.start();

import Hapi from '@hapi/hapi';
import http from 'http';

// Start a Hapi server.
const server = new Hapi.Server({
port: 0,
host: 'localhost'
});

server.route({
method: 'GET',
path: '/route/{param}',
handler: function() {
return { hello: 'world' };
}
});

await server.start();

// Make a single request to it.
await new Promise(resolve => {
http.get(`http://${server.info.host}:${server.info.port}/route/test`, (res) => {
res.resume();
res.on('end', () => {
resolve();
});
})
});

await server.stop();
await sdk.shutdown();
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import {
InMemorySpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
import {
runTestFixture,
TestCollector,
} from '@opentelemetry/contrib-test-utils';
import { getPlugin } from './plugin';
const plugin = getPlugin();

Expand Down Expand Up @@ -537,4 +541,35 @@ describe('Hapi Instrumentation - Core Tests', () => {
);
});
});

describe('ESM', () => {
it('should work with ESM modules', async () => {
await runTestFixture({
cwd: __dirname,
argv: ['fixtures/use-hapi.mjs'],
env: {
NODE_OPTIONS:
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
NODE_NO_WARNINGS: '1',
},
checkResult: (err, stdout, stderr) => {
assert.ifError(err);
},
checkCollector: (collector: TestCollector) => {
const spans = collector.sortedSpans;
assert.strictEqual(spans.length, 2);
assert.strictEqual(spans[0].name, 'GET /route/{param}');
assert.strictEqual(
spans[0].instrumentationScope.name,
'@opentelemetry/instrumentation-http'
);
assert.strictEqual(spans[1].name, 'route - /route/{param}');
assert.strictEqual(
spans[1].instrumentationScope.name,
'@opentelemetry/instrumentation-hapi'
);
},
});
});
});
});