Polish "Add auto-configuration for RabbitMQ metrics"
Closes gh-10887
This commit is contained in:
parent
58c8c4d56a
commit
beaa49a2d6
|
@ -19,17 +19,15 @@ package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import com.rabbitmq.client.Channel;
|
|
||||||
import io.micrometer.core.annotation.Timed;
|
import io.micrometer.core.annotation.Timed;
|
||||||
import io.micrometer.core.instrument.MeterRegistry;
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import io.micrometer.core.instrument.Metrics;
|
import io.micrometer.core.instrument.Metrics;
|
||||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
|
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.beans.factory.ObjectProvider;
|
||||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||||
|
import org.springframework.boot.actuate.autoconfigure.metrics.amqp.RabbitMetricsConfiguration;
|
||||||
import org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsConfiguration;
|
import org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsConfiguration;
|
||||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.MetricsExporter;
|
import org.springframework.boot.actuate.autoconfigure.metrics.export.MetricsExporter;
|
||||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasExportConfiguration;
|
import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasExportConfiguration;
|
||||||
|
@ -46,16 +44,14 @@ 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.client.RestTemplateMetricsConfiguration;
|
||||||
import org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsConfiguration;
|
import org.springframework.boot.actuate.autoconfigure.metrics.web.servlet.WebMvcMetricsConfiguration;
|
||||||
import org.springframework.boot.actuate.metrics.MetricsEndpoint;
|
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.actuate.metrics.integration.SpringIntegrationMetrics;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
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.amqp.RabbitAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
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.condition.SearchStrategy;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
|
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
|
||||||
|
@ -72,7 +68,6 @@ import org.springframework.integration.support.management.IntegrationManagementC
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
* @author Jon Schneider
|
* @author Jon Schneider
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Arnaud Cogoluègnes
|
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass(Timed.class)
|
@ConditionalOnClass(Timed.class)
|
||||||
|
@ -80,13 +75,13 @@ import org.springframework.integration.support.management.IntegrationManagementC
|
||||||
@Import({ MeterBindersConfiguration.class, WebMvcMetricsConfiguration.class,
|
@Import({ MeterBindersConfiguration.class, WebMvcMetricsConfiguration.class,
|
||||||
WebFluxMetricsConfiguration.class, RestTemplateMetricsConfiguration.class,
|
WebFluxMetricsConfiguration.class, RestTemplateMetricsConfiguration.class,
|
||||||
CacheMetricsConfiguration.class, DataSourcePoolMetricsConfiguration.class,
|
CacheMetricsConfiguration.class, DataSourcePoolMetricsConfiguration.class,
|
||||||
AtlasExportConfiguration.class, DatadogExportConfiguration.class,
|
RabbitMetricsConfiguration.class, AtlasExportConfiguration.class,
|
||||||
GangliaExportConfiguration.class, GraphiteExportConfiguration.class,
|
DatadogExportConfiguration.class, GangliaExportConfiguration.class,
|
||||||
InfluxExportConfiguration.class, JmxExportConfiguration.class,
|
GraphiteExportConfiguration.class, InfluxExportConfiguration.class,
|
||||||
PrometheusExportConfiguration.class, SimpleExportConfiguration.class,
|
JmxExportConfiguration.class, PrometheusExportConfiguration.class,
|
||||||
StatsdExportConfiguration.class })
|
SimpleExportConfiguration.class, StatsdExportConfiguration.class })
|
||||||
@AutoConfigureAfter({ CacheAutoConfiguration.class, DataSourceAutoConfiguration.class,
|
@AutoConfigureAfter({ CacheAutoConfiguration.class, DataSourceAutoConfiguration.class,
|
||||||
RestTemplateAutoConfiguration.class })
|
RabbitAutoConfiguration.class, RestTemplateAutoConfiguration.class })
|
||||||
public class MetricsAutoConfiguration {
|
public class MetricsAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -139,19 +134,6 @@ 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
|
@Configuration
|
||||||
static class MeterRegistryConfigurationSupport {
|
static class MeterRegistryConfigurationSupport {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
/*
|
||||||
|
* 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.autoconfigure.metrics.amqp;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.rabbitmq.client.ConnectionFactory;
|
||||||
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
|
import io.micrometer.core.instrument.Tag;
|
||||||
|
import io.micrometer.core.instrument.Tags;
|
||||||
|
|
||||||
|
import org.springframework.amqp.rabbit.connection.AbstractConnectionFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.actuate.metrics.amqp.RabbitMetrics;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure metrics for all available {@link ConnectionFactory connection factories}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnClass({ ConnectionFactory.class, AbstractConnectionFactory.class })
|
||||||
|
@ConditionalOnBean(AbstractConnectionFactory.class)
|
||||||
|
@ConditionalOnProperty(value = "management.metrics.rabbitmq.instrument", matchIfMissing = true)
|
||||||
|
@EnableConfigurationProperties(RabbitMetricsProperties.class)
|
||||||
|
public class RabbitMetricsConfiguration {
|
||||||
|
|
||||||
|
private static final String CONNECTION_FACTORY_SUFFIX = "connectionFactory";
|
||||||
|
|
||||||
|
private final MeterRegistry registry;
|
||||||
|
|
||||||
|
private final String metricName;
|
||||||
|
|
||||||
|
public RabbitMetricsConfiguration(MeterRegistry registry,
|
||||||
|
RabbitMetricsProperties rabbitMetricsProperties) {
|
||||||
|
this.registry = registry;
|
||||||
|
this.metricName = rabbitMetricsProperties.getMetricName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public void bindConnectionFactoriesToRegistry(
|
||||||
|
Map<String, AbstractConnectionFactory> connectionFactories) {
|
||||||
|
connectionFactories.forEach(this::bindConnectionFactoryToRegistry);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindConnectionFactoryToRegistry(String beanName,
|
||||||
|
AbstractConnectionFactory connectionFactory) {
|
||||||
|
List<Tag> tags = Tags.zip("name", getConnectionFactoryName(beanName));
|
||||||
|
new RabbitMetrics(connectionFactory.getRabbitConnectionFactory(), this.metricName,
|
||||||
|
tags).bindTo(this.registry);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of a ConnectionFactory based on its {@code beanName}.
|
||||||
|
* @param beanName the name of the connection factory bean
|
||||||
|
* @return a name for the given connection factory
|
||||||
|
*/
|
||||||
|
private String getConnectionFactoryName(String beanName) {
|
||||||
|
if (beanName.length() > CONNECTION_FACTORY_SUFFIX.length()
|
||||||
|
&& StringUtils.endsWithIgnoreCase(beanName, CONNECTION_FACTORY_SUFFIX)) {
|
||||||
|
return beanName.substring(0, beanName.length() - CONNECTION_FACTORY_SUFFIX.length());
|
||||||
|
}
|
||||||
|
return beanName;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* 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.autoconfigure.metrics.amqp;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration properties for RabbitMQ-based metrics.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("management.metrics.rabbitmq")
|
||||||
|
public class RabbitMetricsProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the metric for RabbitMQ usage.
|
||||||
|
*/
|
||||||
|
private String metricName = "rabbitmq";
|
||||||
|
|
||||||
|
public String getMetricName() {
|
||||||
|
return this.metricName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMetricName(String metricName) {
|
||||||
|
this.metricName = metricName;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-configuration for RabbitMQ metrics.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.actuate.autoconfigure.metrics.amqp;
|
|
@ -233,6 +233,13 @@
|
||||||
"description": "Instrument all available data sources.",
|
"description": "Instrument all available data sources.",
|
||||||
"defaultValue": true
|
"defaultValue": true
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "management.metrics.rabbitmq.instrument",
|
||||||
|
"type": "java.lang.Boolean",
|
||||||
|
"description": "Instrument all available connection factories.",
|
||||||
|
"defaultValue": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "endpoints.actuator.enabled",
|
"name": "endpoints.actuator.enabled",
|
||||||
"type": "java.lang.Boolean",
|
"type": "java.lang.Boolean",
|
||||||
|
|
|
@ -25,7 +25,6 @@ import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
|
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
@ -38,7 +37,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* Tests for {@link MetricsAutoConfiguration}.
|
* Tests for {@link MetricsAutoConfiguration}.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Arnaud Cogoluègnes
|
|
||||||
*/
|
*/
|
||||||
public class MetricsAutoConfigurationTests {
|
public class MetricsAutoConfigurationTests {
|
||||||
|
|
||||||
|
@ -109,27 +107,6 @@ 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
|
@Configuration
|
||||||
static class RegistryConfiguration {
|
static class RegistryConfiguration {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
/*
|
||||||
|
* 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.autoconfigure.metrics.amqp;
|
||||||
|
|
||||||
|
import io.micrometer.core.instrument.MeterRegistry;
|
||||||
|
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link RabbitMetricsConfiguration}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class RabbitMetricsConfigurationTests {
|
||||||
|
|
||||||
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withUserConfiguration(RegistryConfiguration.class)
|
||||||
|
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class,
|
||||||
|
RabbitAutoConfiguration.class))
|
||||||
|
.withPropertyValues("management.metrics.use-global-registry=false");
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void autoConfiguredConnectionFactoryIsInstrumented() {
|
||||||
|
this.contextRunner.run((context) -> {
|
||||||
|
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||||
|
assertThat(registry.find("rabbitmq.connections").meter()).isPresent();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void autoConfiguredConnectionFactoryWithCustomMetricName() {
|
||||||
|
this.contextRunner
|
||||||
|
.withPropertyValues("management.metrics.rabbitmq.metric-name=custom.name")
|
||||||
|
.run((context) -> {
|
||||||
|
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||||
|
assertThat(registry.find("custom.name.connections").meter())
|
||||||
|
.isPresent();
|
||||||
|
assertThat(registry.find("rabbitmq.connections").meter())
|
||||||
|
.isNotPresent();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void rabbitmqNativeConnectionFactoryInstrumentationCanBeDisabled() {
|
||||||
|
this.contextRunner
|
||||||
|
.withPropertyValues(
|
||||||
|
"management.metrics.rabbitmq.instrument=false").run((context) -> {
|
||||||
|
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||||
|
assertThat(registry.find("rabbitmq.connections").meter()).isNotPresent();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class RegistryConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public MeterRegistry meterRegistry() {
|
||||||
|
return new SimpleMeterRegistry();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -24,30 +24,42 @@ import io.micrometer.core.instrument.MeterRegistry;
|
||||||
import io.micrometer.core.instrument.Tag;
|
import io.micrometer.core.instrument.Tag;
|
||||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||||
|
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link MeterBinder} for RabbitMQ Java Client metrics.
|
* A {@link MeterBinder} for RabbitMQ Java Client metrics.
|
||||||
*
|
*
|
||||||
* @author Arnaud Cogoluègnes
|
* @author Arnaud Cogoluègnes
|
||||||
|
* @author Stephane Nicoll
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public class RabbitMetrics implements MeterBinder {
|
public class RabbitMetrics implements MeterBinder {
|
||||||
|
|
||||||
private final Iterable<Tag> tags;
|
private final Iterable<Tag> tags;
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
private final ConnectionFactory connectionFactory;
|
private final ConnectionFactory connectionFactory;
|
||||||
|
|
||||||
public RabbitMetrics(ConnectionFactory connectionFactory) {
|
/**
|
||||||
this(connectionFactory, Collections.emptyList());
|
* Create a new meter binder recording the specified {@link ConnectionFactory}.
|
||||||
}
|
* @param connectionFactory the {@link ConnectionFactory} to instrument
|
||||||
|
* @param name the name prefix of the metrics
|
||||||
public RabbitMetrics(ConnectionFactory connectionFactory, Iterable<Tag> tags) {
|
* @param tags tags to apply to all recorded metrics
|
||||||
|
*/
|
||||||
|
public RabbitMetrics(ConnectionFactory connectionFactory, String name,
|
||||||
|
Iterable<Tag> tags) {
|
||||||
|
Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
|
||||||
|
Assert.notNull(name, "Name must not be null");
|
||||||
this.connectionFactory = connectionFactory;
|
this.connectionFactory = connectionFactory;
|
||||||
this.tags = tags;
|
this.name = name;
|
||||||
|
this.tags = (tags != null ? tags : Collections.EMPTY_LIST);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void bindTo(MeterRegistry registry) {
|
public void bindTo(MeterRegistry registry) {
|
||||||
this.connectionFactory.setMetricsCollector(new MicrometerMetricsCollector(registry, "rabbitmq", this.tags));
|
this.connectionFactory.setMetricsCollector(new MicrometerMetricsCollector(
|
||||||
|
registry, this.name, this.tags));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
/*
|
||||||
|
* 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 com.rabbitmq.client.ConnectionFactory;
|
||||||
|
import io.micrometer.core.instrument.Tags;
|
||||||
|
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link RabbitMetrics}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class RabbitMetricsTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectionFactoryIsInstrumented() {
|
||||||
|
ConnectionFactory connectionFactory = mockConnectionFactory();
|
||||||
|
SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
||||||
|
new RabbitMetrics(connectionFactory, "rabbit", null).bindTo(registry);
|
||||||
|
assertThat(registry.find("rabbit.connections").meter()).isPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void connectionFactoryWithTagsIsInstrumented() {
|
||||||
|
ConnectionFactory connectionFactory = mockConnectionFactory();
|
||||||
|
SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
||||||
|
new RabbitMetrics(connectionFactory, "test", Tags.zip("env", "prod"))
|
||||||
|
.bindTo(registry);
|
||||||
|
assertThat(registry.find("test.connections")
|
||||||
|
.tags("env", "prod").meter()).isPresent();
|
||||||
|
assertThat(registry.find("test.connections")
|
||||||
|
.tags("env", "dev").meter()).isNotPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConnectionFactory mockConnectionFactory() {
|
||||||
|
return mock(ConnectionFactory.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2018 the original author or authors.
|
* Copyright 2012-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -37,7 +37,6 @@ import org.springframework.util.StringUtils;
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
* @author Josh Thornhill
|
* @author Josh Thornhill
|
||||||
* @author Gary Russell
|
* @author Gary Russell
|
||||||
* @author Arnaud Cogoluègnes
|
|
||||||
*/
|
*/
|
||||||
@ConfigurationProperties(prefix = "spring.rabbitmq")
|
@ConfigurationProperties(prefix = "spring.rabbitmq")
|
||||||
public class RabbitProperties {
|
public class RabbitProperties {
|
||||||
|
@ -113,11 +112,6 @@ public class RabbitProperties {
|
||||||
|
|
||||||
private List<Address> parsedAddresses;
|
private List<Address> parsedAddresses;
|
||||||
|
|
||||||
/**
|
|
||||||
* Enable metrics.
|
|
||||||
*/
|
|
||||||
private boolean metrics = true;
|
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
return this.host;
|
return this.host;
|
||||||
}
|
}
|
||||||
|
@ -313,14 +307,6 @@ public class RabbitProperties {
|
||||||
return this.template;
|
return this.template;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isMetrics() {
|
|
||||||
return this.metrics;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMetrics(boolean metrics) {
|
|
||||||
this.metrics = metrics;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Ssl {
|
public static class Ssl {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1353,6 +1353,8 @@ content into your application. Rather, pick only the properties that you need.
|
||||||
management.metrics.export.statsd.queue-size=2147483647 # Maximum size of the queue of items waiting to be sent to the StatsD server.
|
management.metrics.export.statsd.queue-size=2147483647 # Maximum size of the queue of items waiting to be sent to the StatsD server.
|
||||||
management.metrics.jdbc.datasource-metric-name=data.source # Name of the metric for data source usage.
|
management.metrics.jdbc.datasource-metric-name=data.source # Name of the metric for data source usage.
|
||||||
management.metrics.jdbc.instrument=true # Instrument all available data sources.
|
management.metrics.jdbc.instrument=true # Instrument all available data sources.
|
||||||
|
management.metrics.rabbitmq.instrument=true # Instrument all available connection factories.
|
||||||
|
management.metrics.rabbitmq.metric-name=rabbitmq # Name of the metric for RabbitMQ usage.
|
||||||
management.metrics.use-global-registry=true # Whether auto-configured MeterRegistry implementations should be bound to the global static registry on Metrics.
|
management.metrics.use-global-registry=true # Whether auto-configured MeterRegistry implementations should be bound to the global static registry on Metrics.
|
||||||
management.metrics.web.client.record-request-percentiles=false # Whether instrumented requests record percentiles histogram buckets by default.
|
management.metrics.web.client.record-request-percentiles=false # Whether instrumented requests record percentiles histogram buckets by default.
|
||||||
management.metrics.web.client.requests-metric-name=http.client.requests # Name of the metric for sent requests.
|
management.metrics.web.client.requests-metric-name=http.client.requests # Name of the metric for sent requests.
|
||||||
|
|
|
@ -1068,6 +1068,14 @@ Metrics are also tagged by the name of the `DataSource` computed based on the be
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[[production-ready-metrics-rabbitmq]]
|
||||||
|
=== RabbitMQ metrics
|
||||||
|
Auto-configuration will enable the instrumentation of all available RabbitMQ connection
|
||||||
|
factories with a metric named `rabbitmq`. The prefix can be customized by using the
|
||||||
|
`management.metrics.rabbitmq.metric-name` property.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
[[production-ready-metrics-integration]]
|
[[production-ready-metrics-integration]]
|
||||||
=== Spring Integration Metrics
|
=== Spring Integration Metrics
|
||||||
Auto-configuration enables binding of a number of Spring Integration-related metrics:
|
Auto-configuration enables binding of a number of Spring Integration-related metrics:
|
||||||
|
|
Loading…
Reference in New Issue