diff --git a/lambda-runtime-api-client/src/tracing.rs b/lambda-runtime-api-client/src/tracing.rs index 51e187e4..9897c840 100644 --- a/lambda-runtime-api-client/src/tracing.rs +++ b/lambda-runtime-api-client/src/tracing.rs @@ -23,7 +23,7 @@ const DEFAULT_LOG_LEVEL: &str = "INFO"; /// if they're configured for your function. /// /// This subscriber sets the logging level based on environment variables: -/// - if `AWS_LAMBDA_LOG_LEVEL` is set, it takes predecence over any other environment variables. +/// - if `AWS_LAMBDA_LOG_LEVEL` is set, it takes precedence over any other environment variables. /// - if `AWS_LAMBDA_LOG_LEVEL` is not set, check if `RUST_LOG` is set. /// - if none of those two variables are set, use `INFO` as the logging level. /// @@ -44,7 +44,7 @@ pub fn init_default_subscriber() { .from_env_lossy(), ); - if log_format.eq_ignore_ascii_case("json") { + if log_format.to_lowercase().contains("json") { collector.json().init() } else { collector.init() diff --git a/lambda-runtime/src/layers/trace.rs b/lambda-runtime/src/layers/trace.rs index e93927b1..7f499154 100644 --- a/lambda-runtime/src/layers/trace.rs +++ b/lambda-runtime/src/layers/trace.rs @@ -14,6 +14,14 @@ impl TracingLayer { pub fn new() -> Self { Self::default() } + + /// Returns true if the tracing span provided by this service should be included in the log output. + /// The span is enabled by default, + /// but can be disabled by adding `no-span` to the `AWS_LAMBDA_LOG_FORMAT` environment variable. + /// E.g. AWS_LAMBDA_LOG_FORMAT=no-span or =json,no-span. + pub fn is_enabled() -> bool { + !matches! (std::env::var("AWS_LAMBDA_LOG_FORMAT") , Ok(v) if v.to_lowercase().contains("no-span")) + } } impl Layer for TracingLayer { diff --git a/lambda-runtime/src/lib.rs b/lambda-runtime/src/lib.rs index 76d0562a..7df6cefe 100644 --- a/lambda-runtime/src/lib.rs +++ b/lambda-runtime/src/lib.rs @@ -120,6 +120,11 @@ where D: Into + Send, E: Into + Send + Debug, { - let runtime = Runtime::new(handler).layer(layers::TracingLayer::new()); - runtime.run().await + if layers::TracingLayer::is_enabled() { + let runtime = Runtime::new(handler).layer(layers::TracingLayer::new()); + runtime.run().await + } else { + let runtime = Runtime::new(handler); + runtime.run().await + } }