55import com .amazonaws .services .lambda .runtime .Context ;
66import io .lumigo .core .configuration .Configuration ;
77import io .lumigo .core .network .Reporter ;
8- import io .lumigo .core .parsers .AwsParserFactory ;
98import io .lumigo .core .parsers .event .EventParserFactory ;
9+ import io .lumigo .core .parsers .v1 .AwsSdkV1ParserFactory ;
10+ import io .lumigo .core .parsers .v2 .AwsSdkV2ParserFactory ;
1011import io .lumigo .core .utils .AwsUtils ;
1112import io .lumigo .core .utils .JsonUtils ;
1213import io .lumigo .core .utils .StringUtils ;
2122import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
2223import org .apache .http .client .methods .HttpUriRequest ;
2324import org .pmw .tinylog .Logger ;
25+ import software .amazon .awssdk .awscore .AwsResponse ;
26+ import software .amazon .awssdk .core .SdkResponse ;
27+ import software .amazon .awssdk .core .interceptor .ExecutionAttributes ;
28+ import software .amazon .awssdk .core .interceptor .SdkExecutionAttribute ;
29+ import software .amazon .awssdk .core .sync .RequestBody ;
2430
2531public class SpansContainer {
2632
@@ -354,14 +360,88 @@ public void addHttpSpan(Long startTime, Request<?> request, Response<?> response
354360 response .getHttpResponse ().getStatusCode ())
355361 .build ())
356362 .build ());
357- AwsParserFactory .getParser (request .getServiceName ()).parse (httpSpan , request , response );
363+ AwsSdkV1ParserFactory .getParser (request .getServiceName ())
364+ .safeParse (httpSpan , request , response );
365+ httpSpans .add (httpSpan );
366+ }
367+
368+ public void addHttpSpan (
369+ Long startTime ,
370+ final software .amazon .awssdk .core .interceptor .Context .AfterExecution context ,
371+ final ExecutionAttributes executionAttributes ) {
372+ HttpSpan httpSpan = createBaseHttpSpan (startTime );
373+ String spanId = null ;
374+ for (Map .Entry <String , List <String >> header : context .httpResponse ().headers ().entrySet ()) {
375+ if ("x-amzn-requestid" .equalsIgnoreCase (header .getKey ())
376+ || "x-amz-requestid" .equalsIgnoreCase (header .getKey ())) {
377+ spanId = header .getValue ().get (0 );
378+ }
379+ }
380+ if (spanId != null ) {
381+ httpSpan .setId (spanId );
382+ }
383+ httpSpan .getInfo ()
384+ .setHttpInfo (
385+ HttpSpan .HttpInfo .builder ()
386+ .host (context .httpRequest ().getUri ().getHost ())
387+ .request (
388+ HttpSpan .HttpData .builder ()
389+ .headers (
390+ callIfVerbose (
391+ () ->
392+ extractHeadersV2 (
393+ context .httpRequest ()
394+ .headers ())))
395+ .uri (
396+ callIfVerbose (
397+ () ->
398+ context .httpRequest ()
399+ .getUri ()
400+ .toString ()))
401+ .method (context .httpRequest ().method ().name ())
402+ .body (
403+ callIfVerbose (
404+ () ->
405+ extractBodyFromRequest (
406+ context
407+ .requestBody ())))
408+ .build ())
409+ .response (
410+ HttpSpan .HttpData .builder ()
411+ .headers (
412+ callIfVerbose (
413+ () ->
414+ extractHeadersV2 (
415+ context .httpResponse ()
416+ .headers ())))
417+ .body (
418+ callIfVerbose (
419+ () ->
420+ extractBodyFromResponse (
421+ context
422+ .response ())))
423+ .statusCode (context .httpResponse ().statusCode ())
424+ .build ())
425+ .build ());
426+
427+ Logger .debug (
428+ "Trying to extract aws custom properties for service: "
429+ + executionAttributes .getAttribute (SdkExecutionAttribute .SERVICE_NAME ));
430+ AwsSdkV2ParserFactory .getParser (
431+ executionAttributes .getAttribute (SdkExecutionAttribute .SERVICE_NAME ))
432+ .safeParse (httpSpan , context );
433+
358434 httpSpans .add (httpSpan );
359435 }
360436
361437 private static String extractHeaders (Map <String , String > headers ) {
362438 return JsonUtils .getObjectAsJsonString (headers );
363439 }
364440
441+ private static String extractHeadersV2 (Map <String , List <String >> headers ) {
442+ return JsonUtils .getObjectAsJsonString (headers );
443+ }
444+
365445 private static String extractHeaders (Header [] headers ) {
366446 Map <String , String > headersMap = new HashMap <>();
367447 if (headers != null ) {
@@ -373,27 +453,31 @@ private static String extractHeaders(Header[] headers) {
373453 }
374454
375455 protected static String extractBodyFromRequest (Request <?> request ) {
376- return extractBodyFromRequest (request .getContent ());
456+ return extractBodyFromStream (request .getContent ());
457+ }
458+
459+ protected static String extractBodyFromRequest (Optional <RequestBody > request ) {
460+ return request .map (
461+ requestBody ->
462+ extractBodyFromStream (
463+ requestBody .contentStreamProvider ().newStream ()))
464+ .orElse (null );
377465 }
378466
379467 protected static String extractBodyFromRequest (HttpUriRequest request ) throws Exception {
380468 if (request instanceof HttpEntityEnclosingRequestBase ) {
381469 HttpEntity entity = ((HttpEntityEnclosingRequestBase ) request ).getEntity ();
382470 if (entity != null ) {
383- return extractBodyFromRequest (entity .getContent ());
471+ return extractBodyFromStream (entity .getContent ());
384472 }
385473 }
386474 return null ;
387475 }
388476
389- protected static String extractBodyFromRequest (InputStream stream ) {
390- return StringUtils .extractStringForStream (stream , MAX_STRING_SIZE );
391- }
392-
393477 protected static String extractBodyFromResponse (HttpResponse response ) throws IOException {
394- return StringUtils . extractStringForStream (
395- response . getEntity () != null ? response .getEntity ().getContent () : null ,
396- MAX_STRING_SIZE ) ;
478+ return response . getEntity () != null
479+ ? extractBodyFromStream ( response .getEntity ().getContent ())
480+ : null ;
397481 }
398482
399483 protected static String extractBodyFromResponse (Response response ) {
@@ -402,6 +486,17 @@ protected static String extractBodyFromResponse(Response response) {
402486 : null ;
403487 }
404488
489+ protected static String extractBodyFromResponse (SdkResponse response ) {
490+ if (response instanceof AwsResponse ) {
491+ return JsonUtils .getObjectAsJsonString (response .toBuilder ());
492+ }
493+ return null ;
494+ }
495+
496+ protected static String extractBodyFromStream (InputStream stream ) {
497+ return StringUtils .extractStringForStream (stream , MAX_STRING_SIZE );
498+ }
499+
405500 public String getPatchedRoot () {
406501 return String .format (
407502 "Root=%s-0000%s-%s%s" ,
0 commit comments