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 metricspull/11665/merge
commit
171cbc650c
@ -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;
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue