@@ -109,7 +109,7 @@ pub struct EventContext {
109109 /// The AWS request ID generated by the Lambda service.
110110 pub aws_request_id : String ,
111111 /// The X-Ray trace ID for the current invocation.
112- pub xray_trace_id : String ,
112+ pub xray_trace_id : Option < String > ,
113113 /// The execution deadline for the current invocation in milliseconds.
114114 pub deadline : i64 ,
115115 /// The client context object sent by the AWS mobile SDK. This field is
@@ -404,7 +404,17 @@ impl<'ev> RuntimeClient {
404404 headers. get ( LambdaHeaders :: FunctionArn . as_str ( ) ) ,
405405 & LambdaHeaders :: FunctionArn ,
406406 ) ?;
407- let xray_trace_id = header_string ( headers. get ( LambdaHeaders :: TraceId . as_str ( ) ) , & LambdaHeaders :: TraceId ) ?;
407+ let xray_trace_id = match headers. get ( LambdaHeaders :: TraceId . as_str ( ) ) {
408+ Some ( trace_id) => match trace_id. to_str ( ) {
409+ Ok ( trace_str) => Some ( trace_str. to_owned ( ) ) ,
410+ Err ( e) => {
411+ // we do not fail on this error.
412+ error ! ( "Could not parse X-Ray trace id as string: {}" , e) ;
413+ None
414+ }
415+ } ,
416+ None => None ,
417+ } ;
408418 let deadline = header_string ( headers. get ( LambdaHeaders :: Deadline . as_str ( ) ) , & LambdaHeaders :: Deadline ) ?
409419 . parse :: < i64 > ( )
410420 . context ( ApiErrorKind :: Recoverable (
@@ -461,3 +471,49 @@ fn header_string(value: Option<&HeaderValue>, header_type: &LambdaHeaders) -> Re
461471 }
462472 }
463473}
474+
475+ #[ cfg( test) ]
476+ pub ( crate ) mod tests {
477+ use super :: * ;
478+ use chrono:: { Duration , Utc } ;
479+
480+ fn get_headers ( ) -> HeaderMap < HeaderValue > {
481+ let mut headers: HeaderMap < HeaderValue > = HeaderMap :: new ( ) ;
482+ headers. insert (
483+ LambdaHeaders :: RequestId . as_str ( ) ,
484+ HeaderValue :: from_str ( "req_id" ) . unwrap ( ) ,
485+ ) ;
486+ headers. insert (
487+ LambdaHeaders :: FunctionArn . as_str ( ) ,
488+ HeaderValue :: from_str ( "func_arn" ) . unwrap ( ) ,
489+ ) ;
490+ headers. insert ( LambdaHeaders :: TraceId . as_str ( ) , HeaderValue :: from_str ( "trace" ) . unwrap ( ) ) ;
491+ let deadline = Utc :: now ( ) + Duration :: seconds ( 10 ) ;
492+ headers. insert (
493+ LambdaHeaders :: Deadline . as_str ( ) ,
494+ HeaderValue :: from_str ( & deadline. timestamp_millis ( ) . to_string ( ) ) . unwrap ( ) ,
495+ ) ;
496+ headers
497+ }
498+
499+ #[ test]
500+ fn get_event_context_with_empty_trace_id ( ) {
501+ let client = RuntimeClient :: new ( "localhost:8081" , None , None ) . expect ( "Could not initialize runtime client" ) ;
502+ let mut headers = get_headers ( ) ;
503+ headers. remove ( LambdaHeaders :: TraceId . as_str ( ) ) ;
504+ let headers_result = client. get_event_context ( & headers) ;
505+ assert_eq ! ( false , headers_result. is_err( ) ) ;
506+ let ok_result = headers_result. unwrap ( ) ;
507+ assert_eq ! ( None , ok_result. xray_trace_id) ;
508+ assert_eq ! ( "req_id" , ok_result. aws_request_id) ;
509+ }
510+
511+ #[ test]
512+ fn get_event_context_populates_trace_id_when_present ( ) {
513+ let client = RuntimeClient :: new ( "localhost:8081" , None , None ) . expect ( "Could not initialize runtime client" ) ;
514+ let headers = get_headers ( ) ;
515+ let headers_result = client. get_event_context ( & headers) ;
516+ assert_eq ! ( false , headers_result. is_err( ) ) ;
517+ assert_eq ! ( Some ( "trace" . to_owned( ) ) , headers_result. unwrap( ) . xray_trace_id) ;
518+ }
519+ }
0 commit comments