1818
1919import static org .assertj .core .api .Assertions .assertThat ;
2020import static org .awaitility .Awaitility .await ;
21+ import static org .mockito .Mockito .mock ;
2122
2223import java .util .Arrays ;
2324import java .util .Deque ;
4849import org .springframework .kafka .core .KafkaAdmin ;
4950import org .springframework .kafka .core .KafkaTemplate ;
5051import org .springframework .kafka .core .ProducerFactory ;
52+ import org .springframework .kafka .listener .AbstractMessageListenerContainer ;
5153import org .springframework .kafka .support .micrometer .KafkaListenerObservation .DefaultKafkaListenerObservationConvention ;
5254import org .springframework .kafka .support .micrometer .KafkaTemplateObservation .DefaultKafkaTemplateObservationConvention ;
5355import org .springframework .kafka .test .EmbeddedKafkaBroker ;
8183 *
8284 */
8385@ SpringJUnitConfig
84- @ EmbeddedKafka (topics = { "observation.testT1" , "observation.testT2" })
86+ @ EmbeddedKafka (topics = { "observation.testT1" , "observation.testT2" , "ObservationTests.testT3" })
8587@ DirtiesContext
8688public class ObservationTests {
8789
8890 @ Test
8991 void endToEnd (@ Autowired Listener listener , @ Autowired KafkaTemplate <Integer , String > template ,
9092 @ Autowired SimpleTracer tracer , @ Autowired KafkaListenerEndpointRegistry rler ,
9193 @ Autowired MeterRegistry meterRegistry , @ Autowired EmbeddedKafkaBroker broker ,
92- @ Autowired KafkaListenerEndpointRegistry endpointRegistry , @ Autowired KafkaAdmin admin )
94+ @ Autowired KafkaListenerEndpointRegistry endpointRegistry , @ Autowired KafkaAdmin admin ,
95+ @ Autowired KafkaTemplate <Integer , String > customTemplate , @ Autowired Config config )
9396 throws InterruptedException , ExecutionException , TimeoutException {
9497
9598 template .send ("observation.testT1" , "test" ).get (10 , TimeUnit .SECONDS );
@@ -186,9 +189,12 @@ public KeyValues getLowCardinalityKeyValues(KafkaRecordReceiverContext context)
186189 assertThat (pAdmin .getConfigurationProperties ())
187190 .containsEntry (AdminClientConfig .BOOTSTRAP_SERVERS_CONFIG ,
188191 broker .getBrokersAsString () + "," + broker .getBrokersAsString ());
192+ // custom admin
193+ assertThat (customTemplate .getKafkaAdmin ()).isSameAs (config .mockAdmin );
194+
195+ // consumer factory broker different to admin
189196 Object container = KafkaTestUtils
190197 .getPropertyValue (endpointRegistry .getListenerContainer ("obs1" ), "containers" , List .class ).get (0 );
191- // consumer factory broker different to admin
192198 KafkaAdmin cAdmin = KafkaTestUtils .getPropertyValue (container , "listenerConsumer.kafkaAdmin" , KafkaAdmin .class );
193199 assertThat (cAdmin .getOperationTimeout ()).isEqualTo (admin .getOperationTimeout ());
194200 assertThat (cAdmin .getConfigurationProperties ())
@@ -202,12 +208,19 @@ public KeyValues getLowCardinalityKeyValues(KafkaRecordReceiverContext context)
202208 assertThat (cAdmin .getOperationTimeout ()).isEqualTo (admin .getOperationTimeout ());
203209 assertThat (cAdmin .getConfigurationProperties ())
204210 .containsEntry (AdminClientConfig .BOOTSTRAP_SERVERS_CONFIG , broker .getBrokersAsString ());
211+ // custom admin
212+ container = KafkaTestUtils
213+ .getPropertyValue (endpointRegistry .getListenerContainer ("obs3" ), "containers" , List .class ).get (0 );
214+ cAdmin = KafkaTestUtils .getPropertyValue (container , "listenerConsumer.kafkaAdmin" , KafkaAdmin .class );
215+ assertThat (cAdmin ).isSameAs (config .mockAdmin );
205216 }
206217
207218 @ Configuration
208219 @ EnableKafka
209220 public static class Config {
210221
222+ KafkaAdmin mockAdmin = mock (KafkaAdmin .class );
223+
211224 @ Bean
212225 KafkaAdmin admin (EmbeddedKafkaBroker broker ) {
213226 KafkaAdmin admin = new KafkaAdmin (
@@ -239,6 +252,14 @@ KafkaTemplate<Integer, String> template(ProducerFactory<Integer, String> pf) {
239252 return template ;
240253 }
241254
255+ @ Bean
256+ KafkaTemplate <Integer , String > customTemplate (ProducerFactory <Integer , String > pf ) {
257+ KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
258+ template .setObservationEnabled (true );
259+ template .setKafkaAdmin (this .mockAdmin );
260+ return template ;
261+ }
262+
242263 @ Bean
243264 ConcurrentKafkaListenerContainerFactory <Integer , String > kafkaListenerContainerFactory (
244265 ConsumerFactory <Integer , String > cf ) {
@@ -247,6 +268,11 @@ ConcurrentKafkaListenerContainerFactory<Integer, String> kafkaListenerContainerF
247268 new ConcurrentKafkaListenerContainerFactory <>();
248269 factory .setConsumerFactory (cf );
249270 factory .getContainerProperties ().setObservationEnabled (true );
271+ factory .setContainerCustomizer (container -> {
272+ if (container .getListenerId ().equals ("obs3" )) {
273+ ((AbstractMessageListenerContainer <Integer , String >) container ).setKafkaAdmin (this .mockAdmin );
274+ }
275+ });
250276 return factory ;
251277 }
252278
@@ -339,6 +365,10 @@ void listen2(ConsumerRecord<?, ?> in) {
339365 this .latch2 .countDown ();
340366 }
341367
368+ @ KafkaListener (id = "obs3" , topics = "observation.testT3" )
369+ void listen3 (ConsumerRecord <Integer , String > in ) {
370+ }
371+
342372 }
343373
344374}
0 commit comments