Migrate to Spring Integration Micrometer support

Remove custom `SpringIntegrationMetrics` and instead provide
auto-configuration to the direct Micrometer support added in Spring
Integration 5.0.2.

Closes gh-11985
This commit is contained in:
Phillip Webb
2018-02-12 11:43:38 -08:00
parent b0e86bd7ac
commit f34aa6f4d8
13 changed files with 162 additions and 436 deletions

View File

@@ -1,166 +0,0 @@
/*
* Copyright 2012-2018 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
*
* http://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.metrics.integration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.function.ToDoubleFunction;
import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.micrometer.core.instrument.TimeGauge;
import io.micrometer.core.instrument.binder.MeterBinder;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
import org.springframework.integration.support.management.MessageChannelMetrics;
import org.springframework.integration.support.management.MessageHandlerMetrics;
import org.springframework.integration.support.management.MessageSourceMetrics;
import org.springframework.integration.support.management.PollableChannelManagement;
/**
* A {@link MeterBinder} for Spring Integration metrics.
*
* @author Jon Schneider
* @since 2.0.0
*/
public class SpringIntegrationMetrics implements MeterBinder, SmartInitializingSingleton {
private final Tags tags;
private Collection<MeterRegistry> registries = new ArrayList<>();
private final IntegrationManagementConfigurer configurer;
public SpringIntegrationMetrics(IntegrationManagementConfigurer configurer) {
this(configurer, Collections.emptyList());
}
public SpringIntegrationMetrics(IntegrationManagementConfigurer configurer,
Iterable<? extends Tag> tags) {
this.configurer = configurer;
this.tags = Tags.of(tags);
}
@Override
public void bindTo(MeterRegistry registry) {
registerGauge(registry, this.configurer, this.tags,
"spring.integration.channelNames",
"The number of spring integration channels",
(configurer) -> configurer.getChannelNames().length);
registerGauge(registry, this.configurer, this.tags,
"spring.integration.handlerNames",
"The number of spring integration handlers",
(configurer) -> configurer.getHandlerNames().length);
registerGauge(registry, this.configurer, this.tags,
"spring.integration.sourceNames",
"The number of spring integration sources",
(configurer) -> configurer.getSourceNames().length);
this.registries.add(registry);
}
private void addSourceMetrics(MeterRegistry registry) {
for (String source : this.configurer.getSourceNames()) {
MessageSourceMetrics sourceMetrics = this.configurer.getSourceMetrics(source);
Iterable<Tag> tagsWithSource = this.tags.and("source", source);
registerFunctionCounter(registry, sourceMetrics, tagsWithSource,
"spring.integration.source.messages",
"The number of successful handler calls",
MessageSourceMetrics::getMessageCount);
}
}
private void addHandlerMetrics(MeterRegistry registry) {
for (String handler : this.configurer.getHandlerNames()) {
MessageHandlerMetrics handlerMetrics = this.configurer
.getHandlerMetrics(handler);
Iterable<Tag> tagsWithHandler = this.tags.and("handler", handler);
registerTimedGauge(registry, handlerMetrics, tagsWithHandler,
"spring.integration.handler.duration.max",
"The maximum handler duration",
MessageHandlerMetrics::getMaxDuration);
registerTimedGauge(registry, handlerMetrics, tagsWithHandler,
"spring.integration.handler.duration.min",
"The minimum handler duration",
MessageHandlerMetrics::getMinDuration);
registerTimedGauge(registry, handlerMetrics, tagsWithHandler,
"spring.integration.handler.duration.mean",
"The mean handler duration", MessageHandlerMetrics::getMeanDuration);
registerGauge(registry, handlerMetrics, tagsWithHandler,
"spring.integration.handler.activeCount",
"The number of active handlers",
MessageHandlerMetrics::getActiveCount);
}
}
private void addChannelMetrics(MeterRegistry registry) {
for (String channel : this.configurer.getChannelNames()) {
MessageChannelMetrics channelMetrics = this.configurer
.getChannelMetrics(channel);
Iterable<Tag> tagsWithChannel = this.tags.and("channel", channel);
registerFunctionCounter(registry, channelMetrics, tagsWithChannel,
"spring.integration.channel.sendErrors",
"The number of failed sends (either throwing an exception or rejected by the channel)",
MessageChannelMetrics::getSendErrorCount);
registerFunctionCounter(registry, channelMetrics, tagsWithChannel,
"spring.integration.channel.sends", "The number of successful sends",
MessageChannelMetrics::getSendCount);
if (channelMetrics instanceof PollableChannelManagement) {
registerFunctionCounter(registry,
(PollableChannelManagement) channelMetrics, tagsWithChannel,
"spring.integration.receives", "The number of messages received",
PollableChannelManagement::getReceiveCount);
}
}
}
private <T> void registerGauge(MeterRegistry registry, T object, Iterable<Tag> tags,
String name, String description, ToDoubleFunction<T> value) {
Gauge.Builder<?> builder = Gauge.builder(name, object, value);
builder.tags(tags).description(description).register(registry);
}
private <T> void registerTimedGauge(MeterRegistry registry, T object,
Iterable<Tag> tags, String name, String description,
ToDoubleFunction<T> value) {
TimeGauge.Builder<?> builder = TimeGauge.builder(name, object,
TimeUnit.MILLISECONDS, value);
builder.tags(tags).description(description).register(registry);
}
private <T> void registerFunctionCounter(MeterRegistry registry, T object,
Iterable<Tag> tags, String name, String description,
ToDoubleFunction<T> value) {
FunctionCounter.builder(name, object, value).tags(tags).description(description)
.register(registry);
}
@Override
public void afterSingletonsInstantiated() {
this.registries.forEach((registry) -> {
addChannelMetrics(registry);
addHandlerMetrics(registry);
addSourceMetrics(registry);
});
}
}

View File

@@ -1,20 +0,0 @@
/*
* Copyright 2012-2017 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
*
* http://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.
*/
/**
* Actuator support for Spring Integration metrics.
*/
package org.springframework.boot.actuate.metrics.integration;

View File

@@ -1,134 +0,0 @@
/*
* Copyright 2012-2018 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
*
* http://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.metrics.integration;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.Gateway;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.support.management.IntegrationManagementConfigurer;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link SpringIntegrationMetrics}.
*
* @author Jon Schneider
* @author Andy Wilkinson
*/
@RunWith(SpringRunner.class)
public class SpringIntegrationMetricsIntegrationTests {
@Autowired
TestSpringIntegrationApplication.TempConverter converter;
@Autowired
MeterRegistry registry;
@Test
public void springIntegrationMetrics() {
this.converter.fahrenheitToCelsius(68.0);
assertThat(this.registry.get("spring.integration.channel.sends")
.tags("channel", "convert.input").functionCounter().count()).isEqualTo(1);
this.registry.get("spring.integration.handler.duration.min").gauge();
this.registry.get("spring.integration.sourceNames").meter();
}
@Configuration
@EnableIntegration
@IntegrationComponentScan
public static class TestSpringIntegrationApplication {
@Bean
MeterRegistry meterRegistry() {
return new SimpleMeterRegistry();
}
@Bean
public IntegrationManagementConfigurer integrationManagementConfigurer() {
IntegrationManagementConfigurer configurer = new IntegrationManagementConfigurer();
configurer.setDefaultCountsEnabled(true);
configurer.setDefaultStatsEnabled(true);
return configurer;
}
@Bean
public SpringIntegrationMetrics springIntegrationMetrics(
IntegrationManagementConfigurer configurer, MeterRegistry registry) {
SpringIntegrationMetrics springIntegrationMetrics = new SpringIntegrationMetrics(
configurer);
springIntegrationMetrics.bindTo(registry);
return springIntegrationMetrics;
}
@Bean
public IntegrationFlow convert() {
return (f) -> f
.transform((payload) -> "{\"fahrenheit\":" + payload + "}",
(e) -> e.id("toJson"))
.handle(String.class, this::fahrenheitToCelsius,
(e) -> e.id("temperatureConverter"))
.transform(this::extractResult, (e) -> e.id("toResponse"));
}
private double extractResult(String json) {
try {
return (double) new ObjectMapper().readValue(json, Map.class)
.get("celsius");
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private String fahrenheitToCelsius(String payload, Map<String, Object> headers) {
try {
double fahrenheit = (double) new ObjectMapper()
.readValue(payload, Map.class).get("fahrenheit");
double celsius = (fahrenheit - 32) * (5.0 / 9.0);
return "{\"celsius\":" + celsius + "}";
}
catch (Exception ex) {
throw new RuntimeException(ex);
}
}
@MessagingGateway
public interface TempConverter {
@Gateway(requestChannel = "convert.input")
double fahrenheitToCelsius(double fahrenheit);
}
}
}