-
Notifications
You must be signed in to change notification settings - Fork 40.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add micrometer support for kafka streams
See gh-21890
- Loading branch information
1 parent
776cd08
commit eb5974a
Showing
7 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...ingframework/boot/actuate/autoconfigure/metrics/KafkaStreamsMetricsAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...ramework/boot/actuate/autoconfigure/metrics/KafkaStreamsMetricsAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...ava/org/springframework/boot/autoconfigure/kafka/StreamsBuilderFactoryBeanCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters