Skip to content

Commit 6b4d23a

Browse files
committed
Feat: If the 'tracing' feature is enabled, the default generated batch handler ensures to spawn each child task with a dedicated span. It is important for scenarios where the sub task should be related to the overall application run, which should be desired most of the time. Disabling the 'tracing' feature will prevent this behavior. Alternatively users can always overwrite the handler
1 parent 334cb6f commit 6b4d23a

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

lambda-appsync-proc/src/internal/make_appsync/make_handlers.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,39 @@ impl ToTokens for MakeHandlers {
8888
let operation = &self.operation_type;
8989
let mut batch_handler = TokenStream2::new();
9090
if self.batch {
91+
let spawn_code = if cfg!(feature = "tracing") {
92+
quote! {
93+
use ::tracing::Instrument;
94+
let handles = events
95+
.into_iter()
96+
.enumerate()
97+
.map(|(index, event)| ::lambda_appsync::tokio::spawn(
98+
Self::appsync_handler(event).instrument(
99+
::tracing::info_span!(
100+
"AppsyncEvent",
101+
"otel.name"=format!("AppsyncEvent #{index}"),
102+
batch_index=index,
103+
operation=?event.info.operation
104+
)
105+
)
106+
))
107+
.collect::<::std::vec::Vec<_>>();
108+
}
109+
} else {
110+
quote! {
111+
let handles = events
112+
.into_iter()
113+
.map(|event| ::lambda_appsync::tokio::spawn(Self::appsync_handler(event)))
114+
.collect::<::std::vec::Vec<_>>();
115+
}
116+
};
91117
batch_handler.extend(quote! {
92118
#[doc = "Handles a batch of [lambda_appsync::AppsyncEvent<Operation>] concurrently."]
93119
async fn appsync_batch_handler(
94120
events: ::std::vec::Vec<::lambda_appsync::AppsyncEvent<#operation>>
95121
) -> ::std::vec::Vec<::lambda_appsync::AppsyncResponse> {
96-
let handles = events
97-
.into_iter()
98-
.map(|e| ::lambda_appsync::tokio::spawn(Self::appsync_handler(e)))
99-
.collect::<::std::vec::Vec<_>>();
122+
123+
#spawn_code
100124

101125
let mut results = ::std::vec::Vec::new();
102126
for h in handles {
@@ -127,6 +151,18 @@ impl ToTokens for MakeHandlers {
127151
event.info.operation.execute(event)
128152
}
129153
}
154+
} else if cfg!(feature = "tracing") {
155+
quote! {
156+
async fn appsync_handler(event: ::lambda_appsync::AppsyncEvent<#operation>) -> ::lambda_appsync::AppsyncResponse {
157+
event.info.operation.execute(event).instrument(
158+
::tracing::info_span!(
159+
"AppsyncEvent",
160+
"otel.name"=format!("AppsyncEvent"),
161+
operation=?event.info.operation
162+
)
163+
).await
164+
}
165+
}
130166
} else {
131167
quote! {
132168
async fn appsync_handler(event: ::lambda_appsync::AppsyncEvent<#operation>) -> ::lambda_appsync::AppsyncResponse {

0 commit comments

Comments
 (0)