Merge pull request #10887 from acogoluegnes:rabbitmq-metrics-autoconfiguration-spring-boot-2

* pr/10887:
  Polish "Add auto-configuration for RabbitMQ metrics"
  Add auto-configuration for RabbitMQ metrics
pull/11665/merge
Stephane Nicoll 7 years ago
commit 171cbc650c

@ -27,6 +27,7 @@ import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import org.springframework.beans.factory.ObjectProvider;
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.export.MetricsExporter;
import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasExportConfiguration;
@ -46,6 +47,7 @@ import org.springframework.boot.actuate.metrics.MetricsEndpoint;
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.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -73,13 +75,13 @@ import org.springframework.integration.support.management.IntegrationManagementC
@Import({ MeterBindersConfiguration.class, WebMvcMetricsConfiguration.class,
WebFluxMetricsConfiguration.class, RestTemplateMetricsConfiguration.class,
CacheMetricsConfiguration.class, DataSourcePoolMetricsConfiguration.class,
AtlasExportConfiguration.class, DatadogExportConfiguration.class,
GangliaExportConfiguration.class, GraphiteExportConfiguration.class,
InfluxExportConfiguration.class, JmxExportConfiguration.class,
PrometheusExportConfiguration.class, SimpleExportConfiguration.class,
StatsdExportConfiguration.class })
RabbitMetricsConfiguration.class, AtlasExportConfiguration.class,
DatadogExportConfiguration.class, GangliaExportConfiguration.class,
GraphiteExportConfiguration.class, InfluxExportConfiguration.class,
JmxExportConfiguration.class, PrometheusExportConfiguration.class,
SimpleExportConfiguration.class, StatsdExportConfiguration.class })
@AutoConfigureAfter({ CacheAutoConfiguration.class, DataSourceAutoConfiguration.class,
RestTemplateAutoConfiguration.class })
RabbitAutoConfiguration.class, RestTemplateAutoConfiguration.class })
public class MetricsAutoConfiguration {
@Bean

@ -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.",
"defaultValue": true
},
},
{
"name": "management.metrics.rabbitmq.instrument",
"type": "java.lang.Boolean",
"description": "Instrument all available connection factories.",
"defaultValue": true
},
{
"name": "endpoints.actuator.enabled",
"type": "java.lang.Boolean",

@ -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();
}
}
}

@ -0,0 +1,65 @@
/*
* 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;
import org.springframework.util.Assert;
/**
* A {@link MeterBinder} for RabbitMQ Java Client metrics.
*
* @author Arnaud Cogoluègnes
* @author Stephane Nicoll
* @since 2.0.0
*/
public class RabbitMetrics implements MeterBinder {
private final Iterable<Tag> tags;
private final String name;
private final ConnectionFactory connectionFactory;
/**
* 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
* @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.name = name;
this.tags = (tags != null ? tags : Collections.EMPTY_LIST);
}
@Override
public void bindTo(MeterRegistry registry) {
this.connectionFactory.setMetricsCollector(new MicrometerMetricsCollector(
registry, this.name, 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;

@ -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);
}
}

@ -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>

@ -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.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.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.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.

@ -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]]
=== Spring Integration Metrics
Auto-configuration enables binding of a number of Spring Integration-related metrics:

Loading…
Cancel
Save