Skip to content

Logging error chains to CloudWatch #348

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

Closed
mlafeldt opened this issue Sep 23, 2021 · 3 comments · Fixed by #418
Closed

Logging error chains to CloudWatch #348

mlafeldt opened this issue Sep 23, 2021 · 3 comments · Fixed by #418

Comments

@mlafeldt
Copy link

mlafeldt commented Sep 23, 2021

I noticed that the Rust runtime currently logs handler errors using {} as the format:

error!("{}", err); // logs the error in CloudWatch

However, the widely used anyhow crate will only include the error chain with {:?} or {:#}:

https://github.com/dtolnay/anyhow/blob/71caf88b78cc3faf2b99ac3d7397a66a7da5a930/src/fmt.rs#L7-L40

Among other things, this means that only the last error will be logged to CloudWatch and that all underlying causes are lost when using with_context to augment errors.

My current solution/workaround is to add an additional log statement like this:

#[tokio::main]
async fn main() -> Result<(), Error> {
    env_logger::try_init()?;

    lambda_runtime::run(handler_fn(|_: Input, _: Context| async {
        let output = handler().await.map_err(|e| {
            error!("{:?}", e); // log error chain to CloudWatch
            e
        })?;
        Ok(output) as Result<Output>
    }))
    .await?;

    Ok(())
}

Of course, this duplicates error logging to some degree and would have to be done in every Lambda function. I wonder if there's a better way of telling the runtime how to log errors?

@mlafeldt
Copy link
Author

Related to this, the error returned to the caller also misses any context:

error_message: format!("{}", err), // returns the error to the caller via Lambda API

@calavera
Copy link
Contributor

calavera commented Feb 9, 2022

@mlafeldt do you want to open a PR and change this? I think we'd be open to that improvement.

mlafeldt added a commit to mlafeldt/aws-lambda-rust-runtime that referenced this issue Feb 9, 2022
The anyhow crate will only include the error chain with {:?} or {:#}.

Without this change, only the last error will be logged to CloudWatch
and all underlying causes are lost when using with_context to augment
errors.

Fixes awslabs#348
@mlafeldt
Copy link
Author

mlafeldt commented Feb 9, 2022

@calavera Absolutely. See #413

mlafeldt added a commit to mlafeldt/aws-lambda-rust-runtime that referenced this issue Feb 9, 2022
The anyhow crate will only include the error chain with {:?} or {:#}.

Without this change, only the last error will be logged to CloudWatch
and all underlying causes are lost when using with_context to augment
errors.

Fixes awslabs#348
mlafeldt added a commit to mlafeldt/aws-lambda-rust-runtime that referenced this issue Feb 9, 2022
The anyhow crate will only include the error chain with {:?} or {:#}.

Without this change, only the last error will be logged to CloudWatch
and all underlying causes are lost when using with_context to augment
errors.

Fixes awslabs#348
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants