node icon indicating copy to clipboard operation
node copied to clipboard

lib: add util.getCallSite() API

Open RafaelGSS opened this issue 1 year ago • 17 comments

This pull request introduces a new API method, util.getCallSite(), which provides a way to capture and inspect the call stack within a Node.js application. The method returns an array of CallSite objects, so developers can retrieve information about each function call in the stack trace.

Previously, they could reach the same behaviour by using Error.prepareStackTrace but, this API isn't that friendly IMO.

RafaelGSS avatar Aug 14 '24 17:08 RafaelGSS

Codecov Report

Attention: Patch coverage is 95.91837% with 2 lines in your changes missing coverage. Please review.

Project coverage is 87.60%. Comparing base (298dea0) to head (aa11f56). Report is 250 commits behind head on main.

Files with missing lines Patch % Lines
src/node_util.cc 94.59% 0 Missing and 2 partials :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #54380      +/-   ##
==========================================
- Coverage   87.61%   87.60%   -0.01%     
==========================================
  Files         650      650              
  Lines      182829   182878      +49     
  Branches    35382    35390       +8     
==========================================
+ Hits       160179   160210      +31     
  Misses      15931    15931              
- Partials     6719     6737      +18     
Files with missing lines Coverage Δ
lib/util.js 95.91% <100.00%> (+0.12%) :arrow_up:
src/node_util.cc 85.77% <94.59%> (+1.61%) :arrow_up:

... and 29 files with indirect coverage changes

codecov[bot] avatar Aug 15 '24 01:08 codecov[bot]

I'd not include an option to ignore it in this initial version - maybe a follow up pr once it's landed?

It's not supposed to be printed via console.log because it's a CallSite representation. I will modify the API so it retrieves the stack trace from C++, and this should address this issue with the new object

RafaelGSS avatar Aug 17 '24 13:08 RafaelGSS

C++ version will be landed on this PR. I didn't have time yet.

RafaelGSS avatar Aug 18 '24 03:08 RafaelGSS

Is the new API going to work for events like unhandledRejection?

mugli avatar Aug 19 '24 16:08 mugli

CI: https://ci.nodejs.org/job/node-test-pull-request/61255/

nodejs-github-bot avatar Aug 20 '24 02:08 nodejs-github-bot

lgtm

Did you do a benchmark?

I did, but without having a fair comparison I don't know if it will mean much:

From my local machine

util/get-callsite.js n=1000000: 178,641.08162235853

I suspect the C++ implementation will be slower than the JS one for this particular scenario. We need to iterate over v8::StackTrace to create objects while the JS approach does not compute the values unless the function is called. e.g: .getFileName(). So the comparison will be unfair.

But, remember that we've switched to C++ for a side-effect-less approach. I can optimize this code later.

RafaelGSS avatar Aug 20 '24 13:08 RafaelGSS

CI: https://ci.nodejs.org/job/node-test-pull-request/61288/

nodejs-github-bot avatar Aug 20 '24 16:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61297/

nodejs-github-bot avatar Aug 20 '24 20:08 nodejs-github-bot

@mcollina I have just included a benchmark for the previous approach (using Error) and as I said it will be unfair as one computes fields and the Error approach does not (return CallSite{}), so to make it more "fair" I've created a third benchmark that uses the Error approach but compute the fields. (See 8bc130c)

Results:

util/get-callsite.js method="ErrorCallSite" n=1000000: 303,889.4093489688
util/get-callsite.js method="ErrorCallSiteSerialized" n=1000000: 190,435.9778337435
util/get-callsite.js method="CPP" n=1000000: 175,429.33099106076

RafaelGSS avatar Aug 21 '24 15:08 RafaelGSS

CI: https://ci.nodejs.org/job/node-test-pull-request/61327/

nodejs-github-bot avatar Aug 21 '24 18:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61331/

nodejs-github-bot avatar Aug 21 '24 20:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61355/

nodejs-github-bot avatar Aug 22 '24 18:08 nodejs-github-bot

Commit Queue failed
- Loading data for nodejs/node/pull/54380
✔  Done loading data for nodejs/node/pull/54380
----------------------------------- PR info ------------------------------------
Title      lib: add util.getCallSite() API (#54380)
Author     Rafael Gonzaga <[email protected]> (@RafaelGSS)
Branch     RafaelGSS:add-getCallSite -> nodejs:main
Labels     util, semver-minor, author ready, needs-ci, commit-queue-rebase
Commits    6
 - lib: add util.getCallSite() API
 - benchmark: add util.getCallSite bench
 - fixup! lib: add util.getCallSite() API
 - fixup! fixup! lib: add util.getCallSite() API
 - fixup! fixup! fixup! lib: add util.getCallSite() API
 - fixup! fixup! fixup! fixup! lib: add util.getCallSite() API
Committers 1
 - RafaelGSS <[email protected]>
PR-URL: https://github.com/nodejs/node/pull/54380
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/54380
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
--------------------------------------------------------------------------------
   ⚠  Commits were pushed since the last approving review:
   ⚠  - fixup! fixup! fixup! lib: add util.getCallSite() API
   ⚠  - fixup! fixup! fixup! fixup! lib: add util.getCallSite() API
   ℹ  This PR was created on Wed, 14 Aug 2024 17:42:46 GMT
   ✔  Approvals: 3
   ✔  - Vinícius Lourenço Claro Cardoso (@H4ad): https://github.com/nodejs/node/pull/54380#pullrequestreview-2248638869
   ✔  - Yagiz Nizipli (@anonrig) (TSC): https://github.com/nodejs/node/pull/54380#pullrequestreview-2246871973
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/54380#pullrequestreview-2247058195
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2024-08-22T18:07:19Z: https://ci.nodejs.org/job/node-test-pull-request/61355/
- Querying data for job/node-test-pull-request/61355/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/10531270448

nodejs-github-bot avatar Aug 23 '24 19:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61498/

nodejs-github-bot avatar Aug 26 '24 14:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61507/

nodejs-github-bot avatar Aug 26 '24 17:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61517/

nodejs-github-bot avatar Aug 27 '24 00:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61564/

nodejs-github-bot avatar Aug 27 '24 16:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61602/

nodejs-github-bot avatar Aug 28 '24 19:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61618/

nodejs-github-bot avatar Aug 28 '24 23:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61671/

nodejs-github-bot avatar Aug 29 '24 19:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61701/

nodejs-github-bot avatar Aug 30 '24 12:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61710/

nodejs-github-bot avatar Aug 30 '24 14:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61754/

nodejs-github-bot avatar Aug 31 '24 16:08 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61818/

nodejs-github-bot avatar Sep 02 '24 13:09 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61834/

nodejs-github-bot avatar Sep 02 '24 16:09 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61837/

nodejs-github-bot avatar Sep 03 '24 03:09 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61852/

nodejs-github-bot avatar Sep 03 '24 12:09 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61855/

nodejs-github-bot avatar Sep 03 '24 14:09 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61867/

nodejs-github-bot avatar Sep 03 '24 16:09 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/61883/

nodejs-github-bot avatar Sep 03 '24 20:09 nodejs-github-bot