Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add util.getCallSite() API #54380

Merged
merged 9 commits into from
Sep 4, 2024

Conversation

RafaelGSS
Copy link
Member

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 RafaelGSS added the semver-minor PRs that contain new features and should be released in the next minor version. label Aug 14, 2024
@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Aug 14, 2024
lib/util.js Outdated Show resolved Hide resolved
Copy link

codecov bot commented Aug 15, 2024

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 ⚠️
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%) ⬆️
src/node_util.cc 85.77% <94.59%> (+1.61%) ⬆️

... and 29 files with indirect coverage changes

Copy link
Member

@H4ad H4ad left a comment

Choose a reason for hiding this comment

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

Should we add an option to ignore node:internal?

Also, I think we should pretty print the callsite object, if we console.log it:

[
  CallSite {},
  CallSite {},
  CallSite {},
  CallSite {},
  CallSite {},
  CallSite {},
  CallSite {},
  CallSite {}
]

If we JSON.stringify:

[{},{},{},{},{},{},{},{}]

And maybe we should add some snapshot tests, I know they could break if we change/add the methods that call them but usually they are great to track regressions, I know we have a couple snapshot tests like that in our codebase.

In general, looks great to me to have a utility function like this.

@RafaelGSS
Copy link
Member Author

RafaelGSS commented Aug 17, 2024

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

Copy link
Member

@H4ad H4ad left a comment

Choose a reason for hiding this comment

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

I'm fine landing it with this js version, just include a benchmark and then we can evaluate if the new c++ version will be faster.

Also, having some snapshot tests will be good to see what will change when we land the c++ version.

@RafaelGSS RafaelGSS added the blocked PRs that are blocked by other issues or PRs. label Aug 18, 2024
@RafaelGSS
Copy link
Member Author

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

@RafaelGSS RafaelGSS added wip Issues and PRs that are still a work in progress. and removed blocked PRs that are blocked by other issues or PRs. labels Aug 18, 2024
@mugli
Copy link

mugli commented Aug 19, 2024

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

@RafaelGSS RafaelGSS removed the wip Issues and PRs that are still a work in progress. label Aug 20, 2024
@RafaelGSS RafaelGSS added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. labels Aug 20, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 20, 2024
@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

Did you do a benchmark?

src/node_util.cc Outdated Show resolved Hide resolved
src/node_util.cc Outdated Show resolved Hide resolved
@RafaelGSS
Copy link
Member Author

RafaelGSS commented Aug 20, 2024

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 RafaelGSS added the commit-queue-rebase Add this label to allow the Commit Queue to land a PR in several commits. label Aug 20, 2024
@RafaelGSS RafaelGSS added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 20, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 20, 2024
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@RafaelGSS RafaelGSS added the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 4, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 4, 2024
@nodejs-github-bot nodejs-github-bot merged commit 80a989f into nodejs:main Sep 4, 2024
60 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 80a989f

aduh95 pushed a commit that referenced this pull request Sep 12, 2024
PR-URL: #54380
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Claudio Wunder <[email protected]>
Reviewed-By: James M Snell <[email protected]>
RafaelGSS added a commit that referenced this pull request Sep 16, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: TODO
@RafaelGSS RafaelGSS mentioned this pull request Sep 16, 2024
RafaelGSS added a commit that referenced this pull request Sep 16, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 16, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 16, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 16, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 16, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 16, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 17, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 17, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 17, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
RafaelGSS added a commit that referenced this pull request Sep 17, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) #54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) #54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) #54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) #54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) #54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) #54384

PR-URL: #54966
@targos targos added the dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. label Sep 22, 2024
Bratan613

This comment was marked as spam.

@BecStem

This comment was marked as off-topic.

louwers pushed a commit to louwers/node that referenced this pull request Nov 2, 2024
PR-URL: nodejs#54380
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Claudio Wunder <[email protected]>
Reviewed-By: James M Snell <[email protected]>
louwers pushed a commit to louwers/node that referenced this pull request Nov 2, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) nodejs#54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) nodejs#54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) nodejs#54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) nodejs#54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) nodejs#54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) nodejs#54384

PR-URL: nodejs#54966
tpoisseau pushed a commit to tpoisseau/node that referenced this pull request Nov 21, 2024
PR-URL: nodejs#54380
Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Claudio Wunder <[email protected]>
Reviewed-By: James M Snell <[email protected]>
tpoisseau pushed a commit to tpoisseau/node that referenced this pull request Nov 21, 2024
Notable changes:

lib:
  * (SEMVER-MINOR) add util.getCallSite() API (Rafael Gonzaga) nodejs#54380
repl:
  * doc-deprecate instantiating `node:repl` classes without `new` (Aviv Keller) nodejs#54842
src:
  * create handle scope in FastInternalModuleStat (Joyee Cheung) nodejs#54384
stream:
  * (SEMVER-MINOR) relocate the status checking code in the onwritecomplete (YoonSoo_Shin) nodejs#54032
tls:
  * (SEMVER-MINOR) add `allowPartialTrustChain` flag (Anna Henningsen) nodejs#54790
v8:
  * Revert "v8: enable maglev on supported architectures (Joyee Cheung) nodejs#54384

PR-URL: nodejs#54966
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version. util Issues and PRs related to the built-in util module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.