|
|
@ -18,6 +18,7 @@ package org.springframework.boot.actuate.metrics.data;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
|
|
|
|
import io.micrometer.core.annotation.Timed;
|
|
|
|
import io.micrometer.core.annotation.Timed;
|
|
|
|
import io.micrometer.core.instrument.MeterRegistry;
|
|
|
|
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.AutoTimer;
|
|
|
|
import org.springframework.boot.actuate.metrics.annotation.TimedAnnotations;
|
|
|
|
import org.springframework.boot.actuate.metrics.annotation.TimedAnnotations;
|
|
|
|
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener;
|
|
|
|
import org.springframework.data.repository.core.support.RepositoryMethodInvocationListener;
|
|
|
|
|
|
|
|
import org.springframework.util.function.SingletonSupplier;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Intercepts Spring Data {@code Repository} invocations and records metrics about
|
|
|
|
* 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 {
|
|
|
|
public class MetricsRepositoryMethodInvocationListener implements RepositoryMethodInvocationListener {
|
|
|
|
|
|
|
|
|
|
|
|
private final MeterRegistry registry;
|
|
|
|
private final SingletonSupplier<MeterRegistry> registrySupplier;
|
|
|
|
|
|
|
|
|
|
|
|
private final RepositoryTagsProvider tagsProvider;
|
|
|
|
private final RepositoryTagsProvider tagsProvider;
|
|
|
|
|
|
|
|
|
|
|
@ -50,10 +52,27 @@ public class MetricsRepositoryMethodInvocationListener implements RepositoryMeth
|
|
|
|
* @param tagsProvider provider for metrics tags
|
|
|
|
* @param tagsProvider provider for metrics tags
|
|
|
|
* @param metricName name of the metric to record
|
|
|
|
* @param metricName name of the metric to record
|
|
|
|
* @param autoTimer the auto-timers to apply or {@code null} to disable auto-timing
|
|
|
|
* @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,
|
|
|
|
public MetricsRepositoryMethodInvocationListener(MeterRegistry registry, RepositoryTagsProvider tagsProvider,
|
|
|
|
String metricName, AutoTimer autoTimer) {
|
|
|
|
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.tagsProvider = tagsProvider;
|
|
|
|
this.metricName = metricName;
|
|
|
|
this.metricName = metricName;
|
|
|
|
this.autoTimer = (autoTimer != null) ? autoTimer : AutoTimer.DISABLED;
|
|
|
|
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());
|
|
|
|
Set<Timed> annotations = TimedAnnotations.get(invocation.getMethod(), invocation.getRepositoryInterface());
|
|
|
|
Iterable<Tag> tags = this.tagsProvider.repositoryTags(invocation);
|
|
|
|
Iterable<Tag> tags = this.tagsProvider.repositoryTags(invocation);
|
|
|
|
long duration = invocation.getDuration(TimeUnit.NANOSECONDS);
|
|
|
|
long duration = invocation.getDuration(TimeUnit.NANOSECONDS);
|
|
|
|
AutoTimer.apply(this.autoTimer, this.metricName, annotations,
|
|
|
|
AutoTimer.apply(this.autoTimer, this.metricName, annotations, (builder) -> builder.tags(tags)
|
|
|
|
(builder) -> builder.tags(tags).register(this.registry).record(duration, TimeUnit.NANOSECONDS));
|
|
|
|
.register(this.registrySupplier.get()).record(duration, TimeUnit.NANOSECONDS));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|