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

[Koa] Span never ends when a middleware layer throws an exception #279

Closed
oguzbilgener opened this issue Dec 11, 2020 · 0 comments · Fixed by #281
Closed

[Koa] Span never ends when a middleware layer throws an exception #279

oguzbilgener opened this issue Dec 11, 2020 · 0 comments · Fixed by #281
Labels
bug Something isn't working

Comments

@oguzbilgener
Copy link
Contributor

What version of OpenTelemetry are you using?

Versions 0.12.0 and 0.13.0

What version of Node are you using?

Node 12.20.0

What did you do?

In Koa, a middleware can throw exceptions and this exception can be handled by another middleware to return an error message in the response. The @opentelemetry/koa-instrumentation plugin creates a new span for each Koa middleware, wraps the actual middleware call and once this async call is over, it calls span.end().

return this._tracer.withSpan(span, async () => {
const result = await middlewareLayer(context, next);
span.end();
return result;

What did you expect to see?

When an exception happens in the middlewareLayer, I expect the Koa plugin to account for it, mark the span for the middleware with an error by calling span.recordException(err), end the span, and then re-throw the exception.

This would be similar to what other plugins do. Below is an example from the graphql instrumentation plugin:

return instrumentation.tracer.withSpan(span, () => {
return safeExecuteInTheMiddle<
PromiseOrValue<graphqlTypes.ExecutionResult>
>(
() => {
return (original as executeFunctionWithObj).apply(this, [
processedArgs,
]);
},
err => {
endSpan(span, err);

What did you see instead?

Because of the uncaught exception, a span.end() call is never made, so the span is completely missing.


I can gladly come up with a pull request using the safeExecuteInTheMiddle utility function, if you think the solution should be similar to what I expect to see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant