3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- package io .opentelemetry .instrumentation .kafkaclients .v2_6 ;
6
+ package io .opentelemetry .instrumentation .kafkaclients .v2_6 . internal ;
7
7
8
8
import static org .assertj .core .api .Assertions .assertThat ;
9
9
import static org .assertj .core .api .Assertions .assertThatThrownBy ;
10
10
11
- import io .opentelemetry .instrumentation .kafkaclients .v2_6 .internal .KafkaConsumerTelemetrySupplier ;
12
- import io .opentelemetry .instrumentation .kafkaclients .v2_6 .internal .KafkaProducerTelemetrySupplier ;
13
- import io .opentelemetry .instrumentation .kafkaclients .v2_6 .internal .OpenTelemetryConsumerInterceptor ;
14
- import io .opentelemetry .instrumentation .kafkaclients .v2_6 .internal .OpenTelemetryProducerInterceptor ;
11
+ import io .opentelemetry .instrumentation .kafkaclients .v2_6 .KafkaTelemetry ;
15
12
import io .opentelemetry .instrumentation .testing .junit .InstrumentationExtension ;
16
13
import io .opentelemetry .instrumentation .testing .junit .LibraryInstrumentationExtension ;
17
14
import java .io .ByteArrayInputStream ;
26
23
import java .util .function .Supplier ;
27
24
import org .apache .kafka .clients .consumer .ConsumerConfig ;
28
25
import org .apache .kafka .clients .consumer .KafkaConsumer ;
29
- import org .apache .kafka .clients .producer .KafkaProducer ;
30
- import org .apache .kafka .clients .producer .ProducerConfig ;
31
26
import org .apache .kafka .common .serialization .StringDeserializer ;
32
- import org .apache .kafka .common .serialization .StringSerializer ;
33
27
import org .junit .jupiter .api .Assumptions ;
34
28
import org .junit .jupiter .api .Test ;
35
29
import org .junit .jupiter .api .extension .RegisterExtension ;
36
30
37
- class KafkaTelemetryInterceptorTest {
31
+ class OpenTelemetryConsumerInterceptorTest {
38
32
39
33
@ RegisterExtension
40
34
static final InstrumentationExtension testing = LibraryInstrumentationExtension .create ();
41
35
42
- private static Map <String , Object > producerConfig () {
43
- Map <String , Object > config = new HashMap <>();
44
- config .put (ProducerConfig .BOOTSTRAP_SERVERS_CONFIG , "localhost:9092" );
45
- config .put (ProducerConfig .KEY_SERIALIZER_CLASS_CONFIG , StringSerializer .class .getName ());
46
- config .put (ProducerConfig .VALUE_SERIALIZER_CLASS_CONFIG , StringSerializer .class .getName ());
47
- config .putAll (
48
- KafkaTelemetry .create (testing .getOpenTelemetry ()).producerInterceptorConfigProperties ());
49
- return config ;
50
- }
51
-
52
36
private static Map <String , Object > consumerConfig () {
53
37
Map <String , Object > config = new HashMap <>();
54
38
config .put (ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG , "localhost:9092" );
@@ -61,37 +45,7 @@ private static Map<String, Object> consumerConfig() {
61
45
}
62
46
63
47
@ Test
64
- void badProducerConfig () {
65
- Assumptions .assumeFalse (Boolean .getBoolean ("testLatestDeps" ));
66
-
67
- // Bad config - wrong type for supplier
68
- assertThatThrownBy (
69
- () -> {
70
- Map <String , Object > producerConfig = producerConfig ();
71
- producerConfig .put (
72
- OpenTelemetryProducerInterceptor .CONFIG_KEY_KAFKA_PRODUCER_TELEMETRY_SUPPLIER , "foo" );
73
- new KafkaProducer <>(producerConfig ).close ();
74
- })
75
- .hasRootCauseInstanceOf (IllegalStateException .class )
76
- .hasRootCauseMessage (
77
- "Configuration property opentelemetry.kafka-producer-telemetry.supplier is not instance of KafkaProducerTelemetrySupplier" );
78
-
79
- // Bad config - supplier returns wrong type
80
- assertThatThrownBy (
81
- () -> {
82
- Map <String , Object > producerConfig = producerConfig ();
83
- producerConfig .put (
84
- OpenTelemetryProducerInterceptor .CONFIG_KEY_KAFKA_PRODUCER_TELEMETRY_SUPPLIER ,
85
- (Supplier <?>) () -> "not a KafkaProducerTelemetry" );
86
- new KafkaProducer <>(producerConfig ).close ();
87
- })
88
- .hasRootCauseInstanceOf (IllegalStateException .class )
89
- .hasRootCauseMessage (
90
- "Configuration property opentelemetry.kafka-producer-telemetry.supplier is not instance of KafkaProducerTelemetrySupplier" );
91
- }
92
-
93
- @ Test
94
- void badConsumerConfig () {
48
+ void badConfig () {
95
49
Assumptions .assumeFalse (Boolean .getBoolean ("testLatestDeps" ));
96
50
97
51
// Bad config - wrong type for supplier
@@ -122,27 +76,18 @@ void badConsumerConfig() {
122
76
123
77
@ Test
124
78
void serializableConfig () throws IOException , ClassNotFoundException {
125
- testSerialize (producerConfig ());
126
79
testSerialize (consumerConfig ());
127
80
}
128
81
129
82
@ SuppressWarnings ("unchecked" )
130
83
private static void testSerialize (Map <String , Object > map )
131
84
throws IOException , ClassNotFoundException {
132
- // Check that producer config has the supplier
133
- Object producerSupplier =
134
- map .get (OpenTelemetryProducerInterceptor .CONFIG_KEY_KAFKA_PRODUCER_TELEMETRY_SUPPLIER );
85
+ // Check that consumer config has the supplier
135
86
Object consumerSupplier =
136
87
map .get (OpenTelemetryConsumerInterceptor .CONFIG_KEY_KAFKA_CONSUMER_TELEMETRY_SUPPLIER );
137
88
138
- Supplier <?> supplier = null ;
139
- if (producerSupplier instanceof KafkaProducerTelemetrySupplier ) {
140
- supplier = (KafkaProducerTelemetrySupplier ) producerSupplier ;
141
- } else if (consumerSupplier instanceof KafkaConsumerTelemetrySupplier ) {
142
- supplier = (KafkaConsumerTelemetrySupplier ) consumerSupplier ;
143
- }
144
-
145
- assertThat (supplier ).isNotNull ();
89
+ assertThat (consumerSupplier ).isInstanceOf (KafkaConsumerTelemetrySupplier .class );
90
+ KafkaConsumerTelemetrySupplier supplier = (KafkaConsumerTelemetrySupplier ) consumerSupplier ;
146
91
assertThat (supplier .get ()).isNotNull ();
147
92
148
93
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream ();
@@ -169,8 +114,6 @@ protected Class<?> resolveClass(ObjectStreamClass desc)
169
114
try (ObjectInputStream inputStream =
170
115
new CustomObjectInputStream (new ByteArrayInputStream (byteOutputStream .toByteArray ()))) {
171
116
Map <String , Object > result = (Map <String , Object >) inputStream .readObject ();
172
- assertThat (result .get (OpenTelemetryProducerInterceptor .CONFIG_KEY_KAFKA_PRODUCER_TELEMETRY_SUPPLIER ))
173
- .isNull ();
174
117
assertThat (result .get (OpenTelemetryConsumerInterceptor .CONFIG_KEY_KAFKA_CONSUMER_TELEMETRY_SUPPLIER ))
175
118
.isNull ();
176
119
}
0 commit comments