Remove unnecessary config options for JDBC, Rabbit, and Cache metrics
See gh-12017
This commit is contained in:
committed by
Stephane Nicoll
parent
db4ffbbc4b
commit
49f21a2264
@@ -32,8 +32,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
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.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -49,20 +47,14 @@ import org.springframework.util.StringUtils;
|
||||
SimpleMetricsExportAutoConfiguration.class })
|
||||
@ConditionalOnClass({ ConnectionFactory.class, AbstractConnectionFactory.class })
|
||||
@ConditionalOnBean({ AbstractConnectionFactory.class, MeterRegistry.class })
|
||||
@ConditionalOnProperty(value = "management.metrics.rabbitmq.instrument", matchIfMissing = true)
|
||||
@EnableConfigurationProperties(RabbitMetricsProperties.class)
|
||||
public class RabbitMetricsAutoConfiguration {
|
||||
|
||||
private static final String CONNECTION_FACTORY_SUFFIX = "connectionFactory";
|
||||
|
||||
private final MeterRegistry registry;
|
||||
|
||||
private final String metricName;
|
||||
|
||||
public RabbitMetricsAutoConfiguration(MeterRegistry registry,
|
||||
RabbitMetricsProperties rabbitMetricsProperties) {
|
||||
public RabbitMetricsAutoConfiguration(MeterRegistry registry) {
|
||||
this.registry = registry;
|
||||
this.metricName = rabbitMetricsProperties.getMetricName();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@@ -76,7 +68,7 @@ public class RabbitMetricsAutoConfiguration {
|
||||
ConnectionFactory rabbitConnectionFactory = connectionFactory
|
||||
.getRabbitConnectionFactory();
|
||||
String connectionFactoryName = getConnectionFactoryName(beanName);
|
||||
new RabbitMetrics(rabbitConnectionFactory, this.metricName,
|
||||
new RabbitMetrics(rabbitConnectionFactory,
|
||||
Tags.of("name", connectionFactoryName)).bindTo(this.registry);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,7 +21,6 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cache.Cache;
|
||||
import org.springframework.cache.CacheManager;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -37,7 +36,6 @@ import org.springframework.context.annotation.Import;
|
||||
@Configuration
|
||||
@AutoConfigureAfter({ MetricsAutoConfiguration.class, CacheAutoConfiguration.class })
|
||||
@ConditionalOnBean(CacheManager.class)
|
||||
@ConditionalOnProperty(value = "management.metrics.cache.instrument", matchIfMissing = true)
|
||||
@Import({ CacheMeterBinderProvidersConfiguration.class,
|
||||
CacheMetricsRegistrarConfiguration.class })
|
||||
public class CacheMetricsAutoConfiguration {
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.jdbc;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||
@@ -31,9 +31,7 @@ import org.springframework.boot.actuate.metrics.jdbc.DataSourcePoolMetrics;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -50,8 +48,6 @@ import org.springframework.util.StringUtils;
|
||||
SimpleMetricsExportAutoConfiguration.class })
|
||||
@ConditionalOnBean({ DataSource.class, DataSourcePoolMetadataProvider.class,
|
||||
MeterRegistry.class })
|
||||
@ConditionalOnProperty(value = "management.metrics.jdbc.instrument", matchIfMissing = true)
|
||||
@EnableConfigurationProperties(JdbcMetricsProperties.class)
|
||||
public class DataSourcePoolMetricsAutoConfiguration {
|
||||
|
||||
private static final String DATASOURCE_SUFFIX = "dataSource";
|
||||
@@ -60,14 +56,10 @@ public class DataSourcePoolMetricsAutoConfiguration {
|
||||
|
||||
private final Collection<DataSourcePoolMetadataProvider> metadataProviders;
|
||||
|
||||
private final String metricName;
|
||||
|
||||
public DataSourcePoolMetricsAutoConfiguration(MeterRegistry registry,
|
||||
Collection<DataSourcePoolMetadataProvider> metadataProviders,
|
||||
JdbcMetricsProperties jdbcMetricsProperties) {
|
||||
Collection<DataSourcePoolMetadataProvider> metadataProviders) {
|
||||
this.registry = registry;
|
||||
this.metadataProviders = metadataProviders;
|
||||
this.metricName = jdbcMetricsProperties.getMetricName();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@@ -77,8 +69,8 @@ public class DataSourcePoolMetricsAutoConfiguration {
|
||||
|
||||
private void bindDataSourceToRegistry(String beanName, DataSource dataSource) {
|
||||
String dataSourceName = getDataSourceName(beanName);
|
||||
new DataSourcePoolMetrics(dataSource, this.metadataProviders, this.metricName,
|
||||
Tags.of("name", dataSourceName)).bindTo(this.registry);
|
||||
new DataSourcePoolMetrics(dataSource, this.metadataProviders,
|
||||
dataSourceName, Collections.emptyList()).bindTo(this.registry);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* 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.jdbc;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Configuration properties for JDBC-based metrics.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@ConfigurationProperties("management.metrics.jdbc")
|
||||
public class JdbcMetricsProperties {
|
||||
|
||||
/**
|
||||
* Name of the metric for data source usage.
|
||||
*/
|
||||
private String metricName = "data.source";
|
||||
|
||||
public String getMetricName() {
|
||||
return this.metricName;
|
||||
}
|
||||
|
||||
public void setMetricName(String metricName) {
|
||||
this.metricName = metricName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -209,24 +209,6 @@
|
||||
"description": "Whether to enable uptime metrics.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "management.metrics.cache.instrument",
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "Instrument all available caches.",
|
||||
"defaultValue": true
|
||||
},
|
||||
{
|
||||
"name": "management.metrics.jdbc.instrument",
|
||||
"type": "java.lang.Boolean",
|
||||
"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": "management.trace.http.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
|
||||
@@ -45,21 +45,10 @@ public class RabbitMetricsAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoConfiguredConnectionFactoryWithCustomMetricName() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("management.metrics.rabbitmq.metric-name=custom.name")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
registry.get("custom.name.connections").meter();
|
||||
assertThat(registry.find("rabbitmq.connections").meter()).isNull();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rabbitmqNativeConnectionFactoryInstrumentationCanBeDisabled() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("management.metrics.rabbitmq.instrument=false")
|
||||
.withPropertyValues("management.metrics.enable.rabbitmq=false")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
assertThat(registry.find("rabbitmq.connections").meter()).isNull();
|
||||
|
||||
@@ -66,7 +66,7 @@ public class CacheMetricsAutoConfigurationTests {
|
||||
@Test
|
||||
public void cacheInstrumentationCanBeDisabled() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("management.metrics.cache.instrument=false",
|
||||
.withPropertyValues("management.metrics.enable.cache=false",
|
||||
"spring.cache.type=caffeine", "spring.cache.cache-names=cache1")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
|
||||
@@ -24,6 +24,7 @@ import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
@@ -42,6 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
public class DataSourcePoolMetricsAutoConfigurationTests {
|
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.with(MetricsRun.simple())
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(DataSourcePoolMetricsAutoConfiguration.class))
|
||||
.withUserConfiguration(BaseConfiguration.class);
|
||||
@@ -55,22 +57,7 @@ public class DataSourcePoolMetricsAutoConfigurationTests {
|
||||
.run((context) -> {
|
||||
context.getBean(DataSource.class).getConnection().getMetaData();
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
registry.get("data.source.max.connections").tags("name", "dataSource")
|
||||
.meter();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autoConfiguredDataSourceWithCustomMetricName() {
|
||||
this.contextRunner
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(DataSourceAutoConfiguration.class))
|
||||
.withPropertyValues("spring.datasource.generate-unique-name=true",
|
||||
"management.metrics.jdbc.metric-name=custom.name")
|
||||
.run((context) -> {
|
||||
context.getBean(DataSource.class).getConnection().getMetaData();
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
registry.get("custom.name.max.connections").tags("name", "dataSource")
|
||||
registry.get("jdbc.max.connections").tags("name", "dataSource")
|
||||
.meter();
|
||||
});
|
||||
}
|
||||
@@ -81,11 +68,11 @@ public class DataSourcePoolMetricsAutoConfigurationTests {
|
||||
.withConfiguration(
|
||||
AutoConfigurations.of(DataSourceAutoConfiguration.class))
|
||||
.withPropertyValues("spring.datasource.generate-unique-name=true",
|
||||
"management.metrics.jdbc.instrument=false")
|
||||
"management.metrics.enable.jdbc=false")
|
||||
.run((context) -> {
|
||||
context.getBean(DataSource.class).getConnection().getMetaData();
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
assertThat(registry.find("data.source.max.connections")
|
||||
assertThat(registry.find("jdbc.max.connections")
|
||||
.tags("name", "dataSource").meter()).isNull();
|
||||
});
|
||||
}
|
||||
@@ -101,9 +88,9 @@ public class DataSourcePoolMetricsAutoConfigurationTests {
|
||||
context.getBean("secondOne", DataSource.class).getConnection()
|
||||
.getMetaData();
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
registry.get("data.source.max.connections").tags("name", "first")
|
||||
registry.get("jdbc.max.connections").tags("name", "first")
|
||||
.meter();
|
||||
registry.get("data.source.max.connections").tags("name", "secondOne")
|
||||
registry.get("jdbc.max.connections").tags("name", "secondOne")
|
||||
.meter();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -37,29 +37,23 @@ 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) {
|
||||
public RabbitMetrics(ConnectionFactory connectionFactory, 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.emptyList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bindTo(MeterRegistry registry) {
|
||||
this.connectionFactory.setMetricsCollector(
|
||||
new MicrometerMetricsCollector(registry, this.name, this.tags));
|
||||
new MicrometerMetricsCollector(registry, "rabbitmq", this.tags));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import javax.sql.DataSource;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Tag;
|
||||
import io.micrometer.core.instrument.Tags;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
|
||||
import org.springframework.boot.jdbc.metadata.CompositeDataSourcePoolMetadataProvider;
|
||||
@@ -45,15 +46,13 @@ public class DataSourcePoolMetrics implements MeterBinder {
|
||||
|
||||
private final CachingDataSourcePoolMetadataProvider metadataProvider;
|
||||
|
||||
private final String name;
|
||||
|
||||
private final Iterable<Tag> tags;
|
||||
|
||||
public DataSourcePoolMetrics(DataSource dataSource,
|
||||
Collection<DataSourcePoolMetadataProvider> metadataProviders, String name,
|
||||
Collection<DataSourcePoolMetadataProvider> metadataProviders, String dataSourceName,
|
||||
Iterable<Tag> tags) {
|
||||
this(dataSource, new CompositeDataSourcePoolMetadataProvider(metadataProviders),
|
||||
name, tags);
|
||||
dataSourceName, tags);
|
||||
}
|
||||
|
||||
public DataSourcePoolMetrics(DataSource dataSource,
|
||||
@@ -64,8 +63,7 @@ public class DataSourcePoolMetrics implements MeterBinder {
|
||||
this.dataSource = dataSource;
|
||||
this.metadataProvider = new CachingDataSourcePoolMetadataProvider(
|
||||
metadataProvider);
|
||||
this.name = name;
|
||||
this.tags = tags;
|
||||
this.tags = Tags.concat(tags, "name", name);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -77,15 +75,15 @@ public class DataSourcePoolMetrics implements MeterBinder {
|
||||
}
|
||||
}
|
||||
|
||||
private <N extends Number> void bindPoolMetadata(MeterRegistry registry, String name,
|
||||
private <N extends Number> void bindPoolMetadata(MeterRegistry registry, String metricName,
|
||||
Function<DataSourcePoolMetadata, N> function) {
|
||||
bindDataSource(registry, name, this.metadataProvider.getValueFunction(function));
|
||||
bindDataSource(registry, metricName, this.metadataProvider.getValueFunction(function));
|
||||
}
|
||||
|
||||
private <N extends Number> void bindDataSource(MeterRegistry registry, String name,
|
||||
private <N extends Number> void bindDataSource(MeterRegistry registry, String metricName,
|
||||
Function<DataSource, N> function) {
|
||||
if (function.apply(this.dataSource) != null) {
|
||||
registry.gauge(this.name + "." + name + ".connections", this.tags,
|
||||
registry.gauge("jdbc." + metricName + ".connections", this.tags,
|
||||
this.dataSource, (m) -> function.apply(m).doubleValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,19 +35,19 @@ public class RabbitMetricsTests {
|
||||
public void connectionFactoryIsInstrumented() {
|
||||
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
|
||||
SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
||||
new RabbitMetrics(connectionFactory, "rabbit", null).bindTo(registry);
|
||||
registry.get("rabbit.connections");
|
||||
new RabbitMetrics(connectionFactory, null).bindTo(registry);
|
||||
registry.get("rabbitmq.connections");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectionFactoryWithTagsIsInstrumented() {
|
||||
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
|
||||
SimpleMeterRegistry registry = new SimpleMeterRegistry();
|
||||
new RabbitMetrics(connectionFactory, "test", Tags.of("env", "prod"))
|
||||
new RabbitMetrics(connectionFactory, Tags.of("env", "prod"))
|
||||
.bindTo(registry);
|
||||
assertThat(registry.get("test.connections").tags("env", "prod").meter())
|
||||
assertThat(registry.get("rabbitmq.connections").tags("env", "prod").meter())
|
||||
.isNotNull();
|
||||
assertThat(registry.find("test.connections").tags("env", "dev").meter()).isNull();
|
||||
assertThat(registry.find("rabbitmq.connections").tags("env", "dev").meter()).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class DataSourcePoolMetricsTests {
|
||||
.run((context) -> {
|
||||
context.getBean(DataSource.class).getConnection().getMetaData();
|
||||
context.getBean(MeterRegistry.class)
|
||||
.get("data.source.max.connections").meter();
|
||||
.get("jdbc.max.connections").meter();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user