5
5
import com .amazonaws .services .lambda .runtime .Context ;
6
6
import io .lumigo .core .configuration .Configuration ;
7
7
import io .lumigo .core .network .Reporter ;
8
- import io .lumigo .core .parsers .AwsParserFactory ;
9
8
import io .lumigo .core .parsers .event .EventParserFactory ;
9
+ import io .lumigo .core .parsers .v1 .AwsSdkV1ParserFactory ;
10
+ import io .lumigo .core .parsers .v2 .AwsSdkV2ParserFactory ;
10
11
import io .lumigo .core .utils .AwsUtils ;
11
12
import io .lumigo .core .utils .JsonUtils ;
12
13
import io .lumigo .core .utils .StringUtils ;
21
22
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
22
23
import org .apache .http .client .methods .HttpUriRequest ;
23
24
import 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 ;
24
30
25
31
public class SpansContainer {
26
32
@@ -354,14 +360,88 @@ public void addHttpSpan(Long startTime, Request<?> request, Response<?> response
354
360
response .getHttpResponse ().getStatusCode ())
355
361
.build ())
356
362
.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
+
358
434
httpSpans .add (httpSpan );
359
435
}
360
436
361
437
private static String extractHeaders (Map <String , String > headers ) {
362
438
return JsonUtils .getObjectAsJsonString (headers );
363
439
}
364
440
441
+ private static String extractHeadersV2 (Map <String , List <String >> headers ) {
442
+ return JsonUtils .getObjectAsJsonString (headers );
443
+ }
444
+
365
445
private static String extractHeaders (Header [] headers ) {
366
446
Map <String , String > headersMap = new HashMap <>();
367
447
if (headers != null ) {
@@ -373,27 +453,31 @@ private static String extractHeaders(Header[] headers) {
373
453
}
374
454
375
455
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 );
377
465
}
378
466
379
467
protected static String extractBodyFromRequest (HttpUriRequest request ) throws Exception {
380
468
if (request instanceof HttpEntityEnclosingRequestBase ) {
381
469
HttpEntity entity = ((HttpEntityEnclosingRequestBase ) request ).getEntity ();
382
470
if (entity != null ) {
383
- return extractBodyFromRequest (entity .getContent ());
471
+ return extractBodyFromStream (entity .getContent ());
384
472
}
385
473
}
386
474
return null ;
387
475
}
388
476
389
- protected static String extractBodyFromRequest (InputStream stream ) {
390
- return StringUtils .extractStringForStream (stream , MAX_STRING_SIZE );
391
- }
392
-
393
477
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 ;
397
481
}
398
482
399
483
protected static String extractBodyFromResponse (Response response ) {
@@ -402,6 +486,17 @@ protected static String extractBodyFromResponse(Response response) {
402
486
: null ;
403
487
}
404
488
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
+
405
500
public String getPatchedRoot () {
406
501
return String .format (
407
502
"Root=%s-0000%s-%s%s" ,
0 commit comments