From 080186568926ed7056fcab26cac655e075d23195 Mon Sep 17 00:00:00 2001 From: vmarchaud Date: Mon, 18 Apr 2022 18:33:12 +0200 Subject: [PATCH] chore: add patch test --- .../package.json | 4 +- .../src/platform/node/instrumentation.ts | 19 ++++---- .../test/node/esmInstrumentation.test.mjs | 48 +++++++++++++++++++ .../test/node/node_modules/.gitkeep | 0 .../node_modules/my-esm-module/package.json | 6 +++ .../node_modules/my-esm-module/src/index.mjs | 6 +++ 6 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 experimental/packages/opentelemetry-instrumentation/test/node/esmInstrumentation.test.mjs create mode 100644 experimental/packages/opentelemetry-instrumentation/test/node/node_modules/.gitkeep create mode 100644 experimental/packages/opentelemetry-instrumentation/test/node/node_modules/my-esm-module/package.json create mode 100644 experimental/packages/opentelemetry-instrumentation/test/node/node_modules/my-esm-module/src/index.mjs diff --git a/experimental/packages/opentelemetry-instrumentation/package.json b/experimental/packages/opentelemetry-instrumentation/package.json index 6e615d36d03..d5fc3deb5e8 100644 --- a/experimental/packages/opentelemetry-instrumentation/package.json +++ b/experimental/packages/opentelemetry-instrumentation/package.json @@ -47,7 +47,9 @@ "tdd": "npm run tdd:node", "tdd:node": "npm run test -- --watch-extensions ts --watch", "tdd:browser": "karma start", - "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts' --exclude 'test/browser/**/*.ts'", + "test:cjs": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts' --exclude 'test/browser/**/*.ts'", + "test:esm": "nyc node --loader=import-in-the-middle/hook.mjs node_modules/.bin/_mocha test/node/*.test.mjs", + "test": "npm run test:cjs && npm run test:esm", "test:browser": "nyc karma start --single-run", "version": "node ../../../scripts/version-update.js", "watch": "tsc --build --watch tsconfig.all.json", diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index 27daadeb19c..881faa67c70 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -66,16 +66,14 @@ export abstract class InstrumentationBase } } - private _extractPackageVersion(baseDir: string): string | undefined { + private _extractPackage(baseDir: string): { name?: string, version?: string, main?: string } { try { // eslint-disable-next-line @typescript-eslint/no-var-requires - const version = require(path.join(baseDir, 'package.json')).version; - return typeof version === 'string' ? version : undefined; + return require(path.join(baseDir, 'package.json')); } catch (error) { diag.warn('Failed extracting version', baseDir); } - - return undefined; + return {}; } private _onRequireOrImport( @@ -92,12 +90,13 @@ export abstract class InstrumentationBase return exports; } - const version = this._extractPackageVersion(baseDir); - module.moduleVersion = version; - if (module.name === name) { + const pkg = this._extractPackage(baseDir); + module.moduleVersion = pkg.version; + // if the targeted module is an esm, the name will be the name of its entrypoint + if (module.name === name || (pkg.main && path.normalize(`${pkg.name}/${pkg.main}`) === name)) { // main module if ( - isSupported(module.supportedVersions, version, module.includePrerelease) + isSupported(module.supportedVersions, pkg.version, module.includePrerelease) ) { if (typeof module.patch === 'function') { module.moduleExports = exports; @@ -110,7 +109,7 @@ export abstract class InstrumentationBase // internal file const files = module.files ?? []; const file = files.find(f => f.name === name); - if (file && isSupported(file.supportedVersions, version, module.includePrerelease)) { + if (file && isSupported(file.supportedVersions, pkg.version, module.includePrerelease)) { file.moduleExports = exports; if (this._enabled) { return file.patch(exports, module.moduleVersion); diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/esmInstrumentation.test.mjs b/experimental/packages/opentelemetry-instrumentation/test/node/esmInstrumentation.test.mjs new file mode 100644 index 00000000000..84dca047835 --- /dev/null +++ b/experimental/packages/opentelemetry-instrumentation/test/node/esmInstrumentation.test.mjs @@ -0,0 +1,48 @@ +/* + * 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. + */ + +import * as assert from 'assert' +import * as sinon from 'sinon' +import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '../../build/src/index.js'; + +describe('when loading esm module', () => { + it('should patch module file', async () => { + class TestInstrumentation extends InstrumentationBase { + constructor(onPatch, onUnpatch) { + super('my-esm-instrumentation', '0.1.0'); + } + + init() { + return [ + new InstrumentationNodeModuleDefinition( + 'my-esm-module', + ['*'], + (exports, version) => { + exports.myConstant = 43; + exports.myFunction = () => 'another'; + } + ) + ]; + } + } + + const instrumentation = new TestInstrumentation(); + instrumentation.enable(); + const exported = await import('my-esm-module'); + assert.deepEqual(exported.myConstant, 43); + assert.deepEqual(exported.myFunction(), 'another'); + }); +}); diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/.gitkeep b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/my-esm-module/package.json b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/my-esm-module/package.json new file mode 100644 index 00000000000..653eb971124 --- /dev/null +++ b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/my-esm-module/package.json @@ -0,0 +1,6 @@ +{ + "name": "my-esm-module", + "version": "0.1.0", + "main": "./src/index.mjs", + "type": "module" +} \ No newline at end of file diff --git a/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/my-esm-module/src/index.mjs b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/my-esm-module/src/index.mjs new file mode 100644 index 00000000000..9e383818280 --- /dev/null +++ b/experimental/packages/opentelemetry-instrumentation/test/node/node_modules/my-esm-module/src/index.mjs @@ -0,0 +1,6 @@ + +export const myFunction = () => { + return 'test'; +}; + +export const myConstant = 42;