Polish "Allow custom ExemplarSampler to be used"

See gh-35619
pull/37018/head
Stephane Nicoll 1 year ago
parent 94b17ecd26
commit 750096b9c2

@ -20,8 +20,6 @@ import io.micrometer.core.instrument.Clock;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
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.tracer.common.SpanContextSupplier;
import io.prometheus.client.exporter.BasicAuthHttpConnectionFactory;
@ -47,6 +45,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link PrometheusMetricsExportAutoConfiguration}.
@ -66,12 +65,6 @@ class PrometheusMetricsExportAutoConfigurationTests {
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
void autoConfiguresItsConfigCollectorRegistryAndMeterRegistry() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
@ -127,12 +120,21 @@ class PrometheusMetricsExportAutoConfigurationTests {
@Test
void autoConfiguresExemplarSamplerIfSpanContextSupplierIsPresent() {
this.contextRunner.withUserConfiguration(DefaultExemplarSamplerConfiguration.class)
this.contextRunner.withUserConfiguration(ExemplarsConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(SpanContextSupplier.class)
.hasSingleBean(ExemplarSampler.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
void exemplarSamplerIsNotAutoConfiguredIfSpanContextSupplierIsMissing() {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
@ -295,7 +297,7 @@ class PrometheusMetricsExportAutoConfigurationTests {
@Configuration(proxyBeanMethods = false)
@Import(BaseConfiguration.class)
static class DefaultExemplarSamplerConfiguration {
static class ExemplarsConfiguration {
@Bean
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…
Cancel
Save