diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java index ec7742ba0c2..8886316ed5b 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfiguration.java @@ -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 diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsConfiguration.java new file mode 100644 index 00000000000..a24e0f3bd3d --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsConfiguration.java @@ -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 connectionFactories) { + connectionFactories.forEach(this::bindConnectionFactoryToRegistry); + } + + private void bindConnectionFactoryToRegistry(String beanName, + AbstractConnectionFactory connectionFactory) { + List 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; + } + +} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsProperties.java new file mode 100644 index 00000000000..681cf348084 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsProperties.java @@ -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; + } + +} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/package-info.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/package-info.java new file mode 100644 index 00000000000..f57df8abd61 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/package-info.java @@ -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; diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index fda2b35a802..d244c65e868 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsConfigurationTests.java new file mode 100644 index 00000000000..129db567416 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/amqp/RabbitMetricsConfigurationTests.java @@ -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(); + } + + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/amqp/RabbitMetrics.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/amqp/RabbitMetrics.java new file mode 100644 index 00000000000..6b663b45d6e --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/amqp/RabbitMetrics.java @@ -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 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 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)); + } + +} diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/amqp/package-info.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/amqp/package-info.java new file mode 100644 index 00000000000..8c56a721d7b --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/amqp/package-info.java @@ -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; diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/amqp/RabbitMetricsTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/amqp/RabbitMetricsTests.java new file mode 100644 index 00000000000..51e3edd565d --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/amqp/RabbitMetricsTests.java @@ -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); + } + +} diff --git a/spring-boot-project/spring-boot-dependencies/pom.xml b/spring-boot-project/spring-boot-dependencies/pom.xml index f212b227ba5..56c2fd30d12 100644 --- a/spring-boot-project/spring-boot-dependencies/pom.xml +++ b/spring-boot-project/spring-boot-dependencies/pom.xml @@ -126,7 +126,7 @@ 42.1.4 2.3.0 4.1.4 - 5.1.1 + 5.1.2 Bismuth-BUILD-SNAPSHOT 3.0.6 1.0.2 diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 9c65ec631b4..289023c09de 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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. diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc index 252d846fd8e..c7ff9a017d0 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc @@ -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: