You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add the possibility to flush traces/metrics after a handler loop has finished #691
The problem I currently have is that there is no good way to flush traces/logs/metrics/errors once the runtime has finished handling a message.
Currently the handling code is structured something like this:
let request_span = <create span for the request>
async {
<do parsing, call the user provider handler, catch panics etc>
}.instrument(tracing_span)
There are a couple of issues with this approach:
The user provided handler is the only way or the end user to affect the loop and thus he has to e.g. flush spans inside the handler
Since the handler can panic it is possible that the flushing is never done since it has to happen after the work has been done.
The request_span span can not be flushed since it's finished after the user provided handler is provided.
If the handler fails on e.g. parsing there is no way for the end user to send the errors to their error reporting tool of choice because their handler was never run.
I think it would be very valuable that the runtime would provide a hook for the end user so that they can do cleanup after each invocation.
Internal extension could be the solution. You could create a library to start a background thread to interact with Lambda Extension API, register for invoke events. The extension will get a notification event when an invoke happens and can perform all kinds of tasks. It could even continue running after the handler returns. It is a good place to flush logs/traces.
I added an example in #744 that uses an internal extension to flush telemetry after the handler finishes (once there's a new release of lambda-extension that includes the Extension::register API). Just before the handler returns, it notifies the extension via a Tokio channel.
As OP mentioned, handlers sometimes panic, so for a production use case I would wrap the bulk of the handler in something like futures_util::future::FutureExt::catch_unwind so that you can still clean up and flush telemetry even if the handler panics.
Internal extensions are an option, but I think we can make something simpler for this use case. I'm working on an RFC that I think will benefit the runtime in general providing a simpler solution.
I'm marking this as resolved. I'll release a new version with those improvements next week after adding some extra documentation. I'm closing this issue as resolved since it's possible to implement what you explained in this issue now.
This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
Activity
purkhusid commentedon Sep 5, 2023
I patched the runtime with this to fix tracing in our lambdas:
calavera commentedon Sep 5, 2023
Can you open a PR so we can review it with more context?
purkhusid commentedon Sep 5, 2023
Sure thing.
BMorinDrifter commentedon Sep 5, 2023
Cross-posting what I said in the PR here:
See MetricsService in https://github.com/BMorinDrifter/metrics-cloudwatch-embedded/blob/main/src/lambda.rs for how I did this for the embedded metrics crate.
bnusunny commentedon Sep 10, 2023
Internal extension could be the solution. You could create a library to start a background thread to interact with Lambda Extension API, register for invoke events. The extension will get a notification event when an invoke happens and can perform all kinds of tasks. It could even continue running after the handler returns. It is a good place to flush logs/traces.
ramosbugs commentedon Dec 3, 2023
I added an example in #744 that uses an internal extension to flush telemetry after the handler finishes (once there's a new release of
lambda-extensionthat includes theExtension::registerAPI). Just before the handler returns, it notifies the extension via a Tokio channel.As OP mentioned, handlers sometimes panic, so for a production use case I would wrap the bulk of the handler in something like
futures_util::future::FutureExt::catch_unwindso that you can still clean up and flush telemetry even if the handler panics.calavera commentedon Dec 4, 2023
Internal extensions are an option, but I think we can make something simpler for this use case. I'm working on an RFC that I think will benefit the runtime in general providing a simpler solution.
calavera commentedon Dec 4, 2023
This is what I had in mind #747
calavera commentedon Mar 22, 2024
The layering system that I described in #747 has landed in the main branch. Look at this example on how to flush traces/metrics after the handler loop has finished: https://github.com/awslabs/aws-lambda-rust-runtime/blob/main/examples/opentelemetry-tracing/src/main.rs
I'm marking this as resolved. I'll release a new version with those improvements next week after adding some extra documentation. I'm closing this issue as resolved since it's possible to implement what you explained in this issue now.
github-actions commentedon Mar 22, 2024
This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.