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

js: Print stack trace when exception in handleSummary() #3416

Merged
merged 7 commits into from
Nov 1, 2023

Conversation

joanlopez
Copy link
Contributor

@joanlopez joanlopez commented Oct 24, 2023

What?

It splits the execution of the handleSummary() wrapper, so if there's an exception on the script, it prints the stack trace first, then falls back to the default summary.

It also moves the logic to print errext.HasHint and errext.Exception details for a given error into a shared function within the errext package, following "the fprint pattern" (that accepts a "writer", in this case a "logger"), so it can be reused where it was used until now ( rootCommand) and in the aforementioned exception handling.

Why?

Currently, exceptions in handleSummary() do not print stack, just a message that there was an exception, and that makes debugging it way harder than it should.

Checklist

  • I have performed a self-review of my code.
  • I have added tests for my changes.
  • I have run linter locally (make lint) and all checks pass.
  • I have run tests locally (make tests) and all tests pass.
  • I have commented on my code, particularly in hard-to-understand areas.

Related PR(s)/Issue(s)

Closes #3257

Other notes

I haven't added any new test because if I'm not wrong, I think all the logic changed is already covered by tests present in summary_test.go, like TestExceptionInHandleSummaryFallsBackToTextSummary that precisely asserts that the default summary is used as summary when the handleSummary() throws an exception.

In fact, if I'm not wrong, the main behavior should not change, it's just a matter of bringing feedback to the user (logs) in a slightly different way (more clear/explicit).

Thanks!

errext/print.go Outdated Show resolved Hide resolved
errext/print.go Outdated Show resolved Hide resolved
js/runner.go Outdated
if handleSummaryFn, ok := goja.AssertFunction(fn); ok {
callbackResult, _, _, err = vu.runFn(summaryCtx, false, handleSummaryFn, nil, vu.Runtime.ToValue(summaryDataForJS))
if err != nil {
errext.Fprint(r.preInitState.Logger, err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively to what I suggested at https://github.com/grafana/k6/pull/3416/files#r1370043306, I could add another log line here, giving that feedback to the user in a different line (and maybe as debug?).

@codecov-commenter
Copy link

codecov-commenter commented Oct 24, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (92e3aca) 73.34% compared to head (c02227e) 73.34%.

❗ Current head c02227e differs from pull request most recent head d16752e. Consider uploading reports for the commit d16752e to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3416   +/-   ##
=======================================
  Coverage   73.34%   73.34%           
=======================================
  Files         258      259    +1     
  Lines       19645    19653    +8     
=======================================
+ Hits        14408    14415    +7     
  Misses       4352     4352           
- Partials      885      886    +1     
Flag Coverage Δ
ubuntu 73.26% <100.00%> (-0.02%) ⬇️
windows 73.19% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
cmd/root.go 94.00% <100.00%> (-0.27%) ⬇️
errext/format.go 100.00% <100.00%> (ø)
js/runner.go 86.54% <100.00%> (+0.12%) ⬆️

... and 2 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@joanlopez joanlopez self-assigned this Oct 24, 2023
js/runner.go Outdated Show resolved Hide resolved
Copy link
Contributor

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

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

LGTM in general 👏

Can we maybe try option 2 from https://github.com/grafana/k6/pull/3416/files#r1370141890

@mstoykov
Copy link
Contributor

In fact, if I'm not wrong, the main behavior should not change, it's just a matter of bringing feedback to the user (logs) in a slightly different way (more clear/explicit).

That is true, but maybe we can extend

func TestExceptionInHandleSummaryFallsBackToTextSummary(t *testing.T) {
to test that we do actually get the stacktrace

@mstoykov mstoykov requested a review from oleiade October 24, 2023 14:36
@joanlopez
Copy link
Contributor Author

In fact, if I'm not wrong, the main behavior should not change, it's just a matter of bringing feedback to the user (logs) in a slightly different way (more clear/explicit).

That is true, but maybe we can extend

func TestExceptionInHandleSummaryFallsBackToTextSummary(t *testing.T) {

to test that we do actually get the stacktrace

Changed, as per what discussed "offline", thanks!

mstoykov
mstoykov previously approved these changes Oct 25, 2023
oleiade
oleiade previously approved these changes Oct 25, 2023
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏻 👏🏻 Great job @joanlopez 🙇🏻

errext/format.go Outdated Show resolved Hide resolved
@joanlopez joanlopez dismissed stale reviews from oleiade and mstoykov via 3aea489 October 30, 2023 08:52
errext/format.go Outdated Show resolved Hide resolved
mstoykov
mstoykov previously approved these changes Oct 30, 2023
oleiade
oleiade previously approved these changes Oct 30, 2023
Co-authored-by: Mihail Stoykov <312246+mstoykov@users.noreply.github.com>
@joanlopez joanlopez dismissed stale reviews from oleiade and mstoykov via d16752e October 30, 2023 10:33
@mstoykov mstoykov added this to the v0.48.0 milestone Nov 1, 2023
@mstoykov mstoykov merged commit 1ec6519 into master Nov 1, 2023
22 checks passed
@mstoykov mstoykov deleted the fix/summary-exception branch November 1, 2023 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception in handleSummary() do not have a stack
4 participants