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
Changes from 1 commit
8e25785
72dde06
582748b
db87b26
e8dbe27
17f7eb2
aa104f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -337,15 +337,25 @@ export class HapiInstrumentation extends InstrumentationBase { | |
const span = instrumentation.tracer.startSpan(metadata.name, { | ||
attributes: metadata.attributes, | ||
}); | ||
let res; | ||
await api.context.with( | ||
api.trace.setSpan(api.context.active(), span), | ||
async () => { | ||
res = await method(...params); | ||
} | ||
); | ||
span.end(); | ||
return res; | ||
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(); | ||
} | ||
}; | ||
return newHandler as T; | ||
} | ||
|
@@ -386,10 +396,19 @@ export class HapiInstrumentation extends InstrumentationBase { | |
const span = instrumentation.tracer.startSpan(metadata.name, { | ||
attributes: metadata.attributes, | ||
}); | ||
const res = await oldHandler(request, h, err); | ||
span.end(); | ||
|
||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Don't need the assignment nor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed the unneeded assignment. |
||
} catch (err) { | ||
span.recordException(err); | ||
span.setStatus({ | ||
code: api.SpanStatusCode.ERROR, | ||
message: err.message, | ||
}); | ||
throw err; | ||
} finally { | ||
span.end(); | ||
} | ||
}; | ||
if (route.options?.handler) { | ||
route.options.handler = newHandler; | ||
|
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:
Additionally,
with
also takes arguments to be passed to the function, so it can be simplified even further: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.