1212import static java .util .Arrays .asList ;
1313import static org .assertj .core .api .Assertions .assertThat ;
1414
15- import groovy .lang .MissingMethodException ;
1615import io .opentelemetry .api .trace .Span ;
1716import io .opentelemetry .api .trace .SpanContext ;
1817import io .opentelemetry .api .trace .SpanKind ;
2221import io .opentelemetry .sdk .testing .assertj .AttributeAssertion ;
2322import io .opentelemetry .sdk .trace .data .SpanData ;
2423import io .opentelemetry .semconv .incubating .MessagingIncubatingAttributes ;
25- import java .lang .reflect .Constructor ;
2624import java .lang .reflect .InvocationTargetException ;
27- import java .lang .reflect .Method ;
2825import java .nio .charset .StandardCharsets ;
2926import java .time .Duration ;
3027import java .util .Collections ;
3835import org .apache .kafka .clients .consumer .ConsumerRecords ;
3936import org .apache .kafka .clients .producer .ProducerRecord ;
4037import org .apache .kafka .common .header .Headers ;
41- import org .apache .kafka .common .serialization .Serde ;
4238import org .apache .kafka .common .serialization .Serdes ;
4339import org .apache .kafka .streams .KafkaStreams ;
4440import org .apache .kafka .streams .StreamsConfig ;
4541import org .apache .kafka .streams .kstream .KStream ;
46- import org .apache .kafka .streams .processor .TopologyBuilder ;
47- import org .jetbrains .annotations .NotNull ;
4842import org .junit .jupiter .api .DisplayName ;
4943import org .junit .jupiter .api .Test ;
5044
5145class KafkaStreamsSuppressReceiveSpansTest extends KafkaStreamsBaseTest {
5246
53- @ SuppressWarnings ("ClassNewInstance" )
54- private static @ NotNull Object createBuilder ()
55- throws InstantiationException , IllegalAccessException , ClassNotFoundException {
56- Object builder ;
57- try {
58- // Different class names for test and latestDepTest.
59- builder = Class .forName ("org.apache.kafka.streams.kstream.KStreamBuilder" ).newInstance ();
60- } catch (ClassNotFoundException | NoClassDefFoundError e ) {
61- builder = Class .forName ("org.apache.kafka.streams.StreamsBuilder" ).newInstance ();
62- }
63- return builder ;
64- }
65-
66- @ SuppressWarnings ("unchecked" )
67- private static KStream <Integer , String > stream (Object builder )
68- throws IllegalAccessException ,
69- InvocationTargetException ,
70- NoSuchMethodException ,
71- ClassNotFoundException {
72- Method streamMethod ;
73- try {
74- Class .forName ("org.apache.kafka.streams.kstream.KStreamBuilder" );
75- return ((org .apache .kafka .streams .kstream .KStreamBuilder ) builder ).stream (STREAM_PENDING );
76- } catch (ClassNotFoundException e ) {
77- streamMethod =
78- Class .forName ("org.apache.kafka.streams.StreamsBuilder" )
79- .getMethod ("stream" , String .class );
80- return (KStream <Integer , String >) streamMethod .invoke (builder , STREAM_PENDING );
81- }
82- }
83-
8447 @ DisplayName ("test kafka produce and consume with streams in-between" )
8548 @ Test
8649 void testKafkaProduceAndConsumeWithStreamsInBetween ()
@@ -100,16 +63,17 @@ void testKafkaProduceAndConsumeWithStreamsInBetween()
10063 StreamsConfig .DEFAULT_VALUE_SERDE_CLASS_CONFIG , Serdes .String ().getClass ().getName ());
10164
10265 // CONFIGURE PROCESSOR
103- Object builder = createBuilder ();
104- KStream <Integer , String > textLines = stream (builder );
66+ Object builder = KafkaStreamReflectionUtil . createBuilder ();
67+ KStream <Integer , String > textLines = KafkaStreamReflectionUtil . stream (builder , STREAM_PENDING );
10568 KStream <Integer , String > values =
10669 textLines .mapValues (
10770 textLine -> {
10871 Span .current ().setAttribute ("asdf" , "testing" );
10972 return textLine .toLowerCase (Locale .ROOT );
11073 });
11174
112- KafkaStreams streams = createStreams (builder , values , config );
75+ KafkaStreams streams =
76+ KafkaStreamReflectionUtil .createStreams (builder , values , config , STREAM_PROCESSED );
11377 streams .start ();
11478
11579 String greeting = "TESTING TESTING 123!" ;
@@ -275,41 +239,4 @@ public Iterable<String> keys(String carrier) {
275239 assertThat (spanContext .getTraceId ()).isEqualTo (streamSendSpan .getTraceId ());
276240 assertThat (spanContext .getSpanId ()).isEqualTo (streamSendSpan .getSpanId ());
277241 }
278-
279- private static KafkaStreams createStreams (
280- Object builder , KStream <Integer , String > values , Properties config )
281- throws ClassNotFoundException ,
282- NoSuchMethodException ,
283- InvocationTargetException ,
284- IllegalAccessException ,
285- InstantiationException {
286- try {
287- // Different api for test and latestDepTest.
288- values .to (Serdes .Integer (), Serdes .String (), STREAM_PROCESSED );
289- return new KafkaStreams ((TopologyBuilder ) builder , config );
290- } catch (MissingMethodException e ) {
291- // equivalent to:
292- // Produced<Integer, String> produced = Produced.with(Serdes.Integer(), Serdes.String());
293- // values.to(STREAM_PROCESSED, produced);
294- //
295- // Topology topology = builder.build();
296- // new KafkaStreams(topology, props);
297- Class <?> producedClass = Class .forName ("org.apache.kafka.streams.kstream.Produced" );
298- Method producedWith = producedClass .getMethod ("with" , Serde .class , Serde .class );
299- Object producer = producedWith .invoke (null , Serdes .Integer (), Serdes .String ());
300-
301- Class <?> ksteamClass = Class .forName ("org.apache.kafka.streams.kstream.KStream" );
302- ksteamClass
303- .getMethod ("to" , String .class , producedClass )
304- .invoke (values , STREAM_PROCESSED , producer );
305-
306- Class <?> streamsBuilderClass = Class .forName ("org.apache.kafka.streams.StreamsBuilder" );
307- Object topology = streamsBuilderClass .getMethod ("build" ).invoke (builder );
308-
309- Class <?> ksteamsClass = Class .forName ("org.apache.kafka.streams.KStreams" );
310- Class <?> topologyClass = Class .forName ("org.apache.kafka.streams.Topology" );
311- Constructor <?> constructor = ksteamsClass .getConstructor (topologyClass , Properties .class );
312- return (KafkaStreams ) constructor .newInstance (topology , config );
313- }
314- }
315242}
0 commit comments