Permit use of https for configuring Prometheus push gateway

See gh-16084
pull/16278/head
Dmytro Nosan 6 years ago committed by Stephane Nicoll
parent d7d2c345c1
commit eed4e9ea56

@ -16,6 +16,8 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus; package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration; import java.time.Duration;
import java.util.Map; import java.util.Map;
@ -119,13 +121,22 @@ public class PrometheusMetricsExportAutoConfiguration {
PrometheusProperties prometheusProperties, Environment environment) { PrometheusProperties prometheusProperties, Environment environment) {
PrometheusProperties.Pushgateway properties = prometheusProperties PrometheusProperties.Pushgateway properties = prometheusProperties
.getPushgateway(); .getPushgateway();
PushGateway pushGateway = new PushGateway(properties.getBaseUrl());
Duration pushRate = properties.getPushRate(); Duration pushRate = properties.getPushRate();
String job = getJob(properties, environment); String job = getJob(properties, environment);
Map<String, String> groupingKey = properties.getGroupingKey(); Map<String, String> groupingKey = properties.getGroupingKey();
ShutdownOperation shutdownOperation = properties.getShutdownOperation(); ShutdownOperation shutdownOperation = properties.getShutdownOperation();
return new PrometheusPushGatewayManager(pushGateway, collectorRegistry, return new PrometheusPushGatewayManager(
pushRate, job, groupingKey, shutdownOperation); getPushGateway(properties.getBaseUrl()), collectorRegistry, pushRate,
job, groupingKey, shutdownOperation);
}
private PushGateway getPushGateway(String url) {
try {
return new PushGateway(new URL(url));
}
catch (MalformedURLException ex) {
return new PushGateway(url);
}
} }
private String getJob(PrometheusProperties.Pushgateway properties, private String getJob(PrometheusProperties.Pushgateway properties,

@ -26,10 +26,12 @@ import org.springframework.boot.actuate.autoconfigure.web.server.ManagementConte
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager; import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager;
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint; import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -151,8 +153,29 @@ public class PrometheusMetricsExportAutoConfigurationTests {
.withPropertyValues( .withPropertyValues(
"management.metrics.export.prometheus.pushgateway.enabled=true") "management.metrics.export.prometheus.pushgateway.enabled=true")
.withUserConfiguration(BaseConfiguration.class) .withUserConfiguration(BaseConfiguration.class)
.run((context) -> assertThat(context) .run((context) -> hasGatewayURL(context,
.hasSingleBean(PrometheusPushGatewayManager.class)); "http://localhost:9091/metrics/job/"));
}
@Test
public void withCustomPushGatewayURL() {
this.contextRunner
.withConfiguration(
AutoConfigurations.of(ManagementContextAutoConfiguration.class))
.withPropertyValues(
"management.metrics.export.prometheus.pushgateway.enabled=true",
"management.metrics.export.prometheus.pushgateway.base-url=https://localhost:8080/push")
.withUserConfiguration(BaseConfiguration.class)
.run((context) -> hasGatewayURL(context,
"https://localhost:8080/push/metrics/job/"));
}
private void hasGatewayURL(AssertableApplicationContext context, String url) {
assertThat(context).hasSingleBean(PrometheusPushGatewayManager.class);
PrometheusPushGatewayManager gatewayManager = context
.getBean(PrometheusPushGatewayManager.class);
Object pushGateway = ReflectionTestUtils.getField(gatewayManager, "pushGateway");
assertThat(pushGateway).hasFieldOrPropertyWithValue("gatewayBaseURL", url);
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

Loading…
Cancel
Save