Skip to content

Commit 3ec881b

Browse files
authored
feat: Add tracing support and configurable logging initialization
### **Summary** This PR introduces comprehensive tracing support alongside the existing env_logger functionality, while making logging initialization configurable and event logging optional. ### **Key Features Added** #### **🔧 New Feature Flags** - **`tracing`**: Enables tracing/tracing-subscriber integration as an alternative to env_logger - **`env_logger`**: Now an optional feature (still enabled by default for backward compatibility) - **`log`**: Independent log feature flag that controls log statement generation in generated code and the availability of re-exported `log` crate #### **📝 New Macro Parameters** - **`log_init = function_name`**: Allows custom log initialization functions instead of relying on defaults - **`event_logging = bool`**: Controls whether Lambda event payloads are logged (now defaults to `false` for better security) #### **🚀 Tracing Integration** - Adds `#[tracing::instrument]` attributes to generated handler functions - Configures structured JSON logging by default - Includes request ID and operation name in trace spans - Aimed at replacing the existing env_logger setup when enabled (but both features can be enabled together) ### **Technical Changes** #### **Architecture** - **Feature-aware code generation**: Proc macro now conditionally generates logging code based on enabled features - **Flexible initialization**: Supports custom logging setup functions with compile-time signature validation - **Conditional compilation**: Uses cfg attributes to include only relevant logging dependencies #### **Default Behavior Changes** - Event payload logging (the event received by the Lambda function) is now disabled by default for security reasons. It must be explicitly enabled by the new `event_logging = bool` macro option. - When event logging is enabled, events are logged at `debug` level instead of `info` ### **Breaking Changes** - **Event Logging**: The full dump of the event payload received by the lambda function was always performed, it is now disabled by default (can be re-enabled with `event_logging = true`) ### **Backward Compatibility** - Default feature set remains `["env_logger"]` ensuring existing code continues to work - All existing macro parameters and functionality preserved - Generated code structure unchanged for existing feature combinations ### **Examples Usage** ```rust // Custom log initialization appsync_lambda_main!("schema.graphql", log_init = my_custom_logger); // Enable event logging for debugging appsync_lambda_main!("schema.graphql", event_logging = true); ```
1 parent 1d8fd46 commit 3ec881b

14 files changed

Lines changed: 355 additions & 34 deletions

File tree

Cargo.lock

Lines changed: 45 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,20 @@ tokio = { version = "1", features = ["macros"] }
2828
lambda_runtime = "1.0"
2929
aws-config = { version = "1.5", features = ["behavior-version-latest"] }
3030
aws-smithy-types = "1.3"
31-
log = "0.4"
32-
env_logger = "0.11"
3331
thiserror = "1.0"
3432
uuid = { version = "1.11", features = ["v4"] }
3533
serde = { version = "1.0", features = ["derive"] }
3634
serde_json = "1.0"
3735

36+
# Default Lambda logging is based on log/env_logger
37+
log = "0.4"
38+
env_logger = "0.11"
39+
40+
# Alternatively, a feature allow the use of tracing/tracing-subscriber
41+
tracing = "0.1"
42+
tracing-subscriber = { version = "0.3", features = ["json", "env-filter"] }
43+
44+
3845
# Proc-macro crate dependencies
3946
syn = { version = "2.0", default-features = false, features = [
4047
"parsing",

lambda-appsync-proc/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ proc-macro2 = { workspace = true }
2121
graphql-parser = { workspace = true }
2222

2323
[dev-dependencies]
24-
lambda-appsync = { path = "../lambda-appsync" }
24+
lambda-appsync = { path = "../lambda-appsync", default-features = false, features = ["log", "env_logger", "tracing"] }
25+
tracing = { workspace = true }
2526
aws-sdk-s3 = { workspace = true }
2627
aws-sdk-dynamodb = { workspace = true }
2728
serde = { workspace = true }
2829
trybuild = { workspace = true }
30+
31+
[features]
32+
default = ["log", "env_logger", "tracing"]
33+
log = []
34+
env_logger = []
35+
tracing = []

lambda-appsync-proc/src/appsync_lambda_main/graphql.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,16 @@ impl GraphQLSchema {
971971
let subscription_field_execute_match_arm = self
972972
.subscriptions
973973
.execute_match_arm_iter(OperationKind::Subscription);
974+
974975
let span = current_span();
976+
977+
#[allow(unused_mut)]
978+
let mut log_lines = proc_macro2::TokenStream::new();
979+
#[cfg(feature = "log")]
980+
log_lines.extend(quote_spanned! {span=>
981+
::lambda_appsync::log::error!("{e}");
982+
});
983+
975984
tokens.extend(quote_spanned! {span=>
976985
impl Operation {
977986
async fn execute(self,
@@ -980,7 +989,7 @@ impl GraphQLSchema {
980989
match self._execute(event).await {
981990
::core::result::Result::Ok(v) => v.into(),
982991
::core::result::Result::Err(e) => {
983-
::lambda_appsync::log::error!("{e}");
992+
#log_lines
984993
e.into()
985994
}
986995
}

0 commit comments

Comments
 (0)