Skip to content

Commit 4a43ad2

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 4a43ad2

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

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

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,42 @@ 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)| {
98+
let operation = event.info.operation;
99+
::lambda_appsync::tokio::spawn(
100+
Self::appsync_handler(event).instrument(
101+
::tracing::info_span!(
102+
"AppsyncEvent",
103+
"otel.name"=format!("AppsyncEvent #{index}"),
104+
batch_index=index,
105+
?operation
106+
)
107+
)
108+
)
109+
})
110+
.collect::<::std::vec::Vec<_>>();
111+
}
112+
} else {
113+
quote! {
114+
let handles = events
115+
.into_iter()
116+
.map(|event| ::lambda_appsync::tokio::spawn(Self::appsync_handler(event)))
117+
.collect::<::std::vec::Vec<_>>();
118+
}
119+
};
91120
batch_handler.extend(quote! {
92121
#[doc = "Handles a batch of [lambda_appsync::AppsyncEvent<Operation>] concurrently."]
93122
async fn appsync_batch_handler(
94123
events: ::std::vec::Vec<::lambda_appsync::AppsyncEvent<#operation>>
95124
) -> ::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<_>>();
125+
126+
#spawn_code
100127

101128
let mut results = ::std::vec::Vec::new();
102129
for h in handles {
@@ -127,6 +154,19 @@ impl ToTokens for MakeHandlers {
127154
event.info.operation.execute(event)
128155
}
129156
}
157+
} else if cfg!(feature = "tracing") {
158+
quote! {
159+
async fn appsync_handler(event: ::lambda_appsync::AppsyncEvent<#operation>) -> ::lambda_appsync::AppsyncResponse {
160+
let operation = event.info.operation;
161+
event.info.operation.execute(event).instrument(
162+
::tracing::info_span!(
163+
"AppsyncEvent",
164+
"otel.name"=format!("AppsyncEvent"),
165+
?operation
166+
)
167+
).await
168+
}
169+
}
130170
} else {
131171
quote! {
132172
async fn appsync_handler(event: ::lambda_appsync::AppsyncEvent<#operation>) -> ::lambda_appsync::AppsyncResponse {

0 commit comments

Comments
 (0)