mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add auto-configuration for RabbitMQ metrics
See gh-10887
This commit is contained in:
parent
1d9f9716e9
commit
58c8c4d56a
@ -19,12 +19,15 @@ package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import com.rabbitmq.client.Channel;
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Metrics;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
|
||||
|
||||
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsConfiguration;
|
||||
@ -43,13 +46,16 @@ import org.springframework.boot.actuate.autoconfigure.metrics.reactive.server.We
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.web.client.RestTemplateMetricsConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsConfiguration;
|
||||
import org.springframework.boot.actuate.metrics.MetricsEndpoint;
|
||||
import org.springframework.boot.actuate.metrics.amqp.RabbitMetrics;
|
||||
import org.springframework.boot.actuate.metrics.integration.SpringIntegrationMetrics;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
|
||||
@ -66,6 +72,7 @@ import org.springframework.integration.support.management.IntegrationManagementC
|
||||
* @since 2.0.0
|
||||
* @author Jon Schneider
|
||||
* @author Stephane Nicoll
|
||||
* @author Arnaud Cogoluègnes
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(Timed.class)
|
||||
@ -132,6 +139,19 @@ public class MetricsAutoConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
|
||||
@ConditionalOnBean(AbstractConnectionFactory.class)
|
||||
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "metrics", matchIfMissing = true)
|
||||
static class MetricsRabbitConfiguration {
|
||||
|
||||
@Bean
|
||||
public RabbitMetrics rabbitMetrics(AbstractConnectionFactory connectionFactory) {
|
||||
return new RabbitMetrics(connectionFactory.getRabbitConnectionFactory());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class MeterRegistryConfigurationSupport {
|
||||
|
||||
|
@ -25,6 +25,7 @@ import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
@ -37,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link MetricsAutoConfiguration}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Arnaud Cogoluègnes
|
||||
*/
|
||||
public class MetricsAutoConfigurationTests {
|
||||
|
||||
@ -107,6 +109,27 @@ public class MetricsAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rabbitmqNativeConnectionFactoryIsInstrumented() {
|
||||
this.contextRunner
|
||||
.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class))
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
assertThat(registry.find("rabbitmq.connections").meter()).isPresent();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rabbitmqNativeConnectionFactoryInstrumentationCanBeDisabled() {
|
||||
this.contextRunner
|
||||
.withConfiguration(AutoConfigurations.of(RabbitAutoConfiguration.class))
|
||||
.withPropertyValues("spring.rabbitmq.metrics=false")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
assertThat(registry.find("rabbitmq.connections").meter()).isNotPresent();
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class RegistryConfiguration {
|
||||
|
||||
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.amqp;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import com.rabbitmq.client.ConnectionFactory;
|
||||
import com.rabbitmq.client.impl.MicrometerMetricsCollector;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
|
||||
/**
|
||||
* A {@link MeterBinder} for RabbitMQ Java Client metrics.
|
||||
*
|
||||
* @author Arnaud Cogoluègnes
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class RabbitMetrics implements MeterBinder {
|
||||
|
||||
private final Iterable<Tag> tags;
|
||||
|
||||
private final ConnectionFactory connectionFactory;
|
||||
|
||||
public RabbitMetrics(ConnectionFactory connectionFactory) {
|
||||
this(connectionFactory, Collections.emptyList());
|
||||
}
|
||||
|
||||
public RabbitMetrics(ConnectionFactory connectionFactory, Iterable<Tag> tags) {
|
||||
this.connectionFactory = connectionFactory;
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindTo(MeterRegistry registry) {
|
||||
this.connectionFactory.setMetricsCollector(new MicrometerMetricsCollector(registry, "rabbitmq", this.tags));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Actuator support for RabbitMQ Java Client metrics.
|
||||
*/
|
||||
package org.springframework.boot.actuate.metrics.amqp;
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* 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.
|
||||
@ -37,6 +37,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Andy Wilkinson
|
||||
* @author Josh Thornhill
|
||||
* @author Gary Russell
|
||||
* @author Arnaud Cogoluègnes
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.rabbitmq")
|
||||
public class RabbitProperties {
|
||||
@ -112,6 +113,11 @@ public class RabbitProperties {
|
||||
|
||||
private List<Address> parsedAddresses;
|
||||
|
||||
/**
|
||||
* Enable metrics.
|
||||
*/
|
||||
private boolean metrics = true;
|
||||
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
}
|
||||
@ -307,6 +313,14 @@ public class RabbitProperties {
|
||||
return this.template;
|
||||
}
|
||||
|
||||
public boolean isMetrics() {
|
||||
return this.metrics;
|
||||
}
|
||||
|
||||
public void setMetrics(boolean metrics) {
|
||||
this.metrics = metrics;
|
||||
}
|
||||
|
||||
public static class Ssl {
|
||||
|
||||
/**
|
||||
|
@ -126,7 +126,7 @@
|
||||
<postgresql.version>42.1.4</postgresql.version>
|
||||
<quartz.version>2.3.0</quartz.version>
|
||||
<querydsl.version>4.1.4</querydsl.version>
|
||||
<rabbit-amqp-client.version>5.1.1</rabbit-amqp-client.version>
|
||||
<rabbit-amqp-client.version>5.1.2</rabbit-amqp-client.version>
|
||||
<reactor-bom.version>Bismuth-BUILD-SNAPSHOT</reactor-bom.version>
|
||||
<rest-assured.version>3.0.6</rest-assured.version>
|
||||
<reactive-streams.version>1.0.2</reactive-streams.version>
|
||||
|
Loading…
Reference in New Issue
Block a user