Change metrics exporters to auto-configurations
Refactor `*ExportConfiguration` classes to be regular auto-configuration classes. Also removed the `@ConditionalOnProperty` guards for `.enabled` properties since auto-configuration can now be excluded in the usual way. Enabled properties remain where applicable and are adapted for Micrometer to use as it sees fit. Fixes gh-11838pull/11886/merge
parent
7ea6af0e46
commit
d8de8752ea
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/CompositeMeterRegistryPostProcessor.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryPostProcessor.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/CompositeMeterRegistryPostProcessor.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryPostProcessor.java
@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasExportConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.datadog.DatadogExportConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.ganglia.GangliaExportConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.graphite.GraphiteExportConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.influx.InfluxExportConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.jmx.JmxExportConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusExportConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleExportConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.statsd.StatsdExportConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* Imports for registry configurations.
|
||||
*
|
||||
* @author Jon Schneider
|
||||
*/
|
||||
@Configuration
|
||||
@Import({ AtlasExportConfiguration.class, DatadogExportConfiguration.class,
|
||||
GangliaExportConfiguration.class, GraphiteExportConfiguration.class,
|
||||
InfluxExportConfiguration.class, JmxExportConfiguration.class,
|
||||
PrometheusExportConfiguration.class, SimpleExportConfiguration.class,
|
||||
StatsdExportConfiguration.class })
|
||||
class MeterRegistriesConfiguration {
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||
import org.springframework.boot.actuate.metrics.MetricsEndpoint;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link MetricsEndpoint}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(Timed.class)
|
||||
@ConditionalOnBean(MeterRegistry.class)
|
||||
@AutoConfigureAfter(MetricsAutoConfiguration.class)
|
||||
public class MetricsEndpointAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledEndpoint
|
||||
public MetricsEndpoint metricsEndpoint(MeterRegistry registry) {
|
||||
return new MetricsEndpoint(registry);
|
||||
}
|
||||
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Conditionally build a composite registry when more than one registry present.
|
||||
*
|
||||
* @author Jon Schneider
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@Configuration
|
||||
public class CompositeMeterRegistryConfiguration {
|
||||
|
||||
@Bean
|
||||
public static CompositeMeterRegistryPostProcessor compositeMeterRegistryPostProcessor() {
|
||||
return new CompositeMeterRegistryPostProcessor();
|
||||
}
|
||||
|
||||
}
|
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasMetricsExportAutoConfiguration.java
16
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogMetricsExportAutoConfiguration.java
16
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/ganglia/GangliaExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/ganglia/GangliaMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/ganglia/GangliaExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/ganglia/GangliaMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/graphite/GraphiteMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/influx/InfluxMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/jmx/JmxExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/jmx/JmxMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/jmx/JmxExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/jmx/JmxMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java
17
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/PropertiesConfigAdapter.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/properties/PropertiesConfigAdapter.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/PropertiesConfigAdapter.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/properties/PropertiesConfigAdapter.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/StepRegistryProperties.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/properties/StepRegistryProperties.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/StepRegistryProperties.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/properties/StepRegistryProperties.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/StepRegistryPropertiesConfigAdapter.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/properties/StepRegistryPropertiesConfigAdapter.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/StepRegistryPropertiesConfigAdapter.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/properties/StepRegistryPropertiesConfigAdapter.java
6
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/package-info.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/properties/package-info.java
6
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/package-info.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/properties/package-info.java
22
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.java
22
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.java
16
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdMetricsExportAutoConfiguration.java
16
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdExportConfiguration.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/statsd/StatsdMetricsExportAutoConfiguration.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/CompositeMeterRegistryPostProcessorTests.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryPostProcessorTests.java
2
spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/CompositeMeterRegistryPostProcessorTests.java → spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/CompositeMeterRegistryPostProcessorTests.java
@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
/**
|
||||
* Additional metrics configuration and settings that can be applied to a
|
||||
* {@link ApplicationContextRunner} when running a metrics test.
|
||||
*
|
||||
* @author Jon Schneider
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public final class MetricsRun {
|
||||
|
||||
private static final Set<String> IMPLEMENTATIONS = Collections
|
||||
.unmodifiableSet(new LinkedHashSet<>(Arrays.asList("atlas", "datadog",
|
||||
"ganglia", "graphite", "influx", "jmx", "prometheus", "statsd",
|
||||
"newrelic", "signalfx", "wavefront", "simple")));
|
||||
|
||||
private MetricsRun() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a function that configures the run to be limited to the {@code simple}
|
||||
* implementation.
|
||||
* @return the function to apply
|
||||
*/
|
||||
public static Function<ApplicationContextRunner, ApplicationContextRunner> simple() {
|
||||
return limitedTo("simple");
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a function that configures the run to be limited to the specified
|
||||
* implementations.
|
||||
* @param implementations the implementations to include
|
||||
* @return the function to apply
|
||||
*/
|
||||
public static Function<ApplicationContextRunner, ApplicationContextRunner> limitedTo(
|
||||
String... implementations) {
|
||||
return (contextRunner) -> apply(contextRunner, implementations);
|
||||
}
|
||||
|
||||
private static ApplicationContextRunner apply(ApplicationContextRunner contextRunner,
|
||||
String[] implementations) {
|
||||
return contextRunner.withPropertyValues(getPropertyValues(implementations))
|
||||
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class));
|
||||
}
|
||||
|
||||
private static String[] getPropertyValues(String[] implementations) {
|
||||
List<String> propertyValues = new ArrayList<>();
|
||||
propertyValues.add("management.metrics.use-global-registry=false");
|
||||
List<String> keep = Arrays.asList(implementations);
|
||||
IMPLEMENTATIONS.stream()
|
||||
.filter((implementation) -> !keep.contains(implementation))
|
||||
.map(MetricsRun::disableExport).forEach(propertyValues::add);
|
||||
return propertyValues.toArray(new String[0]);
|
||||
}
|
||||
|
||||
private static String disableExport(String implementation) {
|
||||
return "management.metrics.export." + implementation + ".enabled=false";
|
||||
}
|
||||
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.export;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.MockClock;
|
||||
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
|
||||
import io.micrometer.core.instrument.simple.SimpleConfig;
|
||||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
|
||||
import io.micrometer.graphite.GraphiteMeterRegistry;
|
||||
import io.micrometer.jmx.JmxMeterRegistry;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsRun;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link CompositeMeterRegistryConfiguration}.
|
||||
*
|
||||
* @author Jon Schneider
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
public class CompositeMeterRegistryConfigurationTests {
|
||||
|
||||
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.with(MetricsRun.simple());
|
||||
|
||||
/**
|
||||
* The simple registry is off by default UNLESS there is no other registry
|
||||
* implementation on the classpath, in which case it is on.
|
||||
*/
|
||||
@Test
|
||||
public void simpleWithNoCompositeCreated() {
|
||||
this.contextRunner
|
||||
.run((context) -> assertThat(context.getBean(MeterRegistry.class))
|
||||
.isInstanceOf(SimpleMeterRegistry.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* An empty composite is created in the absence of any other registry implementation.
|
||||
* This effectively no-ops instrumentation code throughout the application.
|
||||
*/
|
||||
@Test
|
||||
public void emptyCompositeCreated() {
|
||||
new ApplicationContextRunner().with(MetricsRun.limitedTo()).run((context) -> {
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
assertThat(registry).isInstanceOf(CompositeMeterRegistry.class);
|
||||
assertThat(((CompositeMeterRegistry) registry).getRegistries()).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noCompositeCreatedWhenSingleImplementationIsEnabled() {
|
||||
new ApplicationContextRunner().with(MetricsRun.limitedTo("graphite"))
|
||||
.run((context) -> assertThat(context.getBean(MeterRegistry.class))
|
||||
.isInstanceOf(GraphiteMeterRegistry.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noCompositeCreatedWhenMultipleRegistriesButOneMarkedAsPrimary() {
|
||||
new ApplicationContextRunner().with(MetricsRun.limitedTo("graphite", "jmx"))
|
||||
.withUserConfiguration(PrimarySimpleMeterRegistryConfiguration.class)
|
||||
.run((context) -> assertThat(context.getBean(MeterRegistry.class))
|
||||
.isInstanceOf(SimpleMeterRegistry.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compositeCreatedWhenMultipleImplementationsAreEnabled() {
|
||||
new ApplicationContextRunner().with(MetricsRun.limitedTo("graphite", "jmx"))
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
assertThat(registry).isInstanceOf(CompositeMeterRegistry.class);
|
||||
assertThat(((CompositeMeterRegistry) registry).getRegistries())
|
||||
.hasAtLeastOneElementOfType(GraphiteMeterRegistry.class)
|
||||
.hasAtLeastOneElementOfType(JmxMeterRegistry.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class PrimarySimpleMeterRegistryConfiguration {
|
||||
|
||||
@Primary
|
||||
@Bean
|
||||
public MeterRegistry simpleMeterRegistry() {
|
||||
return new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.atlas.AtlasMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.datadog.DatadogMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.ganglia.GangliaMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.graphite.GraphiteMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.influx.InfluxMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.jmx.JmxMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.actuate.autoconfigure.metrics.export.statsd.StatsdMetricsExportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Additional metrics configuration and settings that can be applied to a
|
||||
* {@link ApplicationContextRunner} when running a metrics test.
|
||||
*
|
||||
* @author Jon Schneider
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public final class MetricsRun {
|
||||
|
||||
private static final Set<Class<?>> IMPLEMENTATIONS;
|
||||
|
||||
static {
|
||||
Set<Class<?>> implementations = new LinkedHashSet<>();
|
||||
implementations.add(AtlasMetricsExportAutoConfiguration.class);
|
||||
implementations.add(DatadogMetricsExportAutoConfiguration.class);
|
||||
implementations.add(GangliaMetricsExportAutoConfiguration.class);
|
||||
implementations.add(GraphiteMetricsExportAutoConfiguration.class);
|
||||
implementations.add(InfluxMetricsExportAutoConfiguration.class);
|
||||
implementations.add(JmxMetricsExportAutoConfiguration.class);
|
||||
implementations.add(PrometheusMetricsExportAutoConfiguration.class);
|
||||
implementations.add(SimpleMetricsExportAutoConfiguration.class);
|
||||
implementations.add(StatsdMetricsExportAutoConfiguration.class);
|
||||
IMPLEMENTATIONS = Collections.unmodifiableSet(implementations);
|
||||
}
|
||||
|
||||
private MetricsRun() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a function that configures the run to be limited to the {@code simple}
|
||||
* implementation.
|
||||
* @return the function to apply
|
||||
*/
|
||||
public static Function<ApplicationContextRunner, ApplicationContextRunner> simple() {
|
||||
return limitedTo(SimpleMetricsExportAutoConfiguration.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a function that configures the run to be limited to the specified
|
||||
* implementations.
|
||||
* @param implementations the implementations to include
|
||||
* @return the function to apply
|
||||
*/
|
||||
public static Function<ApplicationContextRunner, ApplicationContextRunner> limitedTo(
|
||||
Class<?>... implementations) {
|
||||
return (contextRunner) -> apply(contextRunner, implementations);
|
||||
}
|
||||
|
||||
private static ApplicationContextRunner apply(ApplicationContextRunner contextRunner,
|
||||
Class<?>[] implementations) {
|
||||
for (Class<?> implementation : implementations) {
|
||||
Assert.state(IMPLEMENTATIONS.contains(implementation),
|
||||
"Unknown implementation " + implementation.getName());
|
||||
}
|
||||
return contextRunner
|
||||
.withPropertyValues("management.metrics.use-global-registry=false")
|
||||
.withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class))
|
||||
.withConfiguration(AutoConfigurations.of(implementations));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue