mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
Polish "Auto-config support for latest Prometheus client and simpleclient"
See gh-40023
This commit is contained in:
parent
7f26b67e61
commit
ce358c601b
@ -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);
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ import java.util.Map;
|
||||
|
||||
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
|
||||
@ -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.
|
||||
@ -75,6 +76,9 @@ public class PrometheusProperties {
|
||||
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;
|
||||
}
|
||||
@ -103,8 +107,8 @@ public class PrometheusProperties {
|
||||
return this.pushgateway;
|
||||
}
|
||||
|
||||
public Map<String, String> getPrometheusProperties() {
|
||||
return this.prometheusProperties;
|
||||
public Map<String, String> getProperties() {
|
||||
return this.properties;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -218,13 +222,16 @@ public class PrometheusProperties {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ class PrometheusPropertiesConfigAdapter extends PropertiesConfigAdapter<Promethe
|
||||
}
|
||||
|
||||
private Properties fromPropertiesMap(PrometheusProperties prometheusProperties) {
|
||||
Map<String, String> map = prometheusProperties.getPrometheusProperties();
|
||||
Map<String, String> map = prometheusProperties.getProperties();
|
||||
if (map.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
@ -61,9 +61,9 @@ import org.springframework.util.StringUtils;
|
||||
* @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 },
|
||||
@ -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,
|
||||
@ -91,25 +91,25 @@ public class PrometheusSimpleclientMetricsExportAutoConfiguration {
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@ -122,7 +122,7 @@ public class PrometheusSimpleclientMetricsExportAutoConfiguration {
|
||||
@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
|
||||
@ -133,7 +133,7 @@ public class PrometheusSimpleclientMetricsExportAutoConfiguration {
|
||||
|
||||
@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();
|
||||
|
@ -18,7 +18,6 @@ package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -58,8 +57,7 @@ class PrometheusSimpleclientPropertiesConfigAdapter extends PropertiesConfigAdap
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
|
@ -37,6 +37,7 @@ import org.springframework.util.function.SingletonSupplier;
|
||||
*
|
||||
* @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")
|
||||
|
@ -30,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Mirko Sobeck
|
||||
*/
|
||||
@SuppressWarnings({ "deprecation" })
|
||||
@SuppressWarnings({ "deprecation", "removal" })
|
||||
class PrometheusSimpleclientPropertiesConfigAdapterTests extends
|
||||
AbstractPropertiesConfigAdapterTests<PrometheusProperties, PrometheusSimpleclientPropertiesConfigAdapter> {
|
||||
|
||||
|
@ -86,7 +86,7 @@ class PrometheusExemplarsAutoConfigurationTests {
|
||||
@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);
|
||||
|
@ -35,6 +35,7 @@ import org.springframework.lang.Nullable;
|
||||
*
|
||||
* @author Jon Schneider
|
||||
* @author Johnny Lim
|
||||
* @author Moritz Halbritter
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@WebEndpoint(id = "prometheus")
|
||||
@ -51,17 +52,15 @@ public class PrometheusScrapeEndpoint {
|
||||
}
|
||||
|
||||
@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);
|
||||
|
@ -38,7 +38,8 @@ import org.springframework.lang.Nullable;
|
||||
* @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")
|
||||
@ -63,10 +64,8 @@ public class PrometheusSimpleclientScrapeEndpoint {
|
||||
? 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) {
|
||||
|
@ -32,7 +32,7 @@ import org.springframework.util.MimeTypeUtils;
|
||||
*
|
||||
* @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> {
|
||||
|
@ -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]
|
||||
|
Loading…
Reference in New Issue
Block a user