-
Notifications
You must be signed in to change notification settings - Fork 387
feat(lambda-runtime): log non-2xx Lambda Runtime API responses with status and body #1109
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
Merged
+243
−8
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b553d1e
feat: ⚡ Improved Logging when Lambda Runtime Api fails, this will be …
2a86319
chore: fix linting
92367c3
chore: add traced_test to test_concurrent_structured_logging to creat…
1a5204d
chore: fmt
1a84beb
chore: adding a todo for consuoing the body of the response
6ace45b
chore: adding format stuff
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,10 +85,230 @@ where | |
| .boxed(); | ||
| self.set(RuntimeApiClientFuture::Second(next_fut)); | ||
| } | ||
| Err(err) => break Err(err), | ||
| Err(err) => { | ||
| log_or_print!( | ||
| tracing: tracing::error!(error = ?err, "failed to build Lambda Runtime API request"), | ||
| fallback: eprintln!("failed to build Lambda Runtime API request: {err:?}") | ||
| ); | ||
| break Err(err); | ||
| } | ||
| }, | ||
| RuntimeApiClientFutureProj::Second(fut) => match ready!(fut.poll(cx)) { | ||
| Ok(resp) if !resp.status().is_success() => { | ||
| let status = resp.status(); | ||
|
|
||
| // TODO | ||
| // we should consume the response body of the call in order to give a more specific message. | ||
| // https://github.com/aws/aws-lambda-rust-runtime/issues/1110 | ||
|
|
||
| log_or_print!( | ||
| tracing: tracing::error!(status = %status, "Lambda Runtime API returned non-200 response"), | ||
| fallback: eprintln!("Lambda Runtime API returned non-200 response: status={status}") | ||
| ); | ||
|
|
||
| // Adding more information on top of 410 Gone, to make it more clear since we cannot access the body of the message | ||
| if status == 410 { | ||
| log_or_print!( | ||
| tracing: tracing::error!("Lambda function timeout!"), | ||
| fallback: eprintln!("Lambda function timeout!") | ||
| ); | ||
| } | ||
|
|
||
| // Return Ok to maintain existing contract - runtime continues despite API errors | ||
| break Ok(()); | ||
| } | ||
| Ok(_) => break Ok(()), | ||
| Err(err) => { | ||
| log_or_print!( | ||
| tracing: tracing::error!(error = ?err, "Lambda Runtime API request failed"), | ||
| fallback: eprintln!("Lambda Runtime API request failed: {err:?}") | ||
| ); | ||
| break Err(err); | ||
| } | ||
|
Comment on lines
+96
to
+127
Collaborator
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. Exactly what I had in mind. |
||
| }, | ||
| RuntimeApiClientFutureProj::Second(fut) => break ready!(fut.poll(cx)).map(|_| ()), | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use http::StatusCode; | ||
| use http_body_util::Full; | ||
| use hyper::body::Bytes; | ||
| use lambda_runtime_api_client::body::Body; | ||
| use std::convert::Infallible; | ||
| use tokio::net::TcpListener; | ||
| use tracing_test::traced_test; | ||
|
|
||
| async fn start_mock_server(status: StatusCode) -> String { | ||
| let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
| let addr = listener.local_addr().unwrap(); | ||
| let url = format!("http://{}", addr); | ||
|
Comment on lines
+145
to
+148
Collaborator
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. Much easier than harness tests ;) |
||
|
|
||
| tokio::spawn(async move { | ||
| let (stream, _) = listener.accept().await.unwrap(); | ||
| let io = hyper_util::rt::TokioIo::new(stream); | ||
|
|
||
| let service = hyper::service::service_fn(move |_req| async move { | ||
| Ok::<_, Infallible>( | ||
| http::Response::builder() | ||
| .status(status) | ||
| .body(Full::new(Bytes::from("test response"))) | ||
| .unwrap(), | ||
| ) | ||
| }); | ||
|
|
||
| let _ = hyper_util::server::conn::auto::Builder::new(hyper_util::rt::TokioExecutor::new()) | ||
| .serve_connection(io, service) | ||
| .await; | ||
| }); | ||
|
|
||
| // Give the server a moment to start | ||
| tokio::time::sleep(tokio::time::Duration::from_millis(10)).await; | ||
| url | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[traced_test] | ||
| async fn test_successful_response() { | ||
| let url = start_mock_server(StatusCode::OK).await; | ||
| let client = Arc::new( | ||
| lambda_runtime_api_client::Client::builder() | ||
| .with_endpoint(url.parse().unwrap()) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| let request_fut = | ||
| async { Ok::<_, BoxError>(http::Request::builder().uri("/test").body(Body::empty()).unwrap()) }; | ||
|
|
||
| let future = RuntimeApiClientFuture::First(request_fut, client); | ||
| let result = future.await; | ||
|
|
||
| assert!(result.is_ok()); | ||
| // No error logs should be present | ||
| assert!(!logs_contain("Lambda Runtime API returned non-200 response")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[traced_test] | ||
| async fn test_410_timeout_error() { | ||
| let url = start_mock_server(StatusCode::GONE).await; | ||
| let client = Arc::new( | ||
| lambda_runtime_api_client::Client::builder() | ||
| .with_endpoint(url.parse().unwrap()) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| let request_fut = | ||
| async { Ok::<_, BoxError>(http::Request::builder().uri("/test").body(Body::empty()).unwrap()) }; | ||
|
|
||
| let future = RuntimeApiClientFuture::First(request_fut, client); | ||
| let result = future.await; | ||
|
|
||
| // Returns Ok to maintain contract, but logs the error | ||
| assert!(result.is_ok()); | ||
|
|
||
| // Verify the error was logged | ||
| assert!(logs_contain("Lambda Runtime API returned non-200 response")); | ||
| assert!(logs_contain("Lambda function timeout!")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[traced_test] | ||
| async fn test_500_error() { | ||
| let url = start_mock_server(StatusCode::INTERNAL_SERVER_ERROR).await; | ||
| let client = Arc::new( | ||
| lambda_runtime_api_client::Client::builder() | ||
| .with_endpoint(url.parse().unwrap()) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| let request_fut = | ||
| async { Ok::<_, BoxError>(http::Request::builder().uri("/test").body(Body::empty()).unwrap()) }; | ||
|
|
||
| let future = RuntimeApiClientFuture::First(request_fut, client); | ||
| let result = future.await; | ||
|
|
||
| // Returns Ok to maintain contract, but logs the error | ||
| assert!(result.is_ok()); | ||
|
|
||
| // Verify the error was logged with status code | ||
| assert!(logs_contain("Lambda Runtime API returned non-200 response")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[traced_test] | ||
| async fn test_404_error() { | ||
| let url = start_mock_server(StatusCode::NOT_FOUND).await; | ||
| let client = Arc::new( | ||
| lambda_runtime_api_client::Client::builder() | ||
| .with_endpoint(url.parse().unwrap()) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| let request_fut = | ||
| async { Ok::<_, BoxError>(http::Request::builder().uri("/test").body(Body::empty()).unwrap()) }; | ||
|
|
||
| let future = RuntimeApiClientFuture::First(request_fut, client); | ||
| let result = future.await; | ||
|
|
||
| // Returns Ok to maintain contract, but logs the error | ||
| assert!(result.is_ok()); | ||
|
|
||
| // Verify the error was logged | ||
| assert!(logs_contain("Lambda Runtime API returned non-200 response")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[traced_test] | ||
| async fn test_request_build_error() { | ||
| let client = Arc::new( | ||
| lambda_runtime_api_client::Client::builder() | ||
| .with_endpoint("http://localhost:9001".parse().unwrap()) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| let request_fut = async { Err::<http::Request<Body>, BoxError>("Request build error".into()) }; | ||
|
|
||
| let future = RuntimeApiClientFuture::First(request_fut, client); | ||
| let result = future.await; | ||
|
|
||
| assert!(result.is_err()); | ||
| let err = result.unwrap_err(); | ||
| assert!(err.to_string().contains("Request build error")); | ||
|
|
||
| // Verify the error was logged | ||
| assert!(logs_contain("failed to build Lambda Runtime API request")); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[traced_test] | ||
| async fn test_network_error() { | ||
| // Use an invalid endpoint that will fail to connect | ||
| let client = Arc::new( | ||
| lambda_runtime_api_client::Client::builder() | ||
| .with_endpoint("http://127.0.0.1:1".parse().unwrap()) // Port 1 should be unreachable | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
|
|
||
| let request_fut = | ||
| async { Ok::<_, BoxError>(http::Request::builder().uri("/test").body(Body::empty()).unwrap()) }; | ||
|
|
||
| let future = RuntimeApiClientFuture::First(request_fut, client); | ||
| let result = future.await; | ||
|
|
||
| // Network errors should propagate as Err | ||
| assert!(result.is_err()); | ||
|
|
||
| // Verify the error was logged | ||
| assert!(logs_contain("Lambda Runtime API request failed")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Logs using tracing `error!` if a dispatcher is set, otherwise falls back to `eprintln!`. | ||
| macro_rules! log_or_print { | ||
darklight3it marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (tracing: $tracing_expr:expr, fallback: $fallback_expr:expr) => { | ||
| if tracing::dispatcher::has_been_set() { | ||
| $tracing_expr; | ||
| } else { | ||
| $fallback_expr; | ||
| } | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.