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 c2b35e1

Browse files
committed
Merge pull request spring-projects#16084 from nosan
* pr/16084: Polish "Permit use of https for configuring Prometheus push gateway" Permit use of https for configuring Prometheus push gateway
2 parents d7d2c34 + f4c4b1d commit c2b35e1

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

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

Lines changed: 22 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

@@ -24,6 +26,8 @@
2426
import io.micrometer.prometheus.PrometheusMeterRegistry;
2527
import io.prometheus.client.CollectorRegistry;
2628
import io.prometheus.client.exporter.PushGateway;
29+
import org.apache.commons.logging.Log;
30+
import org.apache.commons.logging.LogFactory;
2731

2832
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
2933
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnExposedEndpoint;
@@ -105,6 +109,9 @@ public PrometheusScrapeEndpoint prometheusEndpoint(
105109
@ConditionalOnProperty(prefix = "management.metrics.export.prometheus.pushgateway", name = "enabled")
106110
public static class PrometheusPushGatewayConfiguration {
107111

112+
private static final Log logger = LogFactory
113+
.getLog(PrometheusPushGatewayConfiguration.class);
114+
108115
/**
109116
* The fallback job name. We use 'spring' since there's a history of Prometheus
110117
* spring integration defaulting to that name from when Prometheus integration
@@ -119,13 +126,25 @@ public PrometheusPushGatewayManager prometheusPushGatewayManager(
119126
PrometheusProperties prometheusProperties, Environment environment) {
120127
PrometheusProperties.Pushgateway properties = prometheusProperties
121128
.getPushgateway();
122-
PushGateway pushGateway = new PushGateway(properties.getBaseUrl());
123129
Duration pushRate = properties.getPushRate();
124130
String job = getJob(properties, environment);
125131
Map<String, String> groupingKey = properties.getGroupingKey();
126132
ShutdownOperation shutdownOperation = properties.getShutdownOperation();
127-
return new PrometheusPushGatewayManager(pushGateway, collectorRegistry,
128-
pushRate, job, groupingKey, shutdownOperation);
133+
return new PrometheusPushGatewayManager(
134+
getPushGateway(properties.getBaseUrl()), collectorRegistry, pushRate,
135+
job, groupingKey, shutdownOperation);
136+
}
137+
138+
private PushGateway getPushGateway(String url) {
139+
try {
140+
return new PushGateway(new URL(url));
141+
}
142+
catch (MalformedURLException ex) {
143+
logger.warn(String.format(
144+
"Invalid PushGateway base url '%s': update your configuration to a valid URL",
145+
url));
146+
return new PushGateway(url);
147+
}
129148
}
130149

131150
private String getJob(PrometheusProperties.Pushgateway properties,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,7 +83,7 @@ public static class Pushgateway {
8383
/**
8484
* Base URL for the Pushgateway.
8585
*/
86-
private String baseUrl = "localhost:9091";
86+
private String baseUrl = "http://localhost:9091";
8787

8888
/**
8989
* Frequency with which to push metrics.

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,20 @@
2020
import io.micrometer.prometheus.PrometheusConfig;
2121
import io.micrometer.prometheus.PrometheusMeterRegistry;
2222
import io.prometheus.client.CollectorRegistry;
23+
import org.junit.Rule;
2324
import org.junit.Test;
2425

2526
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration;
2627
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager;
2728
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScrapeEndpoint;
2829
import org.springframework.boot.autoconfigure.AutoConfigurations;
30+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2931
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
32+
import org.springframework.boot.test.rule.OutputCapture;
3033
import org.springframework.context.annotation.Bean;
3134
import org.springframework.context.annotation.Configuration;
3235
import org.springframework.context.annotation.Import;
36+
import org.springframework.test.util.ReflectionTestUtils;
3337

3438
import static org.assertj.core.api.Assertions.assertThat;
3539

@@ -40,6 +44,9 @@
4044
*/
4145
public class PrometheusMetricsExportAutoConfigurationTests {
4246

47+
@Rule
48+
public final OutputCapture output = new OutputCapture();
49+
4350
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
4451
.withConfiguration(AutoConfigurations
4552
.of(PrometheusMetricsExportAutoConfiguration.class));
@@ -150,9 +157,49 @@ public void withPushGatewayEnabled() {
150157
AutoConfigurations.of(ManagementContextAutoConfiguration.class))
151158
.withPropertyValues(
152159
"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")
153192
.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);
156203
}
157204

158205
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
(0)

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