diff --git a/README.md b/README.md index 8d301c0..9b4c56d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ To load esm loader: ```js // esm-loader.mjs import { register } from 'node:module'; -const packages = new Set(['pkg1', 'pkg2']); const instrumentations = [ { channelName: 'channel1', @@ -31,7 +30,7 @@ const instrumentations = [ ] register('@apm-js-collab/tracing-hooks/hook.mjs', import.meta.url, { - data: { instrumentations, packages } + data: { instrumentations } }); ``` @@ -46,7 +45,6 @@ To load CJS patch: ```js // cjs-patch.js const ModulePatch = require('@apm-js-collab/tracing-hooks') -const packages = new Set(['pkg1', 'pkg2']); const instrumentations = [ { channelName: 'channel1', @@ -61,7 +59,7 @@ const instrumentations = [ channelName: 'channel2', module: { name: 'pkg2', verisonRange: '>=1.0.0', filePath: 'index.js' }, functionQuery: { - className: 'Class2, + className: 'Class2', methodName: 'method2', kind: 'Sync' } @@ -69,7 +67,7 @@ const instrumentations = [ ] -const modulePatch = new ModulePatch({ instrumentations, packages }); +const modulePatch = new ModulePatch({ instrumentations }); modulePatch.patch() ``` diff --git a/hook.mjs b/hook.mjs index 6195207..d10fa63 100644 --- a/hook.mjs +++ b/hook.mjs @@ -13,7 +13,7 @@ let instrumentator = null export async function initialize(data = {}) { const instrumentations = data?.instrumentations || [] instrumentator = create(instrumentations) - packages = data?.packages || new Set() + packages = new Set(instrumentations.map(i => i.module.name)) transformers = new Map() } diff --git a/index.js b/index.js index 5c862a7..e3b4cd7 100644 --- a/index.js +++ b/index.js @@ -6,8 +6,8 @@ const getPackageVersion = require('./lib/get-package-version') const debug = require('debug')('@apm-js-collab/tracing-hooks:module-patch') class ModulePatch { - constructor({ packages = new Set(), instrumentations = [] } = {}) { - this.packages = packages + constructor({ instrumentations = [] } = {}) { + this.packages = new Set(instrumentations.map(i => i.module.name)) this.instrumentator = create(instrumentations) this.transformers = new Map() this.resolve = Module._resolveFilename diff --git a/test/hook.test.mjs b/test/hook.test.mjs index 582bc77..c0a42fc 100644 --- a/test/hook.test.mjs +++ b/test/hook.test.mjs @@ -7,9 +7,7 @@ import Snap from '@matteo.collina/snap' test.beforeEach(async (t) => { const esmLoaderRewriter = await import('../hook.mjs') - const packages = new Set(['esm-pkg', 'pkg-1']) esmLoaderRewriter.initialize({ - packages, instrumentations: [ { channelName: 'unitTestEsm', @@ -152,9 +150,7 @@ test('should not rewrite code if it does not match a subscriber and a cjs module test('should not rewrite code if a function query does not exist in file', async (t) => { const { esmLoaderRewriter, snap } = t.ctx - const packages = new Set(['esm-pkg']) esmLoaderRewriter.initialize({ - packages, instrumentations: [ { channelName: 'unitTestEsm', diff --git a/test/index.test.js b/test/index.test.js index b1466da..aeab88f 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -10,7 +10,6 @@ const { readFileSync } = require('node:fs') test.beforeEach((t) => { const subscribers = { - packages: new Set(['pkg-1']), instrumentations: [ { channelName: 'unitTest', @@ -92,7 +91,6 @@ test('should not rewrite code for an unmatch patch', async (t) => { test('should not rewrite code if a function query does not exist in file', async (t) => { const { modulePath, snap } = t.ctx const subscribers = { - packages: new Set(['pkg-1']), instrumentations: [ { channelName: 'unitTest',