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