Disable metrics export in integration tests
This commit introduces a new property to globally disable metrics export. In integration tests, this property is automatically set to disable everything but in-memory metrics. This commit also introduces a `@AutoConfigureMetrics` annotation that can be used for integration tests that require metrics export to operate as they would in an application. See gh-21658
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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
|
||||
*
|
||||
* https://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.test.autoconfigure.metrics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration test to verify behaviour when
|
||||
* {@link AutoConfigureMetrics @AutoConfigureMetrics} is not present on the test class.
|
||||
*
|
||||
* @author Chris Bono
|
||||
*/
|
||||
@SpringBootTest
|
||||
class AutoConfigureMetricsMissingIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
void customizerRunsAndSetsExclusionPropertiesWhenNoAnnotationPresent() {
|
||||
assertThat(this.environment.getProperty("management.metrics.export.enabled")).isEqualTo("false");
|
||||
assertThat(this.environment.getProperty("management.metrics.export.simple.enabled")).isEqualTo("true");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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
|
||||
*
|
||||
* https://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.test.autoconfigure.metrics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration test to verify behaviour when
|
||||
* {@link AutoConfigureMetrics @AutoConfigureMetrics} is present on the test class.
|
||||
*
|
||||
* @author Chris Bono
|
||||
*/
|
||||
@SpringBootTest
|
||||
@AutoConfigureMetrics
|
||||
class AutoConfigureMetricsPresentIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
void customizerDoesNotRunWhenAnnotationPresent() {
|
||||
assertThat(this.environment.containsProperty("management.metrics.export.enabled")).isFalse();
|
||||
assertThat(this.environment.containsProperty("management.metrics.export.simple.enabled")).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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
|
||||
*
|
||||
* https://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.test.autoconfigure.metrics;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Example {@link SpringBootApplication @SpringBootApplication} for use with
|
||||
* {@link AutoConfigureMetrics @AutoConfigureMetrics} tests.
|
||||
*
|
||||
* @author Chris Bono
|
||||
*/
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
class AutoConfigureMetricsSpringBootApplication {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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
|
||||
*
|
||||
* https://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.test.autoconfigure.metrics;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.ContextCustomizer;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link AutoConfigureMetrics} and
|
||||
* {@link ExcludeMetricExportersContextCustomizerFactory} working together.
|
||||
*
|
||||
* @author Chris Bono
|
||||
*/
|
||||
class ExcludeMetricExportersContextCustomizerFactoryTests {
|
||||
|
||||
private ExcludeMetricExportersContextCustomizerFactory factory = new ExcludeMetricExportersContextCustomizerFactory();
|
||||
|
||||
@Test
|
||||
void getContextCustomizerWhenHasNoAnnotationShouldReturnCustomizer() {
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(NoAnnotation.class, null);
|
||||
assertThat(customizer).isNotNull();
|
||||
ConfigurableApplicationContext context = new GenericApplicationContext();
|
||||
customizer.customizeContext(context, null);
|
||||
assertThat(context.getEnvironment().getProperty("management.metrics.export.enabled")).isEqualTo("false");
|
||||
assertThat(context.getEnvironment().getProperty("management.metrics.export.simple.enabled")).isEqualTo("true");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getContextCustomizerWhenHasAnnotationShouldReturnNull() {
|
||||
ContextCustomizer customizer = this.factory.createContextCustomizer(WithAnnotation.class, null);
|
||||
assertThat(customizer).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hashCodeAndEquals() {
|
||||
ContextCustomizer customizer1 = this.factory.createContextCustomizer(NoAnnotation.class, null);
|
||||
ContextCustomizer customizer2 = this.factory.createContextCustomizer(OtherWithNoAnnotation.class, null);
|
||||
assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode());
|
||||
assertThat(customizer1).isEqualTo(customizer1).isEqualTo(customizer2);
|
||||
}
|
||||
|
||||
static class NoAnnotation {
|
||||
|
||||
}
|
||||
|
||||
static class OtherWithNoAnnotation {
|
||||
|
||||
}
|
||||
|
||||
@AutoConfigureMetrics
|
||||
static class WithAnnotation {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user