13
13
import io .lumigo .core .utils .JsonUtils ;
14
14
import io .lumigo .core .utils .SecretScrubber ;
15
15
import io .lumigo .core .utils .StringUtils ;
16
+ import io .lumigo .models .*;
16
17
import io .lumigo .models .HttpSpan ;
17
- import io .lumigo .models .Reportable ;
18
18
import io .lumigo .models .Span ;
19
19
import java .io .*;
20
20
import java .util .*;
21
21
import java .util .concurrent .Callable ;
22
+ import lombok .Getter ;
22
23
import org .apache .http .Header ;
23
24
import org .apache .http .HttpEntity ;
24
25
import org .apache .http .HttpResponse ;
25
26
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
26
27
import org .apache .http .client .methods .HttpUriRequest ;
28
+ import org .apache .kafka .clients .consumer .ConsumerRecords ;
29
+ import org .apache .kafka .clients .consumer .KafkaConsumer ;
30
+ import org .apache .kafka .clients .consumer .internals .ConsumerMetadata ;
31
+ import org .apache .kafka .clients .producer .ProducerRecord ;
32
+ import org .apache .kafka .clients .producer .RecordMetadata ;
33
+ import org .apache .kafka .clients .producer .internals .ProducerMetadata ;
34
+ import org .apache .kafka .common .serialization .Serializer ;
27
35
import org .pmw .tinylog .Logger ;
28
36
import software .amazon .awssdk .awscore .AwsResponse ;
29
37
import software .amazon .awssdk .core .SdkResponse ;
@@ -41,14 +49,16 @@ public class SpansContainer {
41
49
private static final String AMZN_TRACE_ID = "_X_AMZN_TRACE_ID" ;
42
50
private static final String FUNCTION_SPAN_TYPE = "function" ;
43
51
private static final String HTTP_SPAN_TYPE = "http" ;
44
- private static final SecretScrubber secretScrubber = new SecretScrubber ( new EnvUtil (). getEnv ()) ;
52
+ public static final String KAFKA_SPAN_TYPE = "kafka" ;
45
53
46
54
private Span baseSpan ;
47
- private Span startFunctionSpan ;
55
+ @ Getter private Span startFunctionSpan ;
48
56
private Long rttDuration ;
49
57
private Span endFunctionSpan ;
50
58
private Reporter reporter ;
51
- private List <HttpSpan > httpSpans = new LinkedList <>();
59
+ private SecretScrubber secretScrubber = new SecretScrubber (new EnvUtil ().getEnv ());
60
+ @ Getter private List <BaseSpan > spans = new LinkedList <>();
61
+
52
62
private static final SpansContainer ourInstance = new SpansContainer ();
53
63
54
64
public static SpansContainer getInstance () {
@@ -63,14 +73,15 @@ public void clear() {
63
73
rttDuration = null ;
64
74
endFunctionSpan = null ;
65
75
reporter = null ;
66
- httpSpans = new LinkedList <>();
76
+ spans = new LinkedList <>();
67
77
}
68
78
69
79
private SpansContainer () {}
70
80
71
81
public void init (Map <String , String > env , Reporter reporter , Context context , Object event ) {
72
82
this .clear ();
73
83
this .reporter = reporter ;
84
+ this .secretScrubber = new SecretScrubber (new EnvUtil ().getEnv ());
74
85
75
86
int javaVersion = AwsUtils .parseJavaVersion (System .getProperty ("java.version" ));
76
87
if (javaVersion > 11 ) {
@@ -81,6 +92,7 @@ public void init(Map<String, String> env, Reporter reporter, Context context, Ob
81
92
Logger .debug ("awsTracerId {}" , awsTracerId );
82
93
83
94
AwsUtils .TriggeredBy triggeredBy = AwsUtils .extractTriggeredByFromEvent (event );
95
+
84
96
long startTime = System .currentTimeMillis ();
85
97
this .baseSpan =
86
98
Span .builder ()
@@ -166,8 +178,7 @@ public void start() {
166
178
.build ();
167
179
168
180
try {
169
- rttDuration =
170
- reporter .reportSpans (prepareToSend (startFunctionSpan , false ), MAX_REQUEST_SIZE );
181
+ rttDuration = reporter .reportSpans (prepareToSend (startFunctionSpan ), MAX_REQUEST_SIZE );
171
182
} catch (Throwable e ) {
172
183
Logger .error (e , "Failed to send start span" );
173
184
}
@@ -214,25 +225,17 @@ private void end(Span endFunctionSpan) throws IOException {
214
225
MAX_REQUEST_SIZE );
215
226
}
216
227
217
- public Span getStartFunctionSpan () {
218
- return startFunctionSpan ;
219
- }
220
-
221
- public List <Reportable > getAllCollectedSpans () {
222
- List <Reportable > spans = new LinkedList <>();
228
+ public List <BaseSpan > getAllCollectedSpans () {
229
+ List <BaseSpan > spans = new LinkedList <>();
223
230
spans .add (endFunctionSpan );
224
- spans .addAll (httpSpans );
231
+ spans .addAll (this . spans );
225
232
return spans ;
226
233
}
227
234
228
235
public Span getEndSpan () {
229
236
return endFunctionSpan ;
230
237
}
231
238
232
- public List <HttpSpan > getHttpSpans () {
233
- return httpSpans ;
234
- }
235
-
236
239
private String getStackTrace (Throwable throwable ) {
237
240
StringWriter sw = new StringWriter ();
238
241
PrintWriter pw = new PrintWriter (sw , true );
@@ -307,7 +310,7 @@ public void addHttpSpan(Long startTime, HttpUriRequest request, HttpResponse res
307
310
response .getStatusLine ().getStatusCode ())
308
311
.build ())
309
312
.build ());
310
- httpSpans .add (httpSpan );
313
+ this . spans .add (httpSpan );
311
314
}
312
315
313
316
public void addHttpSpan (Long startTime , Request <?> request , Response <?> response ) {
@@ -366,7 +369,7 @@ public void addHttpSpan(Long startTime, Request<?> request, Response<?> response
366
369
.build ());
367
370
AwsSdkV1ParserFactory .getParser (request .getServiceName ())
368
371
.safeParse (httpSpan , request , response );
369
- httpSpans .add (httpSpan );
372
+ this . spans .add (httpSpan );
370
373
}
371
374
372
375
public void addHttpSpan (
@@ -435,7 +438,37 @@ public void addHttpSpan(
435
438
executionAttributes .getAttribute (SdkExecutionAttribute .SERVICE_NAME ))
436
439
.safeParse (httpSpan , context );
437
440
438
- httpSpans .add (httpSpan );
441
+ this .spans .add (httpSpan );
442
+ }
443
+
444
+ public <K , V > void addKafkaProduceSpan (
445
+ Long startTime ,
446
+ Serializer <K > keySerializer ,
447
+ Serializer <V > valueSerializer ,
448
+ ProducerMetadata producerMetadata ,
449
+ ProducerRecord <K , V > record ,
450
+ RecordMetadata recordMetadata ,
451
+ Exception exception ) {
452
+ this .spans .add (
453
+ KafkaSpanFactory .createProduce (
454
+ this .baseSpan ,
455
+ startTime ,
456
+ keySerializer ,
457
+ valueSerializer ,
458
+ producerMetadata ,
459
+ record ,
460
+ recordMetadata ,
461
+ exception ));
462
+ }
463
+
464
+ public void addKafkaConsumeSpan (
465
+ Long startTime ,
466
+ KafkaConsumer <?, ?> consumer ,
467
+ ConsumerMetadata consumerMetadata ,
468
+ ConsumerRecords <?, ?> consumerRecords ) {
469
+ this .spans .add (
470
+ KafkaSpanFactory .createConsume (
471
+ this .baseSpan , startTime , consumer , consumerMetadata , consumerRecords ));
439
472
}
440
473
441
474
private static String extractHeaders (Map <String , String > headers ) {
@@ -522,18 +555,18 @@ protected static <T> T callIfVerbose(Callable<T> method) {
522
555
}
523
556
}
524
557
525
- private Reportable prepareToSend (Reportable span , boolean hasError ) {
526
- return reduceSpanSize (span .scrub (secretScrubber ), hasError );
558
+ private BaseSpan prepareToSend (BaseSpan span ) {
559
+ return reduceSpanSize (span .scrub (secretScrubber ), false );
527
560
}
528
561
529
- private List <Reportable > prepareToSend (List <Reportable > spans , boolean hasError ) {
530
- for (Reportable span : spans ) {
562
+ private List <BaseSpan > prepareToSend (List <BaseSpan > spans , boolean hasError ) {
563
+ for (BaseSpan span : spans ) {
531
564
reduceSpanSize (span .scrub (secretScrubber ), hasError );
532
565
}
533
566
return spans ;
534
567
}
535
568
536
- public Reportable reduceSpanSize (Reportable span , boolean hasError ) {
569
+ public BaseSpan reduceSpanSize (BaseSpan span , boolean hasError ) {
537
570
int maxFieldSize =
538
571
hasError
539
572
? Configuration .getInstance ().maxSpanFieldSizeWhenError ()
0 commit comments