-
Notifications
You must be signed in to change notification settings - Fork 518
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
fix(hapi-instrumentation): close spans on errors in instrumented functions #589
Conversation
Codecov Report
@@ Coverage Diff @@
## main #589 +/- ##
==========================================
+ Coverage 94.94% 94.96% +0.01%
==========================================
Files 200 200
Lines 11741 11787 +46
Branches 1120 1122 +2
==========================================
+ Hits 11148 11194 +46
Misses 593 593
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for yet another contribution! The implementation can be simplified - see the comments.
return res; | ||
try { | ||
const res = await oldHandler(request, h, err); | ||
return res; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need the assignment nor await
here. Just return oldHandler(...);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed the unneeded assignment.
as for the await, it is needed as catch won't work on promises that are not awaited, and I also need to wait to close the span after the action was completed.
maybe i should add a test that includes an async handler?
Also, there is a handy eslint rule that covers that https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/return-await.md.
try { | ||
let res; | ||
await api.context.with( | ||
api.trace.setSpan(api.context.active(), span), | ||
async () => { | ||
res = await method(...params); | ||
} | ||
); | ||
return res; | ||
} catch (err) { | ||
span.recordException(err); | ||
span.setStatus({ | ||
code: api.SpanStatusCode.ERROR, | ||
message: err.message, | ||
}); | ||
throw err; | ||
} finally { | ||
span.end(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
contex.with
passes return values through so the closure is not required here.
This or something similar should work:
try {
return api.context.with(
api.trace.setSpan(api.context.active(), span),
() => method(...params)
);
} catch (err) {
span.recordException(err);
span.setStatus({
code: api.SpanStatusCode.ERROR,
message: err.message,
});
throw err;
} finally {
span.end();
}
Additionally, with
also takes arguments to be passed to the function, so it can be simplified even further:
try {
return api.context.with(
api.trace.setSpan(api.context.active(), span),
method,
...params
);
} catch (err) {
span.recordException(err);
span.setStatus({
code: api.SpanStatusCode.ERROR,
message: err.message,
});
throw err;
} finally {
span.end();
}
Test it out!
Docs: https://open-telemetry.github.io/opentelemetry-js/classes/_opentelemetry_context_async_hooks.asynchookscontextmanager.html#with
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! it looks much better now.
Which problem is this PR solving?
Short description of the changes