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: 4 additions & 4 deletions lib/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ function traceAny (state, node, _parent, ancestry) {
function traceFunction (state, node, program) {
transforms.tracingChannelDeclaration(state, program)

const { functionQuery: { methodName, privateMethodName, functionName, expressionName } } = state
const { functionQuery: { methodName, privateMethodName, functionName, expressionName, propertyName } } = state
const isConstructor = methodName === 'constructor' ||
(!methodName && !privateMethodName && !functionName && !expressionName)
const type = isConstructor ? 'ArrowFunctionExpression' : 'FunctionExpression'
(!methodName && !privateMethodName && !functionName && !expressionName && !propertyName)
const type = isConstructor ? 'ArrowFunctionExpression' : node.type
const params = node.params

node.body = wrap(state, {
Expand Down Expand Up @@ -268,7 +268,7 @@ function wrap (state, node, program) {
};
`
: `
const __apm$arguments = [${args}];
const __apm$arguments = [${args}].slice(0, arguments.length);
const __apm$ctx = {
arguments: __apm$arguments,
self: this,
Expand Down
20 changes: 20 additions & 0 deletions tests/arrow_arguments_cjs/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

// Two methods assigned to `this` inside a constructor that takes 2 arguments.
// The arrow function doesn't bind its own `arguments`, so `arguments.length`
// inside it refers to the constructor's argument count (2), not the call-site
// count (1). The regular function binds its own `arguments`, so it sees the
// call-site count (1). Instrumentation must not change either binding.
Copy link
Copy Markdown
Contributor

@jsumners-nr jsumners-nr Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the count 3?

I see that the 1 is from the test.js usage of the methods. Definitely confusing without knowing what the test file looks like.

// Both functions declare extra parameters to ensure parameter count does not
// affect the reported arguments length.
function Connection (host, port) {
this.fetchArrow = async (sql, extra1, extra2) => {
return arguments.length
}

this.fetchFunction = async function (sql, extra1, extra2) {
return arguments.length
}
}

module.exports = { Connection }
22 changes: 22 additions & 0 deletions tests/arrow_arguments_cjs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const { Connection } = require('./instrumented.js')
const { assert, getContext } = require('../common/preamble.js')

const contextArrow = getContext('orchestrion:undici:Connection_fetchArrow')
const contextFn = getContext('orchestrion:undici:Connection_fetchFunction')

;(async () => {
const conn = new Connection('host', 'port')

// Arrow: arguments.length must still be 2 (constructor args), not 1 (call-site).
const arrowResult = await conn.fetchArrow('SELECT 1')
assert.strictEqual(arrowResult, 2)

// Regular function: arguments.length must be 1 (call-site args).
const fnResult = await conn.fetchFunction('SELECT 1')
assert.strictEqual(fnResult, 1)

assert.deepStrictEqual(contextArrow, { start: true, end: true, asyncStart: 2, asyncEnd: 2 })
assert.deepStrictEqual(contextFn, { start: true, end: true, asyncStart: 1, asyncEnd: 1 })
})()
11 changes: 11 additions & 0 deletions tests/object_property_this_generator_cjs/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

function Connection () {
this.generate = function * () {
yield 1
yield 2
yield 3
}
}

module.exports = { Connection }
16 changes: 16 additions & 0 deletions tests/object_property_this_generator_cjs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const { Connection } = require('./instrumented.js')
const { assert, getContext } = require('../common/preamble.js')
const context = getContext('orchestrion:undici:Connection_generate')

const conn = new Connection()
const iter = conn.generate()

const results = []
for (const val of iter) {
results.push(val)
}

assert.strictEqual(context.start, true)
assert.deepStrictEqual(results, [1, 2, 3])
29 changes: 29 additions & 0 deletions tests/tests.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,18 @@ describe('object_property_this_cjs', () => {
})
})

describe('object_property_this_generator_cjs', () => {
test('instruments generator function assigned to this inside a function constructor', () => {
runTest('object_property_this_generator_cjs', [
{
channelName: 'Connection_generate',
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
functionQuery: { objectName: 'this', propertyName: 'generate', kind: 'Sync' },
},
])
})
})

describe('object_property_named_cjs', () => {
test('instruments async arrow function assigned to a named identifier property', () => {
runTest('object_property_named_cjs', [
Expand Down Expand Up @@ -613,3 +625,20 @@ describe('arrow_this_binding_cjs', () => {
])
})
})

describe('arrow_arguments_cjs', () => {
test('arrow keeps outer arguments binding while regular function sees call-site arguments', () => {
runTest('arrow_arguments_cjs', [
{
channelName: 'Connection_fetchArrow',
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
functionQuery: { objectName: 'this', propertyName: 'fetchArrow', kind: 'Async' },
},
{
channelName: 'Connection_fetchFunction',
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
functionQuery: { objectName: 'this', propertyName: 'fetchFunction', kind: 'Async' },
},
])
})
})
Loading