Skip to content

Commit

Permalink
Add micrometer support for kafka streams
Browse files Browse the repository at this point in the history
  • Loading branch information
eddumelendez committed Jun 19, 2020
1 parent f961043 commit b8370cc
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
Expand Up @@ -67,6 +67,7 @@ dependencies {
optional("org.apache.activemq:activemq-broker")
optional("org.apache.commons:commons-dbcp2")
optional("org.apache.kafka:kafka-clients")
optional("org.apache.kafka:kafka-streams")
optional("org.apache.tomcat.embed:tomcat-embed-core")
optional("org.apache.tomcat.embed:tomcat-embed-el")
optional("org.apache.tomcat:tomcat-jdbc")
Expand Down
Expand Up @@ -18,6 +18,7 @@

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.kafka.KafkaClientMetrics;
import io.micrometer.core.instrument.binder.kafka.KafkaStreamsMetrics;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
Expand All @@ -26,13 +27,16 @@
import org.springframework.boot.autoconfigure.kafka.DefaultKafkaConsumerFactoryCustomizer;
import org.springframework.boot.autoconfigure.kafka.DefaultKafkaProducerFactoryCustomizer;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.autoconfigure.kafka.StreamsBuilderFactoryBeanCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.StreamsBuilderFactoryBean;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.MicrometerConsumerListener;
import org.springframework.kafka.core.MicrometerProducerListener;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.streams.KafkaStreamsMicrometerListener;

/**
* Auto-configuration for Kafka metrics.
Expand Down Expand Up @@ -66,4 +70,15 @@ private <K, V> void addListener(DefaultKafkaProducerFactory<K, V> factory, Meter
factory.addListener(new MicrometerProducerListener<>(meterRegistry));
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ KafkaStreamsMetrics.class, StreamsBuilderFactoryBean.class })
static class KafkaStreamsMetricsAutoConfiguration {

@Bean
public StreamsBuilderFactoryBeanCustomizer kafkaStreamsProducerMetrics(MeterRegistry meterRegistry) {
return (factoryBean) -> factoryBean.setListener(new KafkaStreamsMicrometerListener(meterRegistry));
}

}

}
Expand Up @@ -22,10 +22,15 @@
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafkaStreams;
import org.springframework.kafka.config.StreamsBuilderFactoryBean;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.MicrometerConsumerListener;
import org.springframework.kafka.core.MicrometerProducerListener;
import org.springframework.kafka.streams.KafkaStreamsMicrometerListener;
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -34,6 +39,7 @@
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
class KafkaMetricsAutoConfigurationTests {

Expand Down Expand Up @@ -61,4 +67,36 @@ void whenThereIsNoMeterRegistryThenListenerCustomizationBacksOff() {
});
}

@Test
void whenKafkaStreamsIsEnabledAndThereIsAMeterRegistryThenMetricsListenersAreAdded() {
this.contextRunner.withConfiguration(AutoConfigurations.of(KafkaAutoConfiguration.class))
.withUserConfiguration(EnableKafkaStreamsConfiguration.class)
.withPropertyValues("spring.application.name=my-test-app").with(MetricsRun.simple()).run((context) -> {
StreamsBuilderFactoryBean streamsBuilderFactoryBean = context
.getBean(StreamsBuilderFactoryBean.class);
StreamsBuilderFactoryBean.Listener listener = (StreamsBuilderFactoryBean.Listener) ReflectionTestUtils
.getField(streamsBuilderFactoryBean, "listener");
assertThat(listener).isInstanceOf(KafkaStreamsMicrometerListener.class);
});
}

@Test
void whenKafkaStreamsIsEnabledAndThereIsNoMeterRegistryThenListenerCustomizationBacksOff() {
this.contextRunner.withConfiguration(AutoConfigurations.of(KafkaAutoConfiguration.class))
.withUserConfiguration(EnableKafkaStreamsConfiguration.class)
.withPropertyValues("spring.application.name=my-test-app").run((context) -> {
StreamsBuilderFactoryBean streamsBuilderFactoryBean = context
.getBean(StreamsBuilderFactoryBean.class);
StreamsBuilderFactoryBean.Listener listener = (StreamsBuilderFactoryBean.Listener) ReflectionTestUtils
.getField(streamsBuilderFactoryBean, "listener");
assertThat(listener).isNotInstanceOf(KafkaStreamsMicrometerListener.class);
});
}

@Configuration(proxyBeanMethods = false)
@EnableKafkaStreams
static class EnableKafkaStreamsConfiguration {

}

}
Expand Up @@ -22,6 +22,7 @@
import org.apache.kafka.streams.StreamsConfig;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
Expand All @@ -39,6 +40,7 @@
*
* @author Gary Russell
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(StreamsBuilder.class)
Expand Down Expand Up @@ -68,7 +70,9 @@ KafkaStreamsConfiguration defaultKafkaStreamsConfig(Environment environment) {

@Bean
KafkaStreamsFactoryBeanConfigurer kafkaStreamsFactoryBeanConfigurer(
@Qualifier(KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_BUILDER_BEAN_NAME) StreamsBuilderFactoryBean factoryBean) {
@Qualifier(KafkaStreamsDefaultConfiguration.DEFAULT_STREAMS_BUILDER_BEAN_NAME) StreamsBuilderFactoryBean factoryBean,
ObjectProvider<StreamsBuilderFactoryBeanCustomizer> customizers) {
customizers.orderedStream().forEach((customizer) -> customizer.customize(factoryBean));
return new KafkaStreamsFactoryBeanConfigurer(this.properties, factoryBean);
}

Expand Down
@@ -0,0 +1,36 @@
/*
* Copyright 2012-2020 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.boot.autoconfigure.kafka;

import org.springframework.kafka.config.StreamsBuilderFactoryBean;

/**
* Callback interface for customizing {@code StreamsBuilderFactoryBean} beans.
*
* @author Eddú Meléndez
* @since 2.3.2
*/
@FunctionalInterface
public interface StreamsBuilderFactoryBeanCustomizer {

/**
* Customize the {@link StreamsBuilderFactoryBean}.
* @param factoryBean the factory bean to customize
*/
void customize(StreamsBuilderFactoryBean factoryBean);

}
2 changes: 1 addition & 1 deletion spring-boot-project/spring-boot-dependencies/build.gradle
Expand Up @@ -1707,7 +1707,7 @@ bom {
]
}
}
library("Spring Kafka", "2.5.2.RELEASE") {
library("Spring Kafka", "2.5.3.BUILD-SNAPSHOT") {
group("org.springframework.kafka") {
modules = [
"spring-kafka",
Expand Down

0 comments on commit b8370cc

Please sign in to comment.