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
You can see more examples on how to use these error crates in our [example repository](https://github.com/awslabs/aws-lambda-rust-runtime/tree/main/examples/basic-error-error-crates-integration).
128
128
129
+
### Graceful shutdown
130
+
131
+
`lambda_runtime` offers a helper to simplify configuring graceful shutdown signal handling, `spawn_graceful_shutdown_handler()`. This requires the `graceful-shutdown` feature flag and only supports Unix systems.
132
+
133
+
You can use it by passing a `FnOnce` closure that returns an async block. That async block will be executed
134
+
when the function receives a `SIGTERM` or `SIGKILL`.
135
+
136
+
Note that this helper is opinionated in a number of ways. Most notably:
137
+
1. It spawns a task to drive your signal handlers
138
+
2. It registers a 'no-op' extension in order to enable graceful shutdown signals
139
+
3. It panics on unrecoverable errors
140
+
141
+
If you prefer to fine-tune the behavior, refer to the implementation of `spawn_graceful_shutdown_handler()` as a starting point for your own.
142
+
143
+
For more information on graceful shutdown handling in AWS Lambda, see: [aws-samples/graceful-shutdown-with-aws-lambda](https://github.com/aws-samples/graceful-shutdown-with-aws-lambda).
144
+
145
+
Complete example (cleaning up a non-blocking tracing writer):
146
+
147
+
```rust,no_run
148
+
use lambda_runtime::{service_fn, LambdaEvent, Error};
149
+
use serde_json::{json, Value};
150
+
151
+
#[tokio::main]
152
+
async fn main() -> Result<(), Error> {
153
+
let func = service_fn(func);
154
+
155
+
let (writer, log_guard) = tracing_appender::non_blocking(std::io::stdout());
Copy file name to clipboardExpand all lines: lambda-runtime-api-client/src/tracing.rs
+33-2
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,16 @@ pub use tracing::*;
14
14
15
15
/// Re-export the `tracing-subscriber` crate to build your own subscribers.
16
16
pubuse tracing_subscriber as subscriber;
17
+
use tracing_subscriber::fmt::MakeWriter;
17
18
18
19
constDEFAULT_LOG_LEVEL:&str = "INFO";
19
20
20
21
/// Initialize `tracing-subscriber` with default logging options.
21
22
///
22
-
/// This function uses environment variables set with [Lambda's advance logging controls](https://aws.amazon.com/blogs/compute/introducing-advanced-logging-controls-for-aws-lambda-functions/)
23
+
/// The default subscriber writes logs to STDOUT in the current context.
24
+
/// If you want to customize the writer, see [`init_default_subscriber_with_writer()`].
25
+
///
26
+
/// This function uses environment variables set with [Lambda's advanced logging controls](https://aws.amazon.com/blogs/compute/introducing-advanced-logging-controls-for-aws-lambda-functions/)
23
27
/// if they're configured for your function.
24
28
///
25
29
/// This subscriber sets the logging level based on environment variables:
/// Initialize `tracing-subscriber` with default logging options, and a custom writer.
42
+
///
43
+
/// You might want to avoid writing to STDOUT in the local context via [`init_default_subscriber()`], if you have a high-throughput Lambdas that involve
44
+
/// a lot of async concurrency. Since, writing to STDOUT can briefly block your tokio runtime - ref [tracing #2653](https://github.com/tokio-rs/tracing/issues/2653).
45
+
/// In that case, you might prefer to use [tracing_appender::NonBlocking] instead - particularly if your Lambda is fairly long-running and stays warm.
46
+
/// Though, note that you are then responsible
47
+
/// for ensuring gracefuls shutdown. See [`examples/graceful-shutdown`] for a complete example.
48
+
///
49
+
/// This function uses environment variables set with [Lambda's advanced logging controls](https://aws.amazon.com/blogs/compute/introducing-advanced-logging-controls-for-aws-lambda-functions/)
50
+
/// if they're configured for your function.
51
+
///
52
+
/// This subscriber sets the logging level based on environment variables:
53
+
/// - if `AWS_LAMBDA_LOG_LEVEL` is set, it takes precedence over any other environment variables.
54
+
/// - if `AWS_LAMBDA_LOG_LEVEL` is not set, check if `RUST_LOG` is set.
55
+
/// - if none of those two variables are set, use `INFO` as the logging level.
56
+
///
57
+
/// The logging format can also be changed based on Lambda's advanced logging controls.
58
+
/// If the `AWS_LAMBDA_LOG_FORMAT` environment variable is set to `JSON`, the log lines will be formatted as json objects,
59
+
/// otherwise they will be formatted with the default tracing format.
0 commit comments