Skip to content

Commit ce358c6

Browse files
committed
Polish "Auto-config support for latest Prometheus client and simpleclient"
See gh-40023
1 parent 7f26b67 commit ce358c6

File tree

12 files changed

+45
-39
lines changed

12 files changed

+45
-39
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,31 @@ public class PrometheusMetricsExportAutoConfiguration {
5858

5959
@Bean
6060
@ConditionalOnMissingBean
61-
public PrometheusConfig prometheusConfig(PrometheusProperties prometheusProperties) {
61+
PrometheusConfig prometheusConfig(PrometheusProperties prometheusProperties) {
6262
return new PrometheusPropertiesConfigAdapter(prometheusProperties);
6363
}
6464

6565
@Bean
6666
@ConditionalOnMissingBean
67-
public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig prometheusConfig,
67+
PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig prometheusConfig,
6868
PrometheusRegistry prometheusRegistry, Clock clock, ObjectProvider<SpanContext> spanContext) {
6969
return new PrometheusMeterRegistry(prometheusConfig, prometheusRegistry, clock, spanContext.getIfAvailable());
7070
}
7171

7272
@Bean
7373
@ConditionalOnMissingBean
74-
public PrometheusRegistry prometheusRegistry() {
74+
PrometheusRegistry prometheusRegistry() {
7575
return new PrometheusRegistry();
7676
}
7777

7878
@Configuration(proxyBeanMethods = false)
7979
@ConditionalOnAvailableEndpoint(endpoint = PrometheusScrapeEndpoint.class)
80-
public static class PrometheusScrapeEndpointConfiguration {
80+
static class PrometheusScrapeEndpointConfiguration {
8181

8282
@SuppressWarnings("removal")
8383
@Bean
8484
@ConditionalOnMissingBean({ PrometheusScrapeEndpoint.class, PrometheusSimpleclientScrapeEndpoint.class })
85-
public PrometheusScrapeEndpoint prometheusEndpoint(PrometheusRegistry prometheusRegistry) {
85+
PrometheusScrapeEndpoint prometheusEndpoint(PrometheusRegistry prometheusRegistry) {
8686
return new PrometheusScrapeEndpoint(prometheusRegistry);
8787
}
8888

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

+14-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager.ShutdownOperation;
2424
import org.springframework.boot.context.properties.ConfigurationProperties;
25+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2526

2627
/**
2728
* {@link ConfigurationProperties @ConfigurationProperties} for configuring metrics export
@@ -54,13 +55,13 @@ public class PrometheusProperties {
5455
/**
5556
* Histogram type for backing DistributionSummary and Timer.
5657
*/
57-
@Deprecated(since = "3.3.0")
58+
@Deprecated(since = "3.3.0", forRemoval = true)
5859
private HistogramFlavor histogramFlavor = HistogramFlavor.Prometheus;
5960

6061
/**
6162
* Additional properties to pass to the Prometheus client.
6263
*/
63-
private final Map<String, String> prometheusProperties = new HashMap<>();
64+
private final Map<String, String> properties = new HashMap<>();
6465

6566
/**
6667
* Step size (i.e. reporting frequency) to use.
@@ -75,6 +76,9 @@ public void setDescriptions(boolean descriptions) {
7576
this.descriptions = descriptions;
7677
}
7778

79+
@Deprecated(since = "3.3.0", forRemoval = true)
80+
@DeprecatedConfigurationProperty(since = "3.3.0",
81+
reason = "No longer supported. Works only when using the Prometheus simpleclient.")
7882
public HistogramFlavor getHistogramFlavor() {
7983
return this.histogramFlavor;
8084
}
@@ -103,8 +107,8 @@ public Pushgateway getPushgateway() {
103107
return this.pushgateway;
104108
}
105109

106-
public Map<String, String> getPrometheusProperties() {
107-
return this.prometheusProperties;
110+
public Map<String, String> getProperties() {
111+
return this.properties;
108112
}
109113

110114
/**
@@ -218,13 +222,16 @@ public void setShutdownOperation(ShutdownOperation shutdownOperation) {
218222

219223
}
220224

225+
/**
226+
* Prometheus Histogram flavor.
227+
*
228+
* @deprecated since 3.3.0 for removal in 3.5.0
229+
*/
230+
@Deprecated(since = "3.3.0", forRemoval = true)
221231
public enum HistogramFlavor {
222232

223233
Prometheus, VictoriaMetrics;
224234

225-
HistogramFlavor() {
226-
}
227-
228235
}
229236

230237
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public Properties prometheusProperties() {
6363
}
6464

6565
private Properties fromPropertiesMap(PrometheusProperties prometheusProperties) {
66-
Map<String, String> map = prometheusProperties.getPrometheusProperties();
66+
Map<String, String> map = prometheusProperties.getProperties();
6767
if (map.isEmpty()) {
6868
return null;
6969
}

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@
6161
* @author David J. M. Karlsen
6262
* @author Jonatan Ivanov
6363
* @since 2.0.0
64-
* @deprecated in favor of {@link PrometheusMetricsExportAutoConfiguration}
64+
* @deprecated since 3.3.0 for removal in 3.5.0 in favor of
65+
* {@link PrometheusMetricsExportAutoConfiguration}
6566
*/
66-
@SuppressWarnings("removal")
6767
@Deprecated(since = "3.3.0", forRemoval = true)
6868
@AutoConfiguration(
6969
before = { CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class },
@@ -76,13 +76,13 @@ public class PrometheusSimpleclientMetricsExportAutoConfiguration {
7676

7777
@Bean
7878
@ConditionalOnMissingBean
79-
public PrometheusConfig simpleclientPrometheusConfig(PrometheusProperties prometheusProperties) {
79+
PrometheusConfig simpleclientPrometheusConfig(PrometheusProperties prometheusProperties) {
8080
return new PrometheusSimpleclientPropertiesConfigAdapter(prometheusProperties);
8181
}
8282

8383
@Bean
8484
@ConditionalOnMissingBean
85-
public io.micrometer.prometheus.PrometheusMeterRegistry simpleclientPrometheusMeterRegistry(
85+
io.micrometer.prometheus.PrometheusMeterRegistry simpleclientPrometheusMeterRegistry(
8686
io.micrometer.prometheus.PrometheusConfig prometheusConfig, CollectorRegistry collectorRegistry,
8787
Clock clock, ObjectProvider<ExemplarSampler> exemplarSamplerProvider) {
8888
return new io.micrometer.prometheus.PrometheusMeterRegistry(prometheusConfig, collectorRegistry, clock,
@@ -91,25 +91,25 @@ public io.micrometer.prometheus.PrometheusMeterRegistry simpleclientPrometheusMe
9191

9292
@Bean
9393
@ConditionalOnMissingBean
94-
public CollectorRegistry collectorRegistry() {
94+
CollectorRegistry collectorRegistry() {
9595
return new CollectorRegistry(true);
9696
}
9797

9898
@Bean
9999
@ConditionalOnMissingBean(ExemplarSampler.class)
100100
@ConditionalOnBean(SpanContextSupplier.class)
101-
public DefaultExemplarSampler exemplarSampler(SpanContextSupplier spanContextSupplier) {
101+
DefaultExemplarSampler exemplarSampler(SpanContextSupplier spanContextSupplier) {
102102
return new DefaultExemplarSampler(spanContextSupplier);
103103
}
104104

105105
@SuppressWarnings("removal")
106106
@Configuration(proxyBeanMethods = false)
107107
@ConditionalOnAvailableEndpoint(endpoint = PrometheusSimpleclientScrapeEndpoint.class)
108-
public static class PrometheusScrapeEndpointConfiguration {
108+
static class PrometheusScrapeEndpointConfiguration {
109109

110110
@Bean
111111
@ConditionalOnMissingBean({ PrometheusSimpleclientScrapeEndpoint.class, PrometheusScrapeEndpoint.class })
112-
public PrometheusSimpleclientScrapeEndpoint prometheusEndpoint(CollectorRegistry collectorRegistry) {
112+
PrometheusSimpleclientScrapeEndpoint prometheusEndpoint(CollectorRegistry collectorRegistry) {
113113
return new PrometheusSimpleclientScrapeEndpoint(collectorRegistry);
114114
}
115115

@@ -122,7 +122,7 @@ public PrometheusSimpleclientScrapeEndpoint prometheusEndpoint(CollectorRegistry
122122
@Configuration(proxyBeanMethods = false)
123123
@ConditionalOnClass(PushGateway.class)
124124
@ConditionalOnProperty(prefix = "management.prometheus.metrics.export.pushgateway", name = "enabled")
125-
public static class PrometheusPushGatewayConfiguration {
125+
static class PrometheusPushGatewayConfiguration {
126126

127127
/**
128128
* The fallback job name. We use 'spring' since there's a history of Prometheus
@@ -133,7 +133,7 @@ public static class PrometheusPushGatewayConfiguration {
133133

134134
@Bean
135135
@ConditionalOnMissingBean
136-
public PrometheusPushGatewayManager prometheusPushGatewayManager(CollectorRegistry collectorRegistry,
136+
PrometheusPushGatewayManager prometheusPushGatewayManager(CollectorRegistry collectorRegistry,
137137
PrometheusProperties prometheusProperties, Environment environment) throws MalformedURLException {
138138
PrometheusProperties.Pushgateway properties = prometheusProperties.getPushgateway();
139139
Duration pushRate = properties.getPushRate();

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.time.Duration;
2020

21-
import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusProperties.HistogramFlavor;
2221
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PropertiesConfigAdapter;
2322

2423
/**
@@ -58,8 +57,7 @@ public io.micrometer.prometheus.HistogramFlavor histogramFlavor() {
5857
}
5958

6059
static io.micrometer.prometheus.HistogramFlavor mapToMicrometerHistogramFlavor(PrometheusProperties properties) {
61-
HistogramFlavor histogramFlavor = properties.getHistogramFlavor();
62-
return switch (histogramFlavor) {
60+
return switch (properties.getHistogramFlavor()) {
6361
case Prometheus -> io.micrometer.prometheus.HistogramFlavor.Prometheus;
6462
case VictoriaMetrics -> io.micrometer.prometheus.HistogramFlavor.VictoriaMetrics;
6563
};

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/prometheus/PrometheusSimpleclientExemplarsAutoConfiguration.java

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
*
3838
* @author Jonatan Ivanov
3939
* @since 3.0.0
40+
* @deprecated since 3.3.0 for removal in 3.5.0
4041
*/
4142
@SuppressWarnings("removal")
4243
@Deprecated(forRemoval = true, since = "3.3.0")

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @author Mirko Sobeck
3232
*/
33-
@SuppressWarnings({ "deprecation" })
33+
@SuppressWarnings({ "deprecation", "removal" })
3434
class PrometheusSimpleclientPropertiesConfigAdapterTests extends
3535
AbstractPropertiesConfigAdapterTests<PrometheusProperties, PrometheusSimpleclientPropertiesConfigAdapter> {
3636

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/prometheus/PrometheusExemplarsAutoConfigurationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void shouldSupplyCustomBeans() {
8686
@Test
8787
void prometheusOpenMetricsOutputWithoutExemplarsOnHistogramCount() {
8888
this.contextRunner.withPropertyValues(
89-
"management.prometheus.metrics.export.prometheus-properties.io.prometheus.exporter.exemplarsOnAllMetricTypes=false")
89+
"management.prometheus.metrics.export.properties.io.prometheus.exporter.exemplarsOnAllMetricTypes=false")
9090
.run((context) -> {
9191
assertThat(context).hasSingleBean(SpanContext.class);
9292
ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class);

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*
3636
* @author Jon Schneider
3737
* @author Johnny Lim
38+
* @author Moritz Halbritter
3839
* @since 2.0.0
3940
*/
4041
@WebEndpoint(id = "prometheus")
@@ -51,17 +52,15 @@ public PrometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) {
5152
}
5253

5354
@ReadOperation(producesFrom = PrometheusOutputFormat.class)
54-
public WebEndpointResponse<String> scrape(PrometheusOutputFormat format, @Nullable Set<String> includedNames) {
55+
public WebEndpointResponse<byte[]> scrape(PrometheusOutputFormat format, @Nullable Set<String> includedNames) {
5556
try {
5657
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(this.nextMetricsScrapeSize);
5758
MetricSnapshots metricSnapshots = (includedNames != null)
5859
? this.prometheusRegistry.scrape(includedNames::contains) : this.prometheusRegistry.scrape();
5960
format.write(outputStream, metricSnapshots);
60-
61-
String scrapePage = outputStream.toString();
62-
this.nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA;
63-
64-
return new WebEndpointResponse<>(scrapePage, format);
61+
byte[] content = outputStream.toByteArray();
62+
this.nextMetricsScrapeSize = content.length + METRICS_SCRAPE_CHARS_EXTRA;
63+
return new WebEndpointResponse<>(content, format);
6564
}
6665
catch (IOException ex) {
6766
throw new IllegalStateException("Writing metrics failed", ex);

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
* @author Jon Schneider
3939
* @author Johnny Lim
4040
* @since 2.0.0
41-
* @deprecated in favor of {@link PrometheusScrapeEndpoint}
41+
* @deprecated since 3.3.0 for removal in 3.5.0 in favor of
42+
* {@link PrometheusScrapeEndpoint}
4243
*/
4344
@Deprecated(since = "3.3.0", forRemoval = true)
4445
@WebEndpoint(id = "prometheus")
@@ -63,10 +64,8 @@ public WebEndpointResponse<String> scrape(TextOutputFormat format, @Nullable Set
6364
? this.collectorRegistry.filteredMetricFamilySamples(includedNames)
6465
: this.collectorRegistry.metricFamilySamples();
6566
format.write(writer, samples);
66-
6767
String scrapePage = writer.toString();
6868
this.nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA;
69-
7069
return new WebEndpointResponse<>(scrapePage, format);
7170
}
7271
catch (IOException ex) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*
3333
* @author Andy Wilkinson
3434
* @since 2.5.0
35-
* @deprecated in favor of {@link PrometheusOutputFormat}
35+
* @deprecated since 3.3.0 for removal in 3.5.0 in favor of {@link PrometheusOutputFormat}
3636
*/
3737
@Deprecated(since = "3.3.0", forRemoval = true)
3838
public enum TextOutputFormat implements Producible<TextOutputFormat> {

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/actuator/metrics.adoc

+3-1
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,13 @@ scrape_configs:
543543
----
544544

545545
https://prometheus.io/docs/prometheus/latest/feature_flags/#exemplars-storage[Prometheus Exemplars] are also supported.
546-
To enable this feature, a `SpanContextSupplier` bean should be present.
546+
To enable this feature, a `SpanContext` bean should be present.
547+
If you're using the deprecated Prometheus simpleclient support and want to enable that feature, a `SpanContextSupplier` bean should be present.
547548
If you use https://micrometer.io/docs/tracing[Micrometer Tracing], this will be auto-configured for you, but you can always create your own if you want.
548549
Please check the https://prometheus.io/docs/prometheus/latest/feature_flags/#exemplars-storage[Prometheus Docs], since this feature needs to be explicitly enabled on Prometheus' side, and it is only supported using the https://github.com/OpenObservability/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md#exemplars[OpenMetrics] format.
549550

550551
For ephemeral or batch jobs that may not exist long enough to be scraped, you can use https://github.com/prometheus/pushgateway[Prometheus Pushgateway] support to expose the metrics to Prometheus.
552+
The Prometheus Pushgateway only works with the deprecated Prometheus simpleclient for now, until the Prometheus client 1.x adds support for it.
551553
To enable Prometheus Pushgateway support, add the following dependency to your project:
552554

553555
[source,xml]

0 commit comments

Comments
 (0)