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 15, 2020
1 parent 776cd08 commit eb5974a
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 2 deletions.
Expand Up @@ -70,6 +70,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
@@ -0,0 +1,54 @@
/*
* 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.actuate.autoconfigure.metrics;

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

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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.streams.KafkaStreamsMicrometerListener;

/**
* Auto-configuration for Kafka Streams metrics.
*
* @author Eddú Meléndez
* @since 2.4.0
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore(KafkaMetricsAutoConfiguration.class)
@AutoConfigureAfter(MetricsAutoConfiguration.class)
@ConditionalOnClass({ KafkaStreamsMetrics.class, StreamsBuilderFactoryBean.class })
@ConditionalOnBean(MeterRegistry.class)
public class KafkaStreamsMetricsAutoConfiguration {

@Bean
public StreamsBuilderFactoryBeanCustomizer kafkaProducerMetrics(MeterRegistry meterRegistry) {
return (factoryBean) -> addListener(factoryBean, meterRegistry);
}

private void addListener(StreamsBuilderFactoryBean factory, MeterRegistry meterRegistry) {
factory.setListener(new KafkaStreamsMicrometerListener(meterRegistry));
}

}
Expand Up @@ -40,6 +40,7 @@ org.springframework.boot.actuate.autoconfigure.management.ThreadDumpEndpointAuto
org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.JvmMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.KafkaMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.KafkaStreamsMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.Log4J2MetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.LogbackMetricsAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration,\
Expand Down
@@ -0,0 +1,73 @@
/*
* 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.actuate.autoconfigure.metrics;

import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
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.streams.KafkaStreamsMicrometerListener;
import org.springframework.test.util.ReflectionTestUtils;

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

/**
* Tests for {@link KafkaStreamsMetricsAutoConfiguration}.
*
* @author Eddú Meléndez
*/
class KafkaStreamsMetricsAutoConfigurationTest {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(KafkaStreamsMetricsAutoConfiguration.class))
.withUserConfiguration(EnableKafkaStreamsConfiguration.class)
.withPropertyValues("spring.application.name=my-test-app");

@Test
void whenThereIsAMeterRegistryThenMetricsListenersAreAdded() {
this.contextRunner.with(MetricsRun.simple())
.withConfiguration(AutoConfigurations.of(KafkaAutoConfiguration.class)).run((context) -> {
StreamsBuilderFactoryBean streamsBuilderFactoryBean = context
.getBean(StreamsBuilderFactoryBean.class);
StreamsBuilderFactoryBean.Listener listener = (StreamsBuilderFactoryBean.Listener) ReflectionTestUtils
.getField(streamsBuilderFactoryBean, "listener");
assertThat(listener).isInstanceOf(KafkaStreamsMicrometerListener.class);
});
}

@Test
void whenThereIsNoMeterRegistryThenListenerCustomizationBacksOff() {
this.contextRunner.withConfiguration(AutoConfigurations.of(KafkaAutoConfiguration.class)).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.4.0
*/
@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 @@ -1700,7 +1700,7 @@ bom {
]
}
}
library("Spring Kafka", "2.5.0.RELEASE") {
library("Spring Kafka", "2.5.3.BUILD-SNAPSHOT") {
group("org.springframework.kafka") {
modules = [
"spring-kafka",
Expand Down

0 comments on commit eb5974a

Please sign in to comment.