Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -31,7 +30,7 @@ const instrumentations = [
]

register('@apm-js-collab/tracing-hooks/hook.mjs', import.meta.url, {
data: { instrumentations, packages }
data: { instrumentations }
});
```

Expand All @@ -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',
Expand All @@ -61,15 +59,15 @@ const instrumentations = [
channelName: 'channel2',
module: { name: 'pkg2', verisonRange: '>=1.0.0', filePath: 'index.js' },
functionQuery: {
className: 'Class2,
className: 'Class2',
methodName: 'method2',
kind: 'Sync'
}
}
]


const modulePatch = new ModulePatch({ instrumentations, packages });
const modulePatch = new ModulePatch({ instrumentations });
modulePatch.patch()
```

Expand Down
2 changes: 1 addition & 1 deletion hook.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/hook.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 0 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const { readFileSync } = require('node:fs')

test.beforeEach((t) => {
const subscribers = {
packages: new Set(['pkg-1']),
instrumentations: [
{
channelName: 'unitTest',
Expand Down Expand Up @@ -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',
Expand Down
Loading