Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish #44184

Closed
wants to merge 1 commit into from
Closed

Polish #44184

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static class PrometheusPushGatewayConfiguration {

/**
* The fallback job name. We use 'spring' since there's a history of Prometheus
* spring integration defaulting to that name from when Prometheus integration
* Spring integration defaulting to that name from when Prometheus integration
* didn't exist in Spring itself.
*/
private static final String FALLBACK_JOB = "spring";
Expand Down Expand Up @@ -160,8 +160,7 @@ private Format format(PrometheusProperties.Pushgateway properties) {

private String getJob(PrometheusProperties.Pushgateway properties, Environment environment) {
String job = properties.getJob();
job = (job != null) ? job : environment.getProperty("spring.application.name");
return (job != null) ? job : FALLBACK_JOB;
return (job != null) ? job : environment.getProperty("spring.application.name", FALLBACK_JOB);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static class Pushgateway {
private String address = "localhost:9091";

/**
* The scheme to use when pushing metrics.
* Scheme to use when pushing metrics.
*/
private Scheme scheme = Scheme.HTTP;

Expand All @@ -124,12 +124,12 @@ public static class Pushgateway {
private String password;

/**
* The token to use for authentication with the Prometheus Pushgateway.
* Token to use for authentication with the Prometheus Pushgateway.
*/
private String token;

/**
* The format to use when pushing metrics.
* Format to use when pushing metrics.
*/
private Format format = Format.PROTOBUF;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
import io.micrometer.prometheusmetrics.PrometheusConfig;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import io.prometheus.metrics.exporter.pushgateway.DefaultHttpConnectionFactory;
import io.prometheus.metrics.exporter.pushgateway.HttpConnectionFactory;
import io.prometheus.metrics.exporter.pushgateway.PushGateway;
import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.tracer.common.SpanContext;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.assertj.core.api.ThrowingConsumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -38,10 +36,8 @@
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.annotation.Bean;
Expand All @@ -61,7 +57,6 @@
class PrometheusMetricsExportAutoConfigurationTests {

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withClassLoader(new FilteredClassLoader("io.micrometer.prometheus.", "io.prometheus.client"))
.withConfiguration(AutoConfigurations.of(PrometheusMetricsExportAutoConfiguration.class));

@Test
Expand Down Expand Up @@ -177,35 +172,26 @@ void pushGatewayIsNotConfiguredWhenEnabledFlagIsNotSet() {
@Test
@ExtendWith(OutputCaptureExtension.class)
void withPushGatewayEnabled(CapturedOutput output) {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true")
this.contextRunner.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true")
.withUserConfiguration(BaseConfiguration.class)
.run((context) -> {
assertThat(output).doesNotContain("Invalid PushGateway base url");
hasGatewayUrl(context, "http://localhost:9091/metrics/job/spring");
assertThat(getPushGateway(context)).extracting("connectionFactory")
.isInstanceOf(DefaultHttpConnectionFactory.class);
});
}

@Test
void withPushGatewayDisabled() {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=false")
this.contextRunner.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=false")
.withUserConfiguration(BaseConfiguration.class)
.run((context) -> assertThat(context).doesNotHaveBean(PrometheusPushGatewayManager.class));
}

@Test
void withPushGatewayNoBasicAuth() {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true")
.withUserConfiguration(BaseConfiguration.class)
.run(hasHttpConnectionFactory((httpConnectionFactory) -> assertThat(httpConnectionFactory)
.isInstanceOf(DefaultHttpConnectionFactory.class)));
}

@Test
void withCustomPushGatewayAddress() {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
this.contextRunner
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
"management.prometheus.metrics.export.pushgateway.address=localhost:8080")
.withUserConfiguration(BaseConfiguration.class)
Expand All @@ -214,7 +200,7 @@ void withCustomPushGatewayAddress() {

@Test
void withCustomScheme() {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
this.contextRunner
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
"management.prometheus.metrics.export.pushgateway.scheme=https")
.withUserConfiguration(BaseConfiguration.class)
Expand All @@ -223,7 +209,7 @@ void withCustomScheme() {

@Test
void withCustomFormat() {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
this.contextRunner
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
"management.prometheus.metrics.export.pushgateway.format=text")
.withUserConfiguration(BaseConfiguration.class)
Expand All @@ -233,7 +219,7 @@ void withCustomFormat() {

@Test
void withPushGatewayBasicAuth() {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
this.contextRunner
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
"management.prometheus.metrics.export.pushgateway.username=admin",
"management.prometheus.metrics.export.pushgateway.password=secret")
Expand All @@ -246,7 +232,7 @@ void withPushGatewayBasicAuth() {

@Test
void withPushGatewayBearerToken() {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
this.contextRunner
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
"management.prometheus.metrics.export.pushgateway.token=a1b2c3d4")
.withUserConfiguration(BaseConfiguration.class)
Expand All @@ -257,7 +243,7 @@ void withPushGatewayBearerToken() {

@Test
void failsFastWithBothBearerAndBasicAuthentication() {
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
this.contextRunner
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
"management.prometheus.metrics.export.pushgateway.username=alice",
"management.prometheus.metrics.export.pushgateway.token=a1b2c3d4")
Expand All @@ -277,15 +263,6 @@ private void hasGatewayUrl(AssertableApplicationContext context, String url) {
}
}

private ContextConsumer<AssertableApplicationContext> hasHttpConnectionFactory(
ThrowingConsumer<HttpConnectionFactory> httpConnectionFactory) {
return (context) -> {
PushGateway pushGateway = getPushGateway(context);
httpConnectionFactory
.accept((HttpConnectionFactory) ReflectionTestUtils.getField(pushGateway, "connectionFactory"));
};
}

private PushGateway getPushGateway(AssertableApplicationContext context) {
assertThat(context).hasSingleBean(PrometheusPushGatewayManager.class);
PrometheusPushGatewayManager gatewayManager = context.getBean(PrometheusPushGatewayManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class PrometheusPushGatewayManager {
* @param pushGateway the source push gateway
* @param pushRate the rate at which push operations occur
* @param shutdownOperation the shutdown operation that should be performed when
* context is closed.
* context is closed
* @since 3.5.0
*/
public PrometheusPushGatewayManager(PushGateway pushGateway, Duration pushRate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,28 +571,23 @@ void asyncTaskExecutorWithCustomNonApplicationTaskExecutor() {
@Test
void customMediaTypes() {
this.contextRunner.withPropertyValues("spring.mvc.contentnegotiation.media-types.yaml:text/yaml")
.run((context) -> {
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
ContentNegotiationManager contentNegotiationManager = (ContentNegotiationManager) ReflectionTestUtils
.getField(adapter, "contentNegotiationManager");
assertThat(contentNegotiationManager.getAllFileExtensions()).contains("yaml");
});
.run((context) -> assertThat(context.getBean(RequestMappingHandlerAdapter.class))
.extracting("contentNegotiationManager",
InstanceOfAssertFactories.type(ContentNegotiationManager.class))
.satisfies((contentNegotiationManager) -> assertThat(contentNegotiationManager.getAllFileExtensions())
.contains("yaml")));
}

@Test
void customDefaultContentTypes() {
this.contextRunner
.withPropertyValues("spring.mvc.contentnegotiation.default-content-types:application/json,application/xml")
.run((context) -> {
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
ContentNegotiationManager contentNegotiationManager = (ContentNegotiationManager) ReflectionTestUtils
.getField(adapter, "contentNegotiationManager");
assertThat(contentNegotiationManager).isNotNull();
assertThat(contentNegotiationManager.getStrategy(FixedContentNegotiationStrategy.class))
.extracting(FixedContentNegotiationStrategy::getContentTypes)
.asInstanceOf(InstanceOfAssertFactories.list(MediaType.class))
.containsExactly(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML);
});
.run((context) -> assertThat(context.getBean(RequestMappingHandlerAdapter.class))
.extracting("contentNegotiationManager",
InstanceOfAssertFactories.type(ContentNegotiationManager.class))
.satisfies((contentNegotiationManager) -> assertThat(
contentNegotiationManager.getStrategy(FixedContentNegotiationStrategy.class).getContentTypes())
.containsExactly(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)));
}

@Test
Expand Down