Skip to content

Commit

Permalink
Polish "Auto-config support for latest Prometheus client and simplecl…
Browse files Browse the repository at this point in the history
…ient"

See gh-40023
  • Loading branch information
mhalbritter committed Apr 5, 2024
1 parent 7f26b67 commit ce358c6
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,31 @@ public class PrometheusMetricsExportAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public PrometheusConfig prometheusConfig(PrometheusProperties prometheusProperties) {
PrometheusConfig prometheusConfig(PrometheusProperties prometheusProperties) {
return new PrometheusPropertiesConfigAdapter(prometheusProperties);
}

@Bean
@ConditionalOnMissingBean
public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig prometheusConfig,
PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig prometheusConfig,
PrometheusRegistry prometheusRegistry, Clock clock, ObjectProvider<SpanContext> spanContext) {
return new PrometheusMeterRegistry(prometheusConfig, prometheusRegistry, clock, spanContext.getIfAvailable());
}

@Bean
@ConditionalOnMissingBean
public PrometheusRegistry prometheusRegistry() {
PrometheusRegistry prometheusRegistry() {
return new PrometheusRegistry();
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnAvailableEndpoint(endpoint = PrometheusScrapeEndpoint.class)
public static class PrometheusScrapeEndpointConfiguration {
static class PrometheusScrapeEndpointConfiguration {

@SuppressWarnings("removal")
@Bean
@ConditionalOnMissingBean({ PrometheusScrapeEndpoint.class, PrometheusSimpleclientScrapeEndpoint.class })
public PrometheusScrapeEndpoint prometheusEndpoint(PrometheusRegistry prometheusRegistry) {
PrometheusScrapeEndpoint prometheusEndpoint(PrometheusRegistry prometheusRegistry) {
return new PrometheusScrapeEndpoint(prometheusRegistry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager.ShutdownOperation;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;

/**
* {@link ConfigurationProperties @ConfigurationProperties} for configuring metrics export
Expand Down Expand Up @@ -54,13 +55,13 @@ public class PrometheusProperties {
/**
* Histogram type for backing DistributionSummary and Timer.
*/
@Deprecated(since = "3.3.0")
@Deprecated(since = "3.3.0", forRemoval = true)
private HistogramFlavor histogramFlavor = HistogramFlavor.Prometheus;

/**
* Additional properties to pass to the Prometheus client.
*/
private final Map<String, String> prometheusProperties = new HashMap<>();
private final Map<String, String> properties = new HashMap<>();

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

@Deprecated(since = "3.3.0", forRemoval = true)
@DeprecatedConfigurationProperty(since = "3.3.0",
reason = "No longer supported. Works only when using the Prometheus simpleclient.")
public HistogramFlavor getHistogramFlavor() {
return this.histogramFlavor;
}
Expand Down Expand Up @@ -103,8 +107,8 @@ public Pushgateway getPushgateway() {
return this.pushgateway;
}

public Map<String, String> getPrometheusProperties() {
return this.prometheusProperties;
public Map<String, String> getProperties() {
return this.properties;
}

/**
Expand Down Expand Up @@ -218,13 +222,16 @@ public void setShutdownOperation(ShutdownOperation shutdownOperation) {

}

/**
* Prometheus Histogram flavor.
*
* @deprecated since 3.3.0 for removal in 3.5.0
*/
@Deprecated(since = "3.3.0", forRemoval = true)
public enum HistogramFlavor {

Prometheus, VictoriaMetrics;

HistogramFlavor() {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Properties prometheusProperties() {
}

private Properties fromPropertiesMap(PrometheusProperties prometheusProperties) {
Map<String, String> map = prometheusProperties.getPrometheusProperties();
Map<String, String> map = prometheusProperties.getProperties();
if (map.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@
* @author David J. M. Karlsen
* @author Jonatan Ivanov
* @since 2.0.0
* @deprecated in favor of {@link PrometheusMetricsExportAutoConfiguration}
* @deprecated since 3.3.0 for removal in 3.5.0 in favor of
* {@link PrometheusMetricsExportAutoConfiguration}
*/
@SuppressWarnings("removal")
@Deprecated(since = "3.3.0", forRemoval = true)
@AutoConfiguration(
before = { CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class },
Expand All @@ -76,13 +76,13 @@ public class PrometheusSimpleclientMetricsExportAutoConfiguration {

@Bean
@ConditionalOnMissingBean
public PrometheusConfig simpleclientPrometheusConfig(PrometheusProperties prometheusProperties) {
PrometheusConfig simpleclientPrometheusConfig(PrometheusProperties prometheusProperties) {
return new PrometheusSimpleclientPropertiesConfigAdapter(prometheusProperties);
}

@Bean
@ConditionalOnMissingBean
public io.micrometer.prometheus.PrometheusMeterRegistry simpleclientPrometheusMeterRegistry(
io.micrometer.prometheus.PrometheusMeterRegistry simpleclientPrometheusMeterRegistry(
io.micrometer.prometheus.PrometheusConfig prometheusConfig, CollectorRegistry collectorRegistry,
Clock clock, ObjectProvider<ExemplarSampler> exemplarSamplerProvider) {
return new io.micrometer.prometheus.PrometheusMeterRegistry(prometheusConfig, collectorRegistry, clock,
Expand All @@ -91,25 +91,25 @@ public io.micrometer.prometheus.PrometheusMeterRegistry simpleclientPrometheusMe

@Bean
@ConditionalOnMissingBean
public CollectorRegistry collectorRegistry() {
CollectorRegistry collectorRegistry() {
return new CollectorRegistry(true);
}

@Bean
@ConditionalOnMissingBean(ExemplarSampler.class)
@ConditionalOnBean(SpanContextSupplier.class)
public DefaultExemplarSampler exemplarSampler(SpanContextSupplier spanContextSupplier) {
DefaultExemplarSampler exemplarSampler(SpanContextSupplier spanContextSupplier) {
return new DefaultExemplarSampler(spanContextSupplier);
}

@SuppressWarnings("removal")
@Configuration(proxyBeanMethods = false)
@ConditionalOnAvailableEndpoint(endpoint = PrometheusSimpleclientScrapeEndpoint.class)
public static class PrometheusScrapeEndpointConfiguration {
static class PrometheusScrapeEndpointConfiguration {

@Bean
@ConditionalOnMissingBean({ PrometheusSimpleclientScrapeEndpoint.class, PrometheusScrapeEndpoint.class })
public PrometheusSimpleclientScrapeEndpoint prometheusEndpoint(CollectorRegistry collectorRegistry) {
PrometheusSimpleclientScrapeEndpoint prometheusEndpoint(CollectorRegistry collectorRegistry) {
return new PrometheusSimpleclientScrapeEndpoint(collectorRegistry);
}

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

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

@Bean
@ConditionalOnMissingBean
public PrometheusPushGatewayManager prometheusPushGatewayManager(CollectorRegistry collectorRegistry,
PrometheusPushGatewayManager prometheusPushGatewayManager(CollectorRegistry collectorRegistry,
PrometheusProperties prometheusProperties, Environment environment) throws MalformedURLException {
PrometheusProperties.Pushgateway properties = prometheusProperties.getPushgateway();
Duration pushRate = properties.getPushRate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.time.Duration;

import org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus.PrometheusProperties.HistogramFlavor;
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PropertiesConfigAdapter;

/**
Expand Down Expand Up @@ -58,8 +57,7 @@ public io.micrometer.prometheus.HistogramFlavor histogramFlavor() {
}

static io.micrometer.prometheus.HistogramFlavor mapToMicrometerHistogramFlavor(PrometheusProperties properties) {
HistogramFlavor histogramFlavor = properties.getHistogramFlavor();
return switch (histogramFlavor) {
return switch (properties.getHistogramFlavor()) {
case Prometheus -> io.micrometer.prometheus.HistogramFlavor.Prometheus;
case VictoriaMetrics -> io.micrometer.prometheus.HistogramFlavor.VictoriaMetrics;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*
* @author Jonatan Ivanov
* @since 3.0.0
* @deprecated since 3.3.0 for removal in 3.5.0
*/
@SuppressWarnings("removal")
@Deprecated(forRemoval = true, since = "3.3.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @author Mirko Sobeck
*/
@SuppressWarnings({ "deprecation" })
@SuppressWarnings({ "deprecation", "removal" })
class PrometheusSimpleclientPropertiesConfigAdapterTests extends
AbstractPropertiesConfigAdapterTests<PrometheusProperties, PrometheusSimpleclientPropertiesConfigAdapter> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void shouldSupplyCustomBeans() {
@Test
void prometheusOpenMetricsOutputWithoutExemplarsOnHistogramCount() {
this.contextRunner.withPropertyValues(
"management.prometheus.metrics.export.prometheus-properties.io.prometheus.exporter.exemplarsOnAllMetricTypes=false")
"management.prometheus.metrics.export.properties.io.prometheus.exporter.exemplarsOnAllMetricTypes=false")
.run((context) -> {
assertThat(context).hasSingleBean(SpanContext.class);
ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*
* @author Jon Schneider
* @author Johnny Lim
* @author Moritz Halbritter
* @since 2.0.0
*/
@WebEndpoint(id = "prometheus")
Expand All @@ -51,17 +52,15 @@ public PrometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) {
}

@ReadOperation(producesFrom = PrometheusOutputFormat.class)
public WebEndpointResponse<String> scrape(PrometheusOutputFormat format, @Nullable Set<String> includedNames) {
public WebEndpointResponse<byte[]> scrape(PrometheusOutputFormat format, @Nullable Set<String> includedNames) {
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(this.nextMetricsScrapeSize);
MetricSnapshots metricSnapshots = (includedNames != null)
? this.prometheusRegistry.scrape(includedNames::contains) : this.prometheusRegistry.scrape();
format.write(outputStream, metricSnapshots);

String scrapePage = outputStream.toString();
this.nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA;

return new WebEndpointResponse<>(scrapePage, format);
byte[] content = outputStream.toByteArray();
this.nextMetricsScrapeSize = content.length + METRICS_SCRAPE_CHARS_EXTRA;
return new WebEndpointResponse<>(content, format);
}
catch (IOException ex) {
throw new IllegalStateException("Writing metrics failed", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
* @author Jon Schneider
* @author Johnny Lim
* @since 2.0.0
* @deprecated in favor of {@link PrometheusScrapeEndpoint}
* @deprecated since 3.3.0 for removal in 3.5.0 in favor of
* {@link PrometheusScrapeEndpoint}
*/
@Deprecated(since = "3.3.0", forRemoval = true)
@WebEndpoint(id = "prometheus")
Expand All @@ -63,10 +64,8 @@ public WebEndpointResponse<String> scrape(TextOutputFormat format, @Nullable Set
? this.collectorRegistry.filteredMetricFamilySamples(includedNames)
: this.collectorRegistry.metricFamilySamples();
format.write(writer, samples);

String scrapePage = writer.toString();
this.nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA;

return new WebEndpointResponse<>(scrapePage, format);
}
catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author Andy Wilkinson
* @since 2.5.0
* @deprecated in favor of {@link PrometheusOutputFormat}
* @deprecated since 3.3.0 for removal in 3.5.0 in favor of {@link PrometheusOutputFormat}
*/
@Deprecated(since = "3.3.0", forRemoval = true)
public enum TextOutputFormat implements Producible<TextOutputFormat> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,13 @@ scrape_configs:
----

https://prometheus.io/docs/prometheus/latest/feature_flags/#exemplars-storage[Prometheus Exemplars] are also supported.
To enable this feature, a `SpanContextSupplier` bean should be present.
To enable this feature, a `SpanContext` bean should be present.
If you're using the deprecated Prometheus simpleclient support and want to enable that feature, a `SpanContextSupplier` bean should be present.
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.
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.

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.
The Prometheus Pushgateway only works with the deprecated Prometheus simpleclient for now, until the Prometheus client 1.x adds support for it.
To enable Prometheus Pushgateway support, add the following dependency to your project:

[source,xml]
Expand Down

0 comments on commit ce358c6

Please sign in to comment.