Skip to content

Commit 3d51677

Browse files
committed
feat(tracing): add init_default_subscriber_with_writer()
1 parent c511046 commit 3d51677

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

lambda-runtime-api-client/src/tracing.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ pub use tracing::*;
1414

1515
/// Re-export the `tracing-subscriber` crate to build your own subscribers.
1616
pub use tracing_subscriber as subscriber;
17+
use tracing_subscriber::fmt::MakeWriter;
1718

1819
const DEFAULT_LOG_LEVEL: &str = "INFO";
1920

2021
/// Initialize `tracing-subscriber` with default logging options.
2122
///
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/)
2327
/// if they're configured for your function.
2428
///
2529
/// This subscriber sets the logging level based on environment variables:
@@ -31,6 +35,32 @@ const DEFAULT_LOG_LEVEL: &str = "INFO";
3135
/// If the `AWS_LAMBDA_LOG_FORMAT` environment variable is set to `JSON`, the log lines will be formatted as json objects,
3236
/// otherwise they will be formatted with the default tracing format.
3337
pub fn init_default_subscriber() {
38+
init_default_subscriber_with_writer(std::io::stdout);
39+
}
40+
41+
/// 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.
60+
pub fn init_default_subscriber_with_writer<Writer>(writer: Writer)
61+
where
62+
Writer: for<'writer> MakeWriter<'writer> + Send + Sync + 'static,
63+
{
3464
let log_format = env::var("AWS_LAMBDA_LOG_FORMAT").unwrap_or_default();
3565
let log_level_str = env::var("AWS_LAMBDA_LOG_LEVEL").or_else(|_| env::var("RUST_LOG"));
3666
let log_level =
@@ -43,7 +73,8 @@ pub fn init_default_subscriber() {
4373
EnvFilter::builder()
4474
.with_default_directive(log_level.into())
4575
.from_env_lossy(),
46-
);
76+
)
77+
.with_writer(writer);
4778

4879
if log_format.eq_ignore_ascii_case("json") {
4980
collector.json().init()

0 commit comments

Comments
 (0)