From eed4e9ea56c5e07b46df42d7998482fb99bb0feb Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Mon, 4 Mar 2019 11:42:56 +0200 Subject: [PATCH 1/2] Permit use of https for configuring Prometheus push gateway See gh-16084 --- ...metheusMetricsExportAutoConfiguration.java | 17 +++++++++--- ...usMetricsExportAutoConfigurationTests.java | 27 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java index 03f43a8dfd..7f15df7194 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java @@ -16,6 +16,8 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus; +import java.net.MalformedURLException; +import java.net.URL; import java.time.Duration; import java.util.Map; @@ -119,13 +121,22 @@ public class PrometheusMetricsExportAutoConfiguration { PrometheusProperties prometheusProperties, Environment environment) { PrometheusProperties.Pushgateway properties = prometheusProperties .getPushgateway(); - PushGateway pushGateway = new PushGateway(properties.getBaseUrl()); Duration pushRate = properties.getPushRate(); String job = getJob(properties, environment); Map groupingKey = properties.getGroupingKey(); ShutdownOperation shutdownOperation = properties.getShutdownOperation(); - return new PrometheusPushGatewayManager(pushGateway, collectorRegistry, - pushRate, job, groupingKey, shutdownOperation); + return new PrometheusPushGatewayManager( + 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, diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java index b3b110389e..18305538a0 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java @@ -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.PrometheusScrapeEndpoint; 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.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -151,8 +153,29 @@ public class PrometheusMetricsExportAutoConfigurationTests { .withPropertyValues( "management.metrics.export.prometheus.pushgateway.enabled=true") .withUserConfiguration(BaseConfiguration.class) - .run((context) -> assertThat(context) - .hasSingleBean(PrometheusPushGatewayManager.class)); + .run((context) -> hasGatewayURL(context, + "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) From f4c4b1d3f015c38543c50aae8dde02b57f60c1b5 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 29 Mar 2019 17:03:28 +0100 Subject: [PATCH 2/2] Polish "Permit use of https for configuring Prometheus push gateway" Closes gh-16084 --- ...metheusMetricsExportAutoConfiguration.java | 8 +++++ .../prometheus/PrometheusProperties.java | 4 +-- ...usMetricsExportAutoConfigurationTests.java | 34 ++++++++++++++++--- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java index 7f15df7194..63b0795057 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfiguration.java @@ -26,6 +26,8 @@ import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.exporter.PushGateway; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnExposedEndpoint; @@ -107,6 +109,9 @@ public class PrometheusMetricsExportAutoConfiguration { @ConditionalOnProperty(prefix = "management.metrics.export.prometheus.pushgateway", name = "enabled") public static class PrometheusPushGatewayConfiguration { + private static final Log logger = LogFactory + .getLog(PrometheusPushGatewayConfiguration.class); + /** * The fallback job name. We use 'spring' since there's a history of Prometheus * spring integration defaulting to that name from when Prometheus integration @@ -135,6 +140,9 @@ public class PrometheusMetricsExportAutoConfiguration { return new PushGateway(new URL(url)); } catch (MalformedURLException ex) { + logger.warn(String.format( + "Invalid PushGateway base url '%s': update your configuration to a valid URL", + url)); return new PushGateway(url); } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusProperties.java index 60ddd8e1f2..3679e8a450 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,7 +83,7 @@ public class PrometheusProperties { /** * Base URL for the Pushgateway. */ - private String baseUrl = "localhost:9091"; + private String baseUrl = "http://localhost:9091"; /** * Frequency with which to push metrics. diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java index 18305538a0..9684daf44d 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java @@ -20,6 +20,7 @@ import io.micrometer.core.instrument.Clock; import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.CollectorRegistry; +import org.junit.Rule; import org.junit.Test; import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration; @@ -28,6 +29,7 @@ import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScra 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.rule.OutputCapture; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -42,6 +44,9 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class PrometheusMetricsExportAutoConfigurationTests { + @Rule + public final OutputCapture output = new OutputCapture(); + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations .of(PrometheusMetricsExportAutoConfiguration.class)); @@ -152,9 +157,28 @@ public class PrometheusMetricsExportAutoConfigurationTests { AutoConfigurations.of(ManagementContextAutoConfiguration.class)) .withPropertyValues( "management.metrics.export.prometheus.pushgateway.enabled=true") - .withUserConfiguration(BaseConfiguration.class) - .run((context) -> hasGatewayURL(context, - "http://localhost:9091/metrics/job/")); + .withUserConfiguration(BaseConfiguration.class).run((context) -> { + assertThat(this.output.toString()) + .doesNotContain("Invalid PushGateway base url"); + hasGatewayURL(context, "http://localhost:9091/metrics/job/"); + }); + } + + @Test + @Deprecated + public void withCustomLegacyPushGatewayURL() { + this.contextRunner + .withConfiguration( + AutoConfigurations.of(ManagementContextAutoConfiguration.class)) + .withPropertyValues( + "management.metrics.export.prometheus.pushgateway.enabled=true", + "management.metrics.export.prometheus.pushgateway.base-url=localhost:9090") + .withUserConfiguration(BaseConfiguration.class).run((context) -> { + assertThat(this.output.toString()) + .contains("Invalid PushGateway base url") + .contains("localhost:9090"); + hasGatewayURL(context, "http://localhost:9090/metrics/job/"); + }); } @Test @@ -164,10 +188,10 @@ public class PrometheusMetricsExportAutoConfigurationTests { AutoConfigurations.of(ManagementContextAutoConfiguration.class)) .withPropertyValues( "management.metrics.export.prometheus.pushgateway.enabled=true", - "management.metrics.export.prometheus.pushgateway.base-url=https://localhost:8080/push") + "management.metrics.export.prometheus.pushgateway.base-url=https://example.com:8080") .withUserConfiguration(BaseConfiguration.class) .run((context) -> hasGatewayURL(context, - "https://localhost:8080/push/metrics/job/")); + "https://example.com:8080/metrics/job/")); } private void hasGatewayURL(AssertableApplicationContext context, String url) {