Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eed4e9e

Browse files
nosansnicoll
authored andcommitted
Permit use of https for configuring Prometheus push gateway
See spring-projectsgh-16084
1 parent d7d2c34 commit eed4e9e

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;
1818

19+
import java.net.MalformedURLException;
20+
import java.net.URL;
1921
import java.time.Duration;
2022
import java.util.Map;
2123

@@ -119,13 +121,22 @@ public PrometheusPushGatewayManager prometheusPushGatewayManager(
119121
PrometheusProperties prometheusProperties, Environment environment) {
120122
PrometheusProperties.Pushgateway properties = prometheusProperties
121123
.getPushgateway();
122-
PushGateway pushGateway = new PushGateway(properties.getBaseUrl());
123124
Duration pushRate = properties.getPushRate();
124125
String job = getJob(properties, environment);
125126
Map<String, String> groupingKey = properties.getGroupingKey();
126127
ShutdownOperation shutdownOperation = properties.getShutdownOperation();
127-
return new PrometheusPushGatewayManager(pushGateway, collectorRegistry,
128-
pushRate, job, groupingKey, shutdownOperation);
128+
return new PrometheusPushGatewayManager(
129+
getPushGateway(properties.getBaseUrl()), collectorRegistry, pushRate,
130+
job, groupingKey, shutdownOperation);
131+
}
132+
133+
private PushGateway getPushGateway(String url) {
134+
try {
135+
return new PushGateway(new URL(url));
136+
}
137+
catch (MalformedURLException ex) {
138+
return new PushGateway(url);
139+
}
129140
}
130141

131142
private String getJob(PrometheusProperties.Pushgateway properties,

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager;
2727
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint;
2828
import org.springframework.boot.autoconfigure.AutoConfigurations;
29+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2930
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3031
import org.springframework.context.annotation.Bean;
3132
import org.springframework.context.annotation.Configuration;
3233
import org.springframework.context.annotation.Import;
34+
import org.springframework.test.util.ReflectionTestUtils;
3335

3436
import static org.assertj.core.api.Assertions.assertThat;
3537

@@ -151,8 +153,29 @@ public void withPushGatewayEnabled() {
151153
.withPropertyValues(
152154
"management.metrics.export.prometheus.pushgateway.enabled=true")
153155
.withUserConfiguration(BaseConfiguration.class)
154-
.run((context) -> assertThat(context)
155-
.hasSingleBean(PrometheusPushGatewayManager.class));
156+
.run((context) -> hasGatewayURL(context,
157+
"http://localhost:9091/metrics/job/"));
158+
}
159+
160+
@Test
161+
public void withCustomPushGatewayURL() {
162+
this.contextRunner
163+
.withConfiguration(
164+
AutoConfigurations.of(ManagementContextAutoConfiguration.class))
165+
.withPropertyValues(
166+
"management.metrics.export.prometheus.pushgateway.enabled=true",
167+
"management.metrics.export.prometheus.pushgateway.base-url=https://localhost:8080/push")
168+
.withUserConfiguration(BaseConfiguration.class)
169+
.run((context) -> hasGatewayURL(context,
170+
"https://localhost:8080/push/metrics/job/"));
171+
}
172+
173+
private void hasGatewayURL(AssertableApplicationContext context, String url) {
174+
assertThat(context).hasSingleBean(PrometheusPushGatewayManager.class);
175+
PrometheusPushGatewayManager gatewayManager = context
176+
.getBean(PrometheusPushGatewayManager.class);
177+
Object pushGateway = ReflectionTestUtils.getField(gatewayManager, "pushGateway");
178+
assertThat(pushGateway).hasFieldOrPropertyWithValue("gatewayBaseURL", url);
156179
}
157180

158181
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /