Skip to content

Commit e864993

Browse files
authored
fix: Fixed tracePromise to handle returning result when function being wrapped is not a promise (#46)
1 parent fc0aaa1 commit e864993

7 files changed

Lines changed: 35 additions & 5 deletions

File tree

lib/transforms.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,8 @@ function wrapPromise (state, node) {
391391
try {
392392
let promise = __apm$traced();
393393
if (typeof promise?.then !== 'function') {
394-
__apm$ctx.result = result;
395-
return result;
394+
__apm$ctx.result = promise;
395+
return promise;
396396
}
397397
return promise.then(
398398
result => {

tests/callback_cjs/mod.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function fetch(url, callback) {
2-
callback(null, 42);
2+
process.nextTick(() => {
3+
callback(null, 42);
4+
})
35
}
46

57
module.exports = { fetch };

tests/common/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function getContext (channelName) {
1313
context.start = true;
1414
},
1515
end(message) {
16-
message.context.end = true;
16+
message.context.end = message.result ?? true
1717
// Handle end message
1818
},
1919
asyncStart(message) {

tests/nested_functions/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ const context = getContext('orchestrion:undici:nested_fn');
1111
assert.strictEqual(result, 'Hook added');
1212
assert.deepStrictEqual(context, {
1313
start: true,
14-
end: true,
14+
end: 'Hook added'
1515
});
1616
})();

tests/tests.test.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,15 @@ describe('source_map', () => {
504504
expect(original.column).toBe(originalReturnColumn)
505505
})
506506
})
507+
508+
describe('wrap_promise_non_promise', () => {
509+
test('instruments sync function with wrapPromise and properly returns the result via context', () => {
510+
runTest('wrap_promise_non_promise', [
511+
{
512+
channelName: 'fetch_nonpromise',
513+
module: { name: TEST_MODULE_NAME, versionRange: '>=0.0.1', filePath: TEST_MODULE_PATH },
514+
functionQuery: { functionName: 'fetch', kind: 'Async' }
515+
}
516+
])
517+
})
518+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function fetch (url) {
2+
return 42;
3+
}
4+
5+
module.exports = { fetch };
6+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { fetch } = require('./instrumented.js');
2+
const { assert, getContext } = require('../common/preamble.js');
3+
const context = getContext('orchestrion:undici:fetch_nonpromise');
4+
const result = fetch('https://example.com');
5+
assert.equal(result, 42)
6+
assert.deepStrictEqual(context, {
7+
start: true,
8+
end: 42
9+
})
10+

0 commit comments

Comments
 (0)