Skip to content

Commit cc8a27d

Browse files
OlivierAlbertinimayurkale22
authored andcommitted
fix: pluginLoader for core modules (open-telemetry#312)
closes open-telemetry#311 Signed-off-by: Olivier Albertini <[email protected]>
1 parent 06e0929 commit cc8a27d

File tree

8 files changed

+66
-13
lines changed

8 files changed

+66
-13
lines changed

packages/opentelemetry-node-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"nyc": "^14.1.1",
4949
"shimmer": "^1.2.0",
5050
"tslint-microsoft-contrib": "^6.2.0",
51-
"tslint-consistent-codestyle":"^1.15.1",
51+
"tslint-consistent-codestyle": "^1.15.1",
5252
"ts-mocha": "^6.0.0",
5353
"ts-node": "^8.0.0",
5454
"typescript": "^3.4.5"

packages/opentelemetry-node-sdk/src/instrumentation/PluginLoader.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,22 @@ export class PluginLoader {
8181
// Enable the require hook.
8282
hook(modulesToHook, (exports, name, baseDir) => {
8383
if (this._hookState !== HookState.ENABLED) return exports;
84-
8584
const config = pluginsToLoad[name];
8685
const modulePath = config.path!;
87-
// Get the module version.
88-
const version = utils.getPackageVersion(this.logger, baseDir as string);
86+
let version = null;
87+
88+
if (!baseDir) {
89+
// basedir is the directory where the module is located,
90+
// or undefined for core modules.
91+
// lets plugins restrict what they support for core modules (see plugin.supportedVersions)
92+
version = process.versions.node;
93+
} else {
94+
// Get the module version.
95+
version = utils.getPackageVersion(this.logger, baseDir);
96+
}
97+
8998
this.logger.info(
90-
`PluginLoader#load: trying loading ${name}.${version}`
99+
`PluginLoader#load: trying loading ${name}@${version}`
91100
);
92101

93102
if (!version) return exports;

packages/opentelemetry-node-sdk/src/instrumentation/utils.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ import * as semver from 'semver';
2121
/**
2222
* Gets the package version.
2323
* @param logger The logger to use.
24-
* @param [basedir] The base directory.
24+
* @param basedir The base directory.
2525
*/
2626
export function getPackageVersion(
2727
logger: Logger,
28-
basedir?: string
28+
basedir: string
2929
): string | null {
30-
if (!basedir) return null;
31-
3230
const pjsonPath = path.join(basedir, 'package.json');
3331
try {
3432
const version = require(pjsonPath).version;

packages/opentelemetry-node-sdk/test/instrumentation/PluginLoader.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ const simplePlugins: Plugins = {
3535
},
3636
};
3737

38+
const httpPlugins: Plugins = {
39+
http: {
40+
enabled: true,
41+
path: '@opentelemetry/plugin-http-module',
42+
ignoreMethods: [],
43+
ignoreUrls: [],
44+
},
45+
};
46+
3847
const disablePlugins: Plugins = {
3948
'simple-module': {
4049
enabled: false,
@@ -139,6 +148,17 @@ describe('PluginLoader', () => {
139148
assert.strictEqual(simpleModule.name(), 'patched-simple-module');
140149
pluginLoader.unload();
141150
});
151+
152+
it('should load a plugin and patch the core module', () => {
153+
const pluginLoader = new PluginLoader(tracer, logger);
154+
assert.strictEqual(pluginLoader['_plugins'].length, 0);
155+
pluginLoader.load(httpPlugins);
156+
// The hook is only called the first time the module is loaded.
157+
const httpModule = require('http');
158+
assert.strictEqual(pluginLoader['_plugins'].length, 1);
159+
assert.strictEqual(httpModule.get(), 'patched');
160+
pluginLoader.unload();
161+
});
142162
// @TODO: simplify this test once we can load module with custom path
143163
it('should not load the plugin when supported versions does not match', () => {
144164
const pluginLoader = new PluginLoader(tracer, logger);

packages/opentelemetry-node-sdk/test/instrumentation/node_modules/@opentelemetry/plugin-http-module/http-module.js

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opentelemetry-node-sdk/test/instrumentation/node_modules/@opentelemetry/plugin-http-module/index.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opentelemetry-node-sdk/test/instrumentation/node_modules/@opentelemetry/plugin-http-module/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opentelemetry-node-sdk/test/instrumentation/utils.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ describe('Instrumentation#utils', () => {
4343
});
4444

4545
describe('getPackageVersion', () => {
46-
it('should handle when undefined basedir', () => {
47-
assert.strictEqual(utils.getPackageVersion(logger), null);
48-
});
49-
5046
TEST_MODULES.forEach(testCase => {
5147
it(`should return ${testCase.version} for ${testCase.name}`, () => {
5248
assert.strictEqual(

0 commit comments

Comments
 (0)