Skip to content

Commit

Permalink
Merge pull request #21921 from eddumelendez
Browse files Browse the repository at this point in the history
* gh-21921:
  Reinstate metrics for Kafka Streams

Closes gh-21921
  • Loading branch information
wilkinsona committed Jul 1, 2020
2 parents d643370 + 54e0a61 commit b9bfcdd
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
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,19 +27,23 @@
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.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Eddú Meléndez
* @since 2.1.0
*/
@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -66,4 +71,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
StreamsBuilderFactoryBeanCustomizer kafkaStreamsProducerMetrics(MeterRegistry meterRegistry) {
return (factoryBean) -> factoryBean.addListener(new KafkaStreamsMicrometerListener(meterRegistry));
}

}

}
Expand Up @@ -22,10 +22,14 @@
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 static org.assertj.core.api.Assertions.assertThat;

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

Expand Down Expand Up @@ -61,4 +66,33 @@ 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);
assertThat(streamsBuilderFactoryBean.getListeners()).hasSize(1)
.hasOnlyElementsOfTypes(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);
assertThat(streamsBuilderFactoryBean.getListeners()).isEmpty();
});
}

@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);

}

0 comments on commit b9bfcdd

Please sign in to comment.