Skip to content

Commit 8c35bce

Browse files
izeyesnicoll
authored andcommitted
Polish
See gh-44184 Signed-off-by: Johnny Lim <[email protected]>
1 parent d9fc813 commit 8c35bce

File tree

5 files changed

+27
-56
lines changed

5 files changed

+27
-56
lines changed

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ static class PrometheusPushGatewayConfiguration {
107107

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

161161
private String getJob(PrometheusProperties.Pushgateway properties, Environment environment) {
162162
String job = properties.getJob();
163-
job = (job != null) ? job : environment.getProperty("spring.application.name");
164-
return (job != null) ? job : FALLBACK_JOB;
163+
return (job != null) ? job : environment.getProperty("spring.application.name", FALLBACK_JOB);
165164
}
166165

167166
}

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusProperties.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public static class Pushgateway {
109109
private String address = "localhost:9091";
110110

111111
/**
112-
* The scheme to use when pushing metrics.
112+
* Scheme to use when pushing metrics.
113113
*/
114114
private Scheme scheme = Scheme.HTTP;
115115

@@ -124,12 +124,12 @@ public static class Pushgateway {
124124
private String password;
125125

126126
/**
127-
* The token to use for authentication with the Prometheus Pushgateway.
127+
* Token to use for authentication with the Prometheus Pushgateway.
128128
*/
129129
private String token;
130130

131131
/**
132-
* The format to use when pushing metrics.
132+
* Format to use when pushing metrics.
133133
*/
134134
private Format format = Format.PROTOBUF;
135135

Diff for: spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java

+10-33
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
import io.micrometer.prometheusmetrics.PrometheusConfig;
2424
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
2525
import io.prometheus.metrics.exporter.pushgateway.DefaultHttpConnectionFactory;
26-
import io.prometheus.metrics.exporter.pushgateway.HttpConnectionFactory;
2726
import io.prometheus.metrics.exporter.pushgateway.PushGateway;
2827
import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter;
2928
import io.prometheus.metrics.model.registry.PrometheusRegistry;
3029
import io.prometheus.metrics.tracer.common.SpanContext;
3130
import org.assertj.core.api.InstanceOfAssertFactories;
32-
import org.assertj.core.api.ThrowingConsumer;
3331
import org.junit.jupiter.api.Test;
3432
import org.junit.jupiter.api.extension.ExtendWith;
3533

@@ -38,10 +36,8 @@
3836
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint;
3937
import org.springframework.boot.autoconfigure.AutoConfigurations;
4038
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
41-
import org.springframework.boot.test.context.FilteredClassLoader;
4239
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
4340
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
44-
import org.springframework.boot.test.context.runner.ContextConsumer;
4541
import org.springframework.boot.test.system.CapturedOutput;
4642
import org.springframework.boot.test.system.OutputCaptureExtension;
4743
import org.springframework.context.annotation.Bean;
@@ -61,7 +57,6 @@
6157
class PrometheusMetricsExportAutoConfigurationTests {
6258

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

6762
@Test
@@ -177,35 +172,26 @@ void pushGatewayIsNotConfiguredWhenEnabledFlagIsNotSet() {
177172
@Test
178173
@ExtendWith(OutputCaptureExtension.class)
179174
void withPushGatewayEnabled(CapturedOutput output) {
180-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
181-
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true")
175+
this.contextRunner.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true")
182176
.withUserConfiguration(BaseConfiguration.class)
183177
.run((context) -> {
184178
assertThat(output).doesNotContain("Invalid PushGateway base url");
185179
hasGatewayUrl(context, "http://localhost:9091/metrics/job/spring");
180+
assertThat(getPushGateway(context)).extracting("connectionFactory")
181+
.isInstanceOf(DefaultHttpConnectionFactory.class);
186182
});
187183
}
188184

189185
@Test
190186
void withPushGatewayDisabled() {
191-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
192-
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=false")
187+
this.contextRunner.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=false")
193188
.withUserConfiguration(BaseConfiguration.class)
194189
.run((context) -> assertThat(context).doesNotHaveBean(PrometheusPushGatewayManager.class));
195190
}
196191

197-
@Test
198-
void withPushGatewayNoBasicAuth() {
199-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
200-
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true")
201-
.withUserConfiguration(BaseConfiguration.class)
202-
.run(hasHttpConnectionFactory((httpConnectionFactory) -> assertThat(httpConnectionFactory)
203-
.isInstanceOf(DefaultHttpConnectionFactory.class)));
204-
}
205-
206192
@Test
207193
void withCustomPushGatewayAddress() {
208-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
194+
this.contextRunner
209195
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
210196
"management.prometheus.metrics.export.pushgateway.address=localhost:8080")
211197
.withUserConfiguration(BaseConfiguration.class)
@@ -214,7 +200,7 @@ void withCustomPushGatewayAddress() {
214200

215201
@Test
216202
void withCustomScheme() {
217-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
203+
this.contextRunner
218204
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
219205
"management.prometheus.metrics.export.pushgateway.scheme=https")
220206
.withUserConfiguration(BaseConfiguration.class)
@@ -223,7 +209,7 @@ void withCustomScheme() {
223209

224210
@Test
225211
void withCustomFormat() {
226-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
212+
this.contextRunner
227213
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
228214
"management.prometheus.metrics.export.pushgateway.format=text")
229215
.withUserConfiguration(BaseConfiguration.class)
@@ -233,7 +219,7 @@ void withCustomFormat() {
233219

234220
@Test
235221
void withPushGatewayBasicAuth() {
236-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
222+
this.contextRunner
237223
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
238224
"management.prometheus.metrics.export.pushgateway.username=admin",
239225
"management.prometheus.metrics.export.pushgateway.password=secret")
@@ -246,7 +232,7 @@ void withPushGatewayBasicAuth() {
246232

247233
@Test
248234
void withPushGatewayBearerToken() {
249-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
235+
this.contextRunner
250236
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
251237
"management.prometheus.metrics.export.pushgateway.token=a1b2c3d4")
252238
.withUserConfiguration(BaseConfiguration.class)
@@ -257,7 +243,7 @@ void withPushGatewayBearerToken() {
257243

258244
@Test
259245
void failsFastWithBothBearerAndBasicAuthentication() {
260-
this.contextRunner.withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class))
246+
this.contextRunner
261247
.withPropertyValues("management.prometheus.metrics.export.pushgateway.enabled=true",
262248
"management.prometheus.metrics.export.pushgateway.username=alice",
263249
"management.prometheus.metrics.export.pushgateway.token=a1b2c3d4")
@@ -277,15 +263,6 @@ private void hasGatewayUrl(AssertableApplicationContext context, String url) {
277263
}
278264
}
279265

280-
private ContextConsumer<AssertableApplicationContext> hasHttpConnectionFactory(
281-
ThrowingConsumer<HttpConnectionFactory> httpConnectionFactory) {
282-
return (context) -> {
283-
PushGateway pushGateway = getPushGateway(context);
284-
httpConnectionFactory
285-
.accept((HttpConnectionFactory) ReflectionTestUtils.getField(pushGateway, "connectionFactory"));
286-
};
287-
}
288-
289266
private PushGateway getPushGateway(AssertableApplicationContext context) {
290267
assertThat(context).hasSingleBean(PrometheusPushGatewayManager.class);
291268
PrometheusPushGatewayManager gatewayManager = context.getBean(PrometheusPushGatewayManager.class);

Diff for: spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusPushGatewayManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class PrometheusPushGatewayManager {
5555
* @param pushGateway the source push gateway
5656
* @param pushRate the rate at which push operations occur
5757
* @param shutdownOperation the shutdown operation that should be performed when
58-
* context is closed.
58+
* context is closed
5959
* @since 3.5.0
6060
*/
6161
public PrometheusPushGatewayManager(PushGateway pushGateway, Duration pushRate,

Diff for: spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

+11-16
Original file line numberDiff line numberDiff line change
@@ -571,28 +571,23 @@ void asyncTaskExecutorWithCustomNonApplicationTaskExecutor() {
571571
@Test
572572
void customMediaTypes() {
573573
this.contextRunner.withPropertyValues("spring.mvc.contentnegotiation.media-types.yaml:text/yaml")
574-
.run((context) -> {
575-
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
576-
ContentNegotiationManager contentNegotiationManager = (ContentNegotiationManager) ReflectionTestUtils
577-
.getField(adapter, "contentNegotiationManager");
578-
assertThat(contentNegotiationManager.getAllFileExtensions()).contains("yaml");
579-
});
574+
.run((context) -> assertThat(context.getBean(RequestMappingHandlerAdapter.class))
575+
.extracting("contentNegotiationManager",
576+
InstanceOfAssertFactories.type(ContentNegotiationManager.class))
577+
.satisfies((contentNegotiationManager) -> assertThat(contentNegotiationManager.getAllFileExtensions())
578+
.contains("yaml")));
580579
}
581580

582581
@Test
583582
void customDefaultContentTypes() {
584583
this.contextRunner
585584
.withPropertyValues("spring.mvc.contentnegotiation.default-content-types:application/json,application/xml")
586-
.run((context) -> {
587-
RequestMappingHandlerAdapter adapter = context.getBean(RequestMappingHandlerAdapter.class);
588-
ContentNegotiationManager contentNegotiationManager = (ContentNegotiationManager) ReflectionTestUtils
589-
.getField(adapter, "contentNegotiationManager");
590-
assertThat(contentNegotiationManager).isNotNull();
591-
assertThat(contentNegotiationManager.getStrategy(FixedContentNegotiationStrategy.class))
592-
.extracting(FixedContentNegotiationStrategy::getContentTypes)
593-
.asInstanceOf(InstanceOfAssertFactories.list(MediaType.class))
594-
.containsExactly(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML);
595-
});
585+
.run((context) -> assertThat(context.getBean(RequestMappingHandlerAdapter.class))
586+
.extracting("contentNegotiationManager",
587+
InstanceOfAssertFactories.type(ContentNegotiationManager.class))
588+
.satisfies((contentNegotiationManager) -> assertThat(
589+
contentNegotiationManager.getStrategy(FixedContentNegotiationStrategy.class).getContentTypes())
590+
.containsExactly(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML)));
596591
}
597592

598593
@Test

0 commit comments

Comments
 (0)