@@ -10,7 +10,9 @@ use lambda_runtime::{
1010 LambdaEvent ,
1111} ;
1212use opentelemetry_sdk:: trace:: SdkTracerProvider ;
13- use serde:: Serialize ;
13+ use serde:: de:: DeserializeOwned ;
14+ use serde_json:: Value ;
15+ use std:: marker:: PhantomData ;
1416use std:: sync:: Arc ;
1517use std:: task:: { Context as TaskContext , Poll } ;
1618
@@ -25,11 +27,11 @@ pub struct Config {}
2527pub fn wrap_handler < F , Fut , E , R > (
2628 handler : F ,
2729 provider : SdkTracerProvider ,
28- ) -> impl Fn ( LambdaEvent < E > ) -> BoxFuture < Result < R , HandlerError > >
30+ ) -> impl Fn ( LambdaEvent < Value > ) -> BoxFuture < Result < R , HandlerError > >
2931where
3032 F : Fn ( LambdaEvent < E > ) -> Fut + Send + Sync + ' static ,
3133 Fut : std:: future:: Future < Output = Result < R , HandlerError > > + Send + ' static ,
32- E : Serialize + Send + Sync + ' static ,
34+ E : DeserializeOwned + Send + Sync + ' static ,
3335 R : Send + ' static ,
3436{
3537 wrap_handler_with_config ( handler, provider, Config :: default ( ) )
@@ -40,21 +42,26 @@ pub fn wrap_handler_with_config<F, Fut, E, R>(
4042 handler : F ,
4143 provider : SdkTracerProvider ,
4244 config : Config ,
43- ) -> impl Fn ( LambdaEvent < E > ) -> BoxFuture < Result < R , HandlerError > >
45+ ) -> impl Fn ( LambdaEvent < Value > ) -> BoxFuture < Result < R , HandlerError > >
4446where
4547 F : Fn ( LambdaEvent < E > ) -> Fut + Send + Sync + ' static ,
4648 Fut : std:: future:: Future < Output = Result < R , HandlerError > > + Send + ' static ,
47- E : Serialize + Send + Sync + ' static ,
49+ E : DeserializeOwned + Send + Sync + ' static ,
4850 R : Send + ' static ,
4951{
5052 let handler = Arc :: new ( handler) ;
5153 let config = Arc :: new ( config) ;
52- move |event : LambdaEvent < E > | {
54+ move |event : LambdaEvent < Value > | {
5355 let handler = Arc :: clone ( & handler) ;
5456 let provider = provider. clone ( ) ;
5557 let config = Arc :: clone ( & config) ;
5658 let scope = start_invocation ( & event, & provider, & config) ;
57- let fut = handler ( event) ;
59+ let typed_payload = match serde_json:: from_value :: < E > ( event. payload ) {
60+ Ok ( p) => p,
61+ Err ( e) => return Box :: pin ( async move { Err ( e. into ( ) ) } ) ,
62+ } ;
63+ let typed_event = LambdaEvent :: new ( typed_payload, event. context ) ;
64+ let fut = handler ( typed_event) ;
5865 Box :: pin ( async move { run_in_invocation_scope ( scope, provider, fut) . await } )
5966 }
6067}
@@ -89,15 +96,17 @@ impl<S> Layer<S> for DatadogLambdaLayer {
8996 inner,
9097 provider : self . provider . clone ( ) ,
9198 config : Arc :: clone ( & self . config ) ,
99+ _phantom : PhantomData ,
92100 }
93101 }
94102}
95103
96104#[ doc( hidden) ]
97- pub struct DatadogLambdaService < S > {
105+ pub struct DatadogLambdaService < S , E = Value > {
98106 inner : S ,
99107 provider : SdkTracerProvider ,
100108 config : Arc < Config > ,
109+ _phantom : PhantomData < fn ( LambdaEvent < E > ) > ,
101110}
102111
103112#[ cfg( test) ]
@@ -119,27 +128,32 @@ mod tests {
119128 }
120129}
121130
122- impl < S , E , R , Err > Service < LambdaEvent < E > > for DatadogLambdaService < S >
131+ impl < S , E > Service < LambdaEvent < Value > > for DatadogLambdaService < S , E >
123132where
124- S : Service < LambdaEvent < E > , Response = R , Error = Err > + Send + ' static ,
133+ S : Service < LambdaEvent < E > > + Send + ' static ,
125134 S :: Future : Send + ' static ,
126- E : Serialize + Send + Sync + ' static ,
127- R : Send + ' static ,
128- Err : std :: fmt :: Display + Send + ' static ,
135+ S :: Response : Send + ' static ,
136+ S :: Error : From < serde_json :: Error > + std :: fmt :: Display + Send + ' static ,
137+ E : DeserializeOwned + Send + Sync + ' static ,
129138{
130- type Response = R ;
131- type Error = Err ;
132- type Future = BoxFuture < Result < R , Err > > ;
139+ type Response = S :: Response ;
140+ type Error = S :: Error ;
141+ type Future = BoxFuture < Result < S :: Response , S :: Error > > ;
133142
134143 fn poll_ready ( & mut self , cx : & mut TaskContext < ' _ > ) -> Poll < Result < ( ) , Self :: Error > > {
135144 self . inner . poll_ready ( cx)
136145 }
137146
138- fn call ( & mut self , event : LambdaEvent < E > ) -> Self :: Future {
147+ fn call ( & mut self , event : LambdaEvent < Value > ) -> Self :: Future {
139148 let provider = self . provider . clone ( ) ;
140149 let config = Arc :: clone ( & self . config ) ;
141150 let scope = start_invocation ( & event, & provider, & config) ;
142- let fut = self . inner . call ( event) ;
151+ let typed_payload = match serde_json:: from_value :: < E > ( event. payload ) {
152+ Ok ( p) => p,
153+ Err ( e) => return Box :: pin ( async move { Err ( e. into ( ) ) } ) ,
154+ } ;
155+ let typed_event = LambdaEvent :: new ( typed_payload, event. context ) ;
156+ let fut = self . inner . call ( typed_event) ;
143157 Box :: pin ( async move { run_in_invocation_scope ( scope, provider, fut) . await } )
144158 }
145159}
0 commit comments