|
20 | 20 | import io.micrometer.prometheus.PrometheusConfig;
|
21 | 21 | import io.micrometer.prometheus.PrometheusMeterRegistry;
|
22 | 22 | import io.prometheus.client.CollectorRegistry;
|
| 23 | +import org.junit.Rule; |
23 | 24 | import org.junit.Test;
|
24 | 25 |
|
25 | 26 | import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
|
26 | 27 | import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager;
|
27 | 28 | import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint;
|
28 | 29 | import org.springframework.boot.autoconfigure.AutoConfigurations;
|
| 30 | +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; |
29 | 31 | import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
| 32 | +import org.springframework.boot.test.rule.OutputCapture; |
30 | 33 | import org.springframework.context.annotation.Bean;
|
31 | 34 | import org.springframework.context.annotation.Configuration;
|
32 | 35 | import org.springframework.context.annotation.Import;
|
| 36 | +import org.springframework.test.util.ReflectionTestUtils; |
33 | 37 |
|
34 | 38 | import static org.assertj.core.api.Assertions.assertThat;
|
35 | 39 |
|
|
40 | 44 | */
|
41 | 45 | public class PrometheusMetricsExportAutoConfigurationTests {
|
42 | 46 |
|
| 47 | + @Rule |
| 48 | + public final OutputCapture output = new OutputCapture(); |
| 49 | + |
43 | 50 | private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
44 | 51 | .withConfiguration(AutoConfigurations
|
45 | 52 | .of(PrometheusMetricsExportAutoConfiguration.class));
|
@@ -150,9 +157,49 @@ public void withPushGatewayEnabled() {
|
150 | 157 | AutoConfigurations.of(ManagementContextAutoConfiguration.class))
|
151 | 158 | .withPropertyValues(
|
152 | 159 | "management.metrics.export.prometheus.pushgateway.enabled=true")
|
| 160 | + .withUserConfiguration(BaseConfiguration.class).run((context) -> { |
| 161 | + assertThat(this.output.toString()) |
| 162 | + .doesNotContain("Invalid PushGateway base url"); |
| 163 | + hasGatewayURL(context, "http://localhost:9091/metrics/job/"); |
| 164 | + }); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + @Deprecated |
| 169 | + public void withCustomLegacyPushGatewayURL() { |
| 170 | + this.contextRunner |
| 171 | + .withConfiguration( |
| 172 | + AutoConfigurations.of(ManagementContextAutoConfiguration.class)) |
| 173 | + .withPropertyValues( |
| 174 | + "management.metrics.export.prometheus.pushgateway.enabled=true", |
| 175 | + "management.metrics.export.prometheus.pushgateway.base-url=localhost:9090") |
| 176 | + .withUserConfiguration(BaseConfiguration.class).run((context) -> { |
| 177 | + assertThat(this.output.toString()) |
| 178 | + .contains("Invalid PushGateway base url") |
| 179 | + .contains("localhost:9090"); |
| 180 | + hasGatewayURL(context, "http://localhost:9090/metrics/job/"); |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void withCustomPushGatewayURL() { |
| 186 | + this.contextRunner |
| 187 | + .withConfiguration( |
| 188 | + AutoConfigurations.of(ManagementContextAutoConfiguration.class)) |
| 189 | + .withPropertyValues( |
| 190 | + "management.metrics.export.prometheus.pushgateway.enabled=true", |
| 191 | + "management.metrics.export.prometheus.pushgateway.base-url=https://example.com:8080") |
153 | 192 | .withUserConfiguration(BaseConfiguration.class)
|
154 | | - .run((context) -> assertThat(context) |
155 | | - .hasSingleBean(PrometheusPushGatewayManager.class)); |
| 193 | + .run((context) -> hasGatewayURL(context, |
| 194 | + "https://example.com:8080/metrics/job/")); |
| 195 | + } |
| 196 | + |
| 197 | + private void hasGatewayURL(AssertableApplicationContext context, String url) { |
| 198 | + assertThat(context).hasSingleBean(PrometheusPushGatewayManager.class); |
| 199 | + PrometheusPushGatewayManager gatewayManager = context |
| 200 | + .getBean(PrometheusPushGatewayManager.class); |
| 201 | + Object pushGateway = ReflectionTestUtils.getField(gatewayManager, "pushGateway"); |
| 202 | + assertThat(pushGateway).hasFieldOrPropertyWithValue("gatewayBaseURL", url); |
156 | 203 | }
|
157 | 204 |
|
158 | 205 | @Configuration(proxyBeanMethods = false)
|
|
0 commit comments