mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Prevent repository metrics from causing a circular reference
Fixes gh-27591
This commit is contained in:
parent
f2c3c0d207
commit
35ea3b234d
@ -64,11 +64,11 @@ public class RepositoryMetricsAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MetricsRepositoryMethodInvocationListener metricsRepositoryMethodInvocationListener(MeterRegistry registry,
|
||||
RepositoryTagsProvider tagsProvider) {
|
||||
public MetricsRepositoryMethodInvocationListener metricsRepositoryMethodInvocationListener(
|
||||
ObjectProvider<MeterRegistry> registry, RepositoryTagsProvider tagsProvider) {
|
||||
Repository properties = this.properties.getData().getRepository();
|
||||
return new MetricsRepositoryMethodInvocationListener(registry, tagsProvider, properties.getMetricName(),
|
||||
properties.getAutotime());
|
||||
return new MetricsRepositoryMethodInvocationListener(registry::getObject, tagsProvider,
|
||||
properties.getMetricName(), properties.getAutotime());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -16,7 +16,9 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.data;
|
||||
|
||||
import io.micrometer.core.instrument.Gauge;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.data.city.CityRepository;
|
||||
@ -28,6 +30,7 @@ import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfig
|
||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
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;
|
||||
@ -55,10 +58,26 @@ class RepositoryMetricsAutoConfigurationIntegrationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void doesNotPreventMeterBindersFromDependingUponSpringDataRepositories() {
|
||||
this.contextRunner.withUserConfiguration(SpringDataRepositoryMeterBinderConfiguration.class)
|
||||
.run((context) -> assertThat(context).hasNotFailed());
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigurationPackage
|
||||
static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class SpringDataRepositoryMeterBinderConfiguration {
|
||||
|
||||
@Bean
|
||||
MeterBinder meterBinder(CityRepository repository) {
|
||||
return (registry) -> Gauge.builder("city.count", repository::count);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics.data;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.micrometer.core.instrument.Meter;
|
||||
@ -28,6 +29,7 @@ import io.micrometer.core.instrument.binder.MeterBinder;
|
||||
import io.micrometer.core.instrument.distribution.HistogramSnapshot;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
|
||||
import org.springframework.boot.actuate.metrics.AutoTimer;
|
||||
import org.springframework.boot.actuate.metrics.data.DefaultRepositoryTagsProvider;
|
||||
@ -180,17 +182,18 @@ class RepositoryMetricsAutoConfigurationTests {
|
||||
static class MetricsRepositoryMethodInvocationListenerConfiguration {
|
||||
|
||||
@Bean
|
||||
MetricsRepositoryMethodInvocationListener metricsRepositoryMethodInvocationListener(MeterRegistry registry,
|
||||
RepositoryTagsProvider tagsProvider) {
|
||||
return new TestMetricsRepositoryMethodInvocationListener(registry, tagsProvider);
|
||||
MetricsRepositoryMethodInvocationListener metricsRepositoryMethodInvocationListener(
|
||||
ObjectFactory<MeterRegistry> registry, RepositoryTagsProvider tagsProvider) {
|
||||
return new TestMetricsRepositoryMethodInvocationListener(registry::getObject, tagsProvider);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class TestMetricsRepositoryMethodInvocationListener extends MetricsRepositoryMethodInvocationListener {
|
||||
|
||||
TestMetricsRepositoryMethodInvocationListener(MeterRegistry registry, RepositoryTagsProvider tagsProvider) {
|
||||
super(registry, tagsProvider, "test", AutoTimer.DISABLED);
|
||||
TestMetricsRepositoryMethodInvocationListener(Supplier<MeterRegistry> registrySupplier,
|
||||
RepositoryTagsProvider tagsProvider) {
|
||||
super(registrySupplier, tagsProvider, "test", AutoTimer.DISABLED);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.boot.actuate.metrics.data;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
@ -26,6 +27,7 @@ import io.micrometer.core.instrument.Tag;
|
||||
import org.springframework.boot.actuate.metrics.AutoTimer;
|
||||
import org.springframework.boot.actuate.metrics.annotation.TimedAnnotations;
|
||||
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener;
|
||||
import org.springframework.util.function.SingletonSupplier;
|
||||
|
||||
/**
|
||||
* Intercepts Spring Data {@code Repository} invocations and records metrics about
|
||||
@ -36,7 +38,7 @@ import org.springframework.data.repository.core.support.RepositoryMethodInvocati
|
||||
*/
|
||||
public class MetricsRepositoryMethodInvocationListener implements RepositoryMethodInvocationListener {
|
||||
|
||||
private final MeterRegistry registry;
|
||||
private final SingletonSupplier<MeterRegistry> registrySupplier;
|
||||
|
||||
private final RepositoryTagsProvider tagsProvider;
|
||||
|
||||
@ -50,10 +52,27 @@ public class MetricsRepositoryMethodInvocationListener implements RepositoryMeth
|
||||
* @param tagsProvider provider for metrics tags
|
||||
* @param metricName name of the metric to record
|
||||
* @param autoTimer the auto-timers to apply or {@code null} to disable auto-timing
|
||||
* @deprecated since 2.5.4 for removal in 2.7.0 in favor of
|
||||
* {@link #MetricsRepositoryMethodInvocationListener(Supplier, RepositoryTagsProvider, String, AutoTimer)}
|
||||
*/
|
||||
@Deprecated
|
||||
public MetricsRepositoryMethodInvocationListener(MeterRegistry registry, RepositoryTagsProvider tagsProvider,
|
||||
String metricName, AutoTimer autoTimer) {
|
||||
this.registry = registry;
|
||||
this(SingletonSupplier.of(registry), tagsProvider, metricName, autoTimer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code MetricsRepositoryMethodInvocationListener}.
|
||||
* @param registrySupplier a supplier for the registry to which metrics are recorded
|
||||
* @param tagsProvider provider for metrics tags
|
||||
* @param metricName name of the metric to record
|
||||
* @param autoTimer the auto-timers to apply or {@code null} to disable auto-timing
|
||||
* @since 2.5.4
|
||||
*/
|
||||
public MetricsRepositoryMethodInvocationListener(Supplier<MeterRegistry> registrySupplier,
|
||||
RepositoryTagsProvider tagsProvider, String metricName, AutoTimer autoTimer) {
|
||||
this.registrySupplier = (registrySupplier instanceof SingletonSupplier)
|
||||
? (SingletonSupplier<MeterRegistry>) registrySupplier : SingletonSupplier.of(registrySupplier);
|
||||
this.tagsProvider = tagsProvider;
|
||||
this.metricName = metricName;
|
||||
this.autoTimer = (autoTimer != null) ? autoTimer : AutoTimer.DISABLED;
|
||||
@ -64,8 +83,8 @@ public class MetricsRepositoryMethodInvocationListener implements RepositoryMeth
|
||||
Set<Timed> annotations = TimedAnnotations.get(invocation.getMethod(), invocation.getRepositoryInterface());
|
||||
Iterable<Tag> tags = this.tagsProvider.repositoryTags(invocation);
|
||||
long duration = invocation.getDuration(TimeUnit.NANOSECONDS);
|
||||
AutoTimer.apply(this.autoTimer, this.metricName, annotations,
|
||||
(builder) -> builder.tags(tags).register(this.registry).record(duration, TimeUnit.NANOSECONDS));
|
||||
AutoTimer.apply(this.autoTimer, this.metricName, annotations, (builder) -> builder.tags(tags)
|
||||
.register(this.registrySupplier.get()).record(duration, TimeUnit.NANOSECONDS));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -53,13 +53,13 @@ class MetricsRepositoryMethodInvocationListenerTests {
|
||||
void setup() {
|
||||
MockClock clock = new MockClock();
|
||||
this.registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
|
||||
this.listener = new MetricsRepositoryMethodInvocationListener(this.registry,
|
||||
this.listener = new MetricsRepositoryMethodInvocationListener(() -> this.registry,
|
||||
new DefaultRepositoryTagsProvider(), REQUEST_METRICS_NAME, AutoTimer.ENABLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void afterInvocationWhenNoTimerAnnotationsAndNoAutoTimerDoesNothing() {
|
||||
this.listener = new MetricsRepositoryMethodInvocationListener(this.registry,
|
||||
this.listener = new MetricsRepositoryMethodInvocationListener(() -> this.registry,
|
||||
new DefaultRepositoryTagsProvider(), REQUEST_METRICS_NAME, null);
|
||||
this.listener.afterInvocation(createInvocation(NoAnnotationsRepository.class));
|
||||
assertThat(this.registry.find(REQUEST_METRICS_NAME).timers()).isEmpty();
|
||||
|
Loading…
Reference in New Issue
Block a user