From 9117f85f54005929ce027c83745fda0b8a8c757f Mon Sep 17 00:00:00 2001 From: rimutaka Date: Mon, 15 Jul 2024 08:51:05 +1200 Subject: [PATCH 1/2] Test implementation of optional tracing span --- lambda-runtime-api-client/src/tracing.rs | 4 ++-- lambda-runtime/src/layers/trace.rs | 8 ++++++++ lambda-runtime/src/lib.rs | 9 +++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) 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..15af25ba 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 + } } From 5266c4f7064704273587a6a5ea6f1ea6752be266 Mon Sep 17 00:00:00 2001 From: rimutaka Date: Mon, 15 Jul 2024 09:31:12 +1200 Subject: [PATCH 2/2] cargo fmt fix --- lambda-runtime/src/layers/trace.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lambda-runtime/src/layers/trace.rs b/lambda-runtime/src/layers/trace.rs index 15af25ba..7f499154 100644 --- a/lambda-runtime/src/layers/trace.rs +++ b/lambda-runtime/src/layers/trace.rs @@ -16,7 +16,7 @@ impl TracingLayer { } /// Returns true if the tracing span provided by this service should be included in the log output. - /// The span is enabled by default, + /// 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 {