Skip to content

Commit 4ea5878

Browse files
authored
feat: Derive packages argument from instrumentations (#5)
1 parent 43a1847 commit 4ea5878

File tree

5 files changed

+6
-14
lines changed

5 files changed

+6
-14
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ To load esm loader:
88
```js
99
// esm-loader.mjs
1010
import { register } from 'node:module';
11-
const packages = new Set(['pkg1', 'pkg2']);
1211
const instrumentations = [
1312
{
1413
channelName: 'channel1',
@@ -31,7 +30,7 @@ const instrumentations = [
3130
]
3231
3332
register('@apm-js-collab/tracing-hooks/hook.mjs', import.meta.url, {
34-
data: { instrumentations, packages }
33+
data: { instrumentations }
3534
});
3635
```
3736
@@ -46,7 +45,6 @@ To load CJS patch:
4645
```js
4746
// cjs-patch.js
4847
const ModulePatch = require('@apm-js-collab/tracing-hooks')
49-
const packages = new Set(['pkg1', 'pkg2']);
5048
const instrumentations = [
5149
{
5250
channelName: 'channel1',
@@ -61,15 +59,15 @@ const instrumentations = [
6159
channelName: 'channel2',
6260
module: { name: 'pkg2', verisonRange: '>=1.0.0', filePath: 'index.js' },
6361
functionQuery: {
64-
className: 'Class2,
62+
className: 'Class2',
6563
methodName: 'method2',
6664
kind: 'Sync'
6765
}
6866
}
6967
]
7068
7169
72-
const modulePatch = new ModulePatch({ instrumentations, packages });
70+
const modulePatch = new ModulePatch({ instrumentations });
7371
modulePatch.patch()
7472
```
7573

hook.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let instrumentator = null
1313
export async function initialize(data = {}) {
1414
const instrumentations = data?.instrumentations || []
1515
instrumentator = create(instrumentations)
16-
packages = data?.packages || new Set()
16+
packages = new Set(instrumentations.map(i => i.module.name))
1717
transformers = new Map()
1818
}
1919

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const getPackageVersion = require('./lib/get-package-version')
66
const debug = require('debug')('@apm-js-collab/tracing-hooks:module-patch')
77

88
class ModulePatch {
9-
constructor({ packages = new Set(), instrumentations = [] } = {}) {
10-
this.packages = packages
9+
constructor({ instrumentations = [] } = {}) {
10+
this.packages = new Set(instrumentations.map(i => i.module.name))
1111
this.instrumentator = create(instrumentations)
1212
this.transformers = new Map()
1313
this.resolve = Module._resolveFilename

test/hook.test.mjs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import Snap from '@matteo.collina/snap'
77

88
test.beforeEach(async (t) => {
99
const esmLoaderRewriter = await import('../hook.mjs')
10-
const packages = new Set(['esm-pkg', 'pkg-1'])
1110
esmLoaderRewriter.initialize({
12-
packages,
1311
instrumentations: [
1412
{
1513
channelName: 'unitTestEsm',
@@ -152,9 +150,7 @@ test('should not rewrite code if it does not match a subscriber and a cjs module
152150

153151
test('should not rewrite code if a function query does not exist in file', async (t) => {
154152
const { esmLoaderRewriter, snap } = t.ctx
155-
const packages = new Set(['esm-pkg'])
156153
esmLoaderRewriter.initialize({
157-
packages,
158154
instrumentations: [
159155
{
160156
channelName: 'unitTestEsm',

test/index.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const { readFileSync } = require('node:fs')
1010

1111
test.beforeEach((t) => {
1212
const subscribers = {
13-
packages: new Set(['pkg-1']),
1413
instrumentations: [
1514
{
1615
channelName: 'unitTest',
@@ -92,7 +91,6 @@ test('should not rewrite code for an unmatch patch', async (t) => {
9291
test('should not rewrite code if a function query does not exist in file', async (t) => {
9392
const { modulePath, snap } = t.ctx
9493
const subscribers = {
95-
packages: new Set(['pkg-1']),
9694
instrumentations: [
9795
{
9896
channelName: 'unitTest',

0 commit comments

Comments
 (0)