Skip to content

Commit fc0aaa1

Browse files
authored
fix: Updated regular expression for creating the tracing channel variable (#45)
1 parent 2069006 commit fc0aaa1

5 files changed

Lines changed: 18 additions & 10 deletions

File tree

lib/transforms.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ const tracingChannelPredicate = (node) => (
1414
node.declarations?.[0]?.id?.properties?.[0]?.value?.name === 'tr_ch_apm_tracingChannel'
1515
)
1616

17+
const CHANNEL_REGEX = /[^\w]/g
18+
/**
19+
* Formats the channel variable name by replacing any non-whitespace characters with `_`
20+
*
21+
* @param {string} channelName
22+
*/
23+
const formatChannelVariable = (channelName) => `tr_ch_apm$${channelName.replace(CHANNEL_REGEX, '_')}`
24+
1725
const transforms = module.exports = {
1826
/**
1927
* Injects a `tracingChannel` import/require into the program body if one is not
@@ -55,7 +63,7 @@ const transforms = module.exports = {
5563
*/
5664
tracingChannelDeclaration (state, node) {
5765
const { channelName, module: { name } } = state
58-
const channelVariable = 'tr_ch_apm$' + channelName.replaceAll(':', '_')
66+
const channelVariable = formatChannelVariable(channelName)
5967

6068
if (node.body.some(child => child.declarations?.[0]?.id?.name === channelVariable)) return
6169

@@ -313,7 +321,7 @@ function wrapSuper (_state, node) {
313321
*/
314322
function wrapCallback (state, node) {
315323
const { channelName, functionQuery: { callbackIndex = -1 } } = state
316-
const channelVariable = 'tr_ch_apm$' + channelName.replaceAll(':', '_')
324+
const channelVariable = formatChannelVariable(channelName)
317325

318326
return parse(`
319327
function wrapper () {
@@ -373,7 +381,7 @@ function wrapCallback (state, node) {
373381
*/
374382
function wrapPromise (state, node) {
375383
const { channelName } = state
376-
const channelVariable = 'tr_ch_apm$' + channelName.replaceAll(':', '_')
384+
const channelVariable = formatChannelVariable(channelName)
377385

378386
return parse(`
379387
function wrapper () {
@@ -426,7 +434,7 @@ function wrapPromise (state, node) {
426434
*/
427435
function wrapSync (state, node) {
428436
const { channelName } = state
429-
const channelVariable = 'tr_ch_apm$' + channelName.replaceAll(':', '_')
437+
const channelVariable = formatChannelVariable(channelName)
430438

431439
return parse(`
432440
function wrapper () {

tests/arguments_mutation/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const handler = {
2222
};
2323

2424
tracingChannel('orchestrion:undici:fetch_simple').subscribe(handler);
25-
tracingChannel('orchestrion:undici:fetch_complex').subscribe(handler);
25+
tracingChannel('orchestrion:undici:fetch.complex').subscribe(handler);
2626

2727
assert.strictEqual(fetch_simple.length, 2);
2828
assert.strictEqual(fetch_complex.length, 2);

tests/callback_cjs/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { fetch } = require('./instrumented.js');
22
const { assert, getContext } = require('../common/preamble.js');
3-
const context = getContext('orchestrion:undici:fetch_cb');
3+
const context = getContext('orchestrion:undici:fetch.cb');
44
(async () => {
55
const result = await new Promise((resolve, reject) => {
66
fetch('https://example.com', (err, val) => {

tests/decl_cjs/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
**/
55
const { fetch } = require('./instrumented.js');
66
const { assert, getContext } = require('../common/preamble.js');
7-
const context = getContext('orchestrion:undici:fetch_decl');
7+
const context = getContext('orchestrion:undici:fetch.decl');
88
(async () => {
99
const result = await fetch('https://example.com');
1010
assert.strictEqual(result, 42);

tests/tests.test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('arguments_mutation', () => {
5555
functionQuery: { functionName: 'fetch_simple', kind: 'Sync' },
5656
},
5757
{
58-
channelName: 'fetch_complex',
58+
channelName: 'fetch.complex',
5959
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
6060
functionQuery: { functionName: 'fetch_complex', kind: 'Sync' },
6161
},
@@ -115,7 +115,7 @@ describe('decl_cjs', () => {
115115
test('instruments async function declaration (cjs)', () => {
116116
runTest('decl_cjs', [
117117
{
118-
channelName: 'fetch_decl',
118+
channelName: 'fetch.decl',
119119
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
120120
functionQuery: { functionName: 'fetch', kind: 'Async' },
121121
},
@@ -264,7 +264,7 @@ describe('callback_cjs', () => {
264264
test('instruments callback-style function', () => {
265265
runTest('callback_cjs', [
266266
{
267-
channelName: 'fetch_cb',
267+
channelName: 'fetch.cb',
268268
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
269269
functionQuery: { functionName: 'fetch', kind: 'Callback' },
270270
},

0 commit comments

Comments
 (0)