mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Polish "Allow custom ExemplarSampler to be used"
See gh-35619
This commit is contained in:
parent
94b17ecd26
commit
750096b9c2
@ -20,8 +20,6 @@ import io.micrometer.core.instrument.Clock;
|
|||||||
import io.micrometer.prometheus.PrometheusConfig;
|
import io.micrometer.prometheus.PrometheusConfig;
|
||||||
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
||||||
import io.prometheus.client.CollectorRegistry;
|
import io.prometheus.client.CollectorRegistry;
|
||||||
import io.prometheus.client.exemplars.DefaultExemplarSampler;
|
|
||||||
import io.prometheus.client.exemplars.Exemplar;
|
|
||||||
import io.prometheus.client.exemplars.ExemplarSampler;
|
import io.prometheus.client.exemplars.ExemplarSampler;
|
||||||
import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier;
|
import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier;
|
||||||
import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory;
|
import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory;
|
||||||
@ -47,6 +45,7 @@ import org.springframework.context.annotation.Import;
|
|||||||
import org.springframework.test.util.ReflectionTestUtils;
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link PrometheusMetricsExportAutoConfiguration}.
|
* Tests for {@link PrometheusMetricsExportAutoConfiguration}.
|
||||||
@ -66,12 +65,6 @@ class PrometheusMetricsExportAutoConfigurationTests {
|
|||||||
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(PrometheusMeterRegistry.class));
|
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(PrometheusMeterRegistry.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void backsOfWhenExemplarSamplerIsPresent() {
|
|
||||||
this.contextRunner.withUserConfiguration(CustomExemplarSamplerConfiguration.class)
|
|
||||||
.run((context) -> assertThat(context).doesNotHaveBean(DefaultExemplarSampler.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void autoConfiguresItsConfigCollectorRegistryAndMeterRegistry() {
|
void autoConfiguresItsConfigCollectorRegistryAndMeterRegistry() {
|
||||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||||
@ -127,12 +120,21 @@ class PrometheusMetricsExportAutoConfigurationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void autoConfiguresExemplarSamplerIfSpanContextSupplierIsPresent() {
|
void autoConfiguresExemplarSamplerIfSpanContextSupplierIsPresent() {
|
||||||
this.contextRunner.withUserConfiguration(DefaultExemplarSamplerConfiguration.class)
|
this.contextRunner.withUserConfiguration(ExemplarsConfiguration.class)
|
||||||
.run((context) -> assertThat(context).hasSingleBean(SpanContextSupplier.class)
|
.run((context) -> assertThat(context).hasSingleBean(SpanContextSupplier.class)
|
||||||
.hasSingleBean(ExemplarSampler.class)
|
.hasSingleBean(ExemplarSampler.class)
|
||||||
.hasSingleBean(PrometheusMeterRegistry.class));
|
.hasSingleBean(PrometheusMeterRegistry.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void allowsCustomExemplarSamplerToBeUsed() {
|
||||||
|
this.contextRunner.withUserConfiguration(ExemplarsConfiguration.class)
|
||||||
|
.withBean("customExemplarSampler", ExemplarSampler.class, () -> mock(ExemplarSampler.class))
|
||||||
|
.run((context) -> assertThat(context).hasSingleBean(ExemplarSampler.class)
|
||||||
|
.getBean(ExemplarSampler.class)
|
||||||
|
.isSameAs(context.getBean("customExemplarSampler")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void exemplarSamplerIsNotAutoConfiguredIfSpanContextSupplierIsMissing() {
|
void exemplarSamplerIsNotAutoConfiguredIfSpanContextSupplierIsMissing() {
|
||||||
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
|
||||||
@ -295,7 +297,7 @@ class PrometheusMetricsExportAutoConfigurationTests {
|
|||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@Import(BaseConfiguration.class)
|
@Import(BaseConfiguration.class)
|
||||||
static class DefaultExemplarSamplerConfiguration {
|
static class ExemplarsConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
SpanContextSupplier spanContextSupplier() {
|
SpanContextSupplier spanContextSupplier() {
|
||||||
@ -321,26 +323,4 @@ class PrometheusMetricsExportAutoConfigurationTests {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@Import(BaseConfiguration.class)
|
|
||||||
static class CustomExemplarSamplerConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
ExemplarSampler exemplarSampler() {
|
|
||||||
return new ExemplarSampler() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Exemplar sample(double value, double bucketFrom, double bucketTo, Exemplar previous) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Exemplar sample(double increment, Exemplar previous) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user