From 34c4efb35c854801d5b675809187711cebefd618 Mon Sep 17 00:00:00 2001 From: ncheema Date: Mon, 10 Aug 2020 09:29:20 -0700 Subject: [PATCH] Fix micrometer configuration for multiBinder - In MultiBinder configuration, MeterRegistery is loaded in the outterContext, hence removing the conditional on MeterRegistry bean check - Fix checkstyle issues --- .../config/KafkaBinderConfiguration.java | 1 - .../KafkaBinderMeterRegistryTest.java | 106 ++++++++++++++++++ .../MultiBinderMeterRegistryTest.java | 70 ------------ 3 files changed, 106 insertions(+), 71 deletions(-) create mode 100644 spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/bootstrap/KafkaBinderMeterRegistryTest.java delete mode 100644 spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/bootstrap/MultiBinderMeterRegistryTest.java diff --git a/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java b/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java index 3ae6b6778..18f044161 100644 --- a/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java +++ b/spring-cloud-stream-binder-kafka/src/main/java/org/springframework/cloud/stream/binder/kafka/config/KafkaBinderConfiguration.java @@ -238,7 +238,6 @@ public class KafkaBinderConfiguration { } @ConditionalOnClass(name = "org.springframework.kafka.core.MicrometerConsumerListener") - @ConditionalOnBean(MeterRegistry.class) protected class KafkaMicrometer { @Bean diff --git a/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/bootstrap/KafkaBinderMeterRegistryTest.java b/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/bootstrap/KafkaBinderMeterRegistryTest.java new file mode 100644 index 000000000..3b677cdda --- /dev/null +++ b/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/bootstrap/KafkaBinderMeterRegistryTest.java @@ -0,0 +1,106 @@ +/* + * Copyright 2019-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.stream.binder.kafka.bootstrap; + +import java.util.function.Function; + +import io.micrometer.core.instrument.MeterRegistry; +import org.junit.ClassRule; +import org.junit.Test; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.kafka.test.rule.EmbeddedKafkaRule; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +/** + * @author Soby Chacko + */ +public class KafkaBinderMeterRegistryTest { + + @ClassRule + public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, 10); + + @Test + public void testMetricsWorkWithSingleBinder() { + ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SimpleApplication.class) + .web(WebApplicationType.NONE) + .run("--spring.cloud.stream.bindings.uppercase-in-0.destination=inputTopic", + "--spring.cloud.stream.bindings.uppercase-in-0.group=inputGroup", + "--spring.cloud.stream.bindings.uppercase-in-0.binder=kafka1", + "--spring.cloud.stream.bindings.uppercase-output-0.destination=outputTopic", + "--spring.cloud.stream.bindings.uppercase-output-0.binder=kafka1", + "--spring.cloud.stream.binders.kafka1.type=kafka"); + + final MeterRegistry meterRegistry = applicationContext.getBean(MeterRegistry.class); + + assertMeterRegistry(meterRegistry); + + applicationContext.close(); + } + + @Test + public void testMetricsWorkWithMultiBinders() { + ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SimpleApplication.class) + .web(WebApplicationType.NONE) + .run("--spring.cloud.stream.bindings.uppercase-in-0.destination=inputTopic", + "--spring.cloud.stream.bindings.uppercase-in-0.group=inputGroup", + "--spring.cloud.stream.bindings.uppercase-in-0.binder=kafka1", + "--spring.cloud.stream.bindings.uppercase-output-0.destination=outputTopic", + "--spring.cloud.stream.bindings.uppercase-output-0.binder=kafka2", + "--spring.cloud.stream.binders.kafka1.type=kafka", + "--spring.cloud.stream.binders.kafka2.type=kafka", + "--spring.cloud.stream.default.binder=kafka1"); + + final MeterRegistry meterRegistry = applicationContext.getBean(MeterRegistry.class); + + assertMeterRegistry(meterRegistry); + + applicationContext.close(); + } + + private void assertMeterRegistry(MeterRegistry meterRegistry) { + assertThat(meterRegistry).isNotNull(); + + // assert kafka binder metrics + assertThat(meterRegistry.get("spring.cloud.stream.binder.kafka.offset") + .tag("group", "inputGroup") + .tag("topic", "inputTopic").gauge().value()).isNotNull(); + + // assert consumer metrics + assertThatCode(() -> meterRegistry.get("kafka.consumer.connection.count").meter()).doesNotThrowAnyException(); + + // assert producer metrics + assertThatCode(() -> meterRegistry.get("kafka.producer.connection.count").meter()).doesNotThrowAnyException(); + } + + @SpringBootApplication + static class SimpleApplication { + + @Bean + public Function uppercase() { + return String::toUpperCase; + } + + } + +} diff --git a/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/bootstrap/MultiBinderMeterRegistryTest.java b/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/bootstrap/MultiBinderMeterRegistryTest.java deleted file mode 100644 index 2a26123e4..000000000 --- a/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/bootstrap/MultiBinderMeterRegistryTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2019-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.stream.binder.kafka.bootstrap; - -import io.micrometer.core.instrument.MeterRegistry; -import org.junit.ClassRule; -import org.junit.Test; - -import org.springframework.boot.WebApplicationType; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.cloud.stream.annotation.EnableBinding; -import org.springframework.cloud.stream.messaging.Sink; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.kafka.test.rule.EmbeddedKafkaRule; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Soby Chacko - */ -public class MultiBinderMeterRegistryTest { - - @ClassRule - public static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, true, 10); - - @Test - public void testMetricsWorkWithMultiBinders() { - ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder( - SimpleApplication.class).web(WebApplicationType.NONE).run( - "--spring.cloud.stream.bindings.input.destination=foo", - "--spring.cloud.stream.bindings.input.binder=inbound", - "--spring.cloud.stream.bindings.input.group=testGroupabc", - "--spring.cloud.stream.binders.inbound.type=kafka", - "--spring.cloud.stream.binders.inbound.environment" - + ".spring.cloud.stream.kafka.binder.brokers" + "=" - + embeddedKafka.getEmbeddedKafka().getBrokersAsString()); - - final MeterRegistry meterRegistry = applicationContext.getBean(MeterRegistry.class); - - assertThat(meterRegistry).isNotNull(); - - assertThat(meterRegistry.get("spring.cloud.stream.binder.kafka.offset") - .tag("group", "testGroupabc") - .tag("topic", "foo").gauge().value()).isNotNull(); - - applicationContext.close(); - } - - @SpringBootApplication - @EnableBinding(Sink.class) - static class SimpleApplication { - - } - -}