Skip to content

Commit

Permalink
Use properties from Prometheus config in PrometheusScrapeEndpoint
Browse files Browse the repository at this point in the history
Closes gh-40904
  • Loading branch information
mhalbritter committed Jun 11, 2024
1 parent c470ce2 commit 3c37e45
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ static class PrometheusScrapeEndpointConfiguration {
@SuppressWarnings("removal")
@Bean
@ConditionalOnMissingBean({ PrometheusScrapeEndpoint.class, PrometheusSimpleclientScrapeEndpoint.class })
PrometheusScrapeEndpoint prometheusEndpoint(PrometheusRegistry prometheusRegistry) {
return new PrometheusScrapeEndpoint(prometheusRegistry);
PrometheusScrapeEndpoint prometheusEndpoint(PrometheusRegistry prometheusRegistry,
PrometheusConfig prometheusConfig) {
return new PrometheusScrapeEndpoint(prometheusRegistry, prometheusConfig.prometheusProperties());
}

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

private Properties fromPropertiesMap(PrometheusProperties prometheusProperties) {
Map<String, String> map = prometheusProperties.getProperties();
if (map.isEmpty()) {
Map<String, String> additionalProperties = prometheusProperties.getProperties();
if (additionalProperties.isEmpty()) {
return null;
}
Properties properties = PrometheusConfig.super.prometheusProperties();
properties = (properties != null) ? properties : new Properties();
properties.putAll(map);
if (properties == null) {
properties = new Properties();
}
properties.putAll(additionalProperties);
return properties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.actuate.autoconfigure.endpoint.web.documentation;

import java.util.Properties;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
Expand Down Expand Up @@ -78,7 +80,7 @@ PrometheusScrapeEndpoint endpoint() {
PrometheusMeterRegistry meterRegistry = new PrometheusMeterRegistry((key) -> null, prometheusRegistry,
Clock.SYSTEM);
new JvmMemoryMetrics().bindTo(meterRegistry);
return new PrometheusScrapeEndpoint(prometheusRegistry);
return new PrometheusScrapeEndpoint(prometheusRegistry, new Properties());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;

import io.micrometer.core.instrument.Clock;
import io.micrometer.prometheusmetrics.PrometheusConfig;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exemplars.ExemplarSampler;
import io.prometheus.client.exemplars.tracer.common.SpanContextSupplier;
Expand Down Expand Up @@ -358,8 +359,9 @@ PrometheusSimpleclientScrapeEndpoint customEndpoint(CollectorRegistry collectorR
static class CustomSecondEndpointConfiguration {

@Bean
PrometheusScrapeEndpoint prometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) {
return new PrometheusScrapeEndpoint(prometheusRegistry);
PrometheusScrapeEndpoint prometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry,
PrometheusConfig prometheusConfig) {
return new PrometheusScrapeEndpoint(prometheusRegistry, prometheusConfig.prometheusProperties());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure.metrics.export.prometheus;

import io.micrometer.core.instrument.Clock;
import io.micrometer.prometheusmetrics.PrometheusConfig;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.tracer.common.SpanContext;
Expand Down Expand Up @@ -207,8 +208,9 @@ PrometheusRegistry customPrometheusRegistry() {
static class CustomEndpointConfiguration {

@Bean
PrometheusScrapeEndpoint customEndpoint(PrometheusRegistry prometheusRegistry) {
return new PrometheusScrapeEndpoint(prometheusRegistry);
PrometheusScrapeEndpoint customEndpoint(PrometheusRegistry prometheusRegistry,
PrometheusConfig prometheusConfig) {
return new PrometheusScrapeEndpoint(prometheusRegistry, prometheusConfig.prometheusProperties());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public enum PrometheusOutputFormat implements Producible<PrometheusOutputFormat>
CONTENT_TYPE_004(PrometheusTextFormatWriter.CONTENT_TYPE) {

@Override
void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOException {
EXPOSITION_FORMATS.getPrometheusTextFormatWriter().write(outputStream, snapshots);
void write(ExpositionFormats expositionFormats, OutputStream outputStream, MetricSnapshots snapshots)
throws IOException {
expositionFormats.getPrometheusTextFormatWriter().write(outputStream, snapshots);
}

@Override
Expand All @@ -60,8 +61,9 @@ public boolean isDefault() {
CONTENT_TYPE_OPENMETRICS_100(OpenMetricsTextFormatWriter.CONTENT_TYPE) {

@Override
void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOException {
EXPOSITION_FORMATS.getOpenMetricsTextFormatWriter().write(outputStream, snapshots);
void write(ExpositionFormats expositionFormats, OutputStream outputStream, MetricSnapshots snapshots)
throws IOException {
expositionFormats.getOpenMetricsTextFormatWriter().write(outputStream, snapshots);
}

},
Expand All @@ -72,14 +74,13 @@ void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOExcept
CONTENT_TYPE_PROTOBUF(PrometheusProtobufWriter.CONTENT_TYPE) {

@Override
void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOException {
EXPOSITION_FORMATS.getPrometheusProtobufWriter().write(outputStream, snapshots);
void write(ExpositionFormats expositionFormats, OutputStream outputStream, MetricSnapshots snapshots)
throws IOException {
expositionFormats.getPrometheusProtobufWriter().write(outputStream, snapshots);
}

};

private static final ExpositionFormats EXPOSITION_FORMATS = ExpositionFormats.init();

private final MimeType mimeType;

PrometheusOutputFormat(String mimeType) {
Expand All @@ -91,6 +92,7 @@ public MimeType getProducedMimeType() {
return this.mimeType;
}

abstract void write(OutputStream outputStream, MetricSnapshots snapshots) throws IOException;
abstract void write(ExpositionFormats expositionFormats, OutputStream outputStream, MetricSnapshots snapshots)
throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.config.PrometheusPropertiesLoader;
import io.prometheus.metrics.expositionformats.ExpositionFormats;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;

Expand All @@ -45,10 +49,32 @@ public class PrometheusScrapeEndpoint {

private final PrometheusRegistry prometheusRegistry;

private final ExpositionFormats expositionFormats;

private volatile int nextMetricsScrapeSize = 16;

/**
* Creates a new {@link PrometheusScrapeEndpoint}.
* @param prometheusRegistry the Prometheus registry to use
* @deprecated since 3.3.1 for removal in 3.5.0 in favor of
* {@link #PrometheusScrapeEndpoint(PrometheusRegistry, Properties)}
*/
@Deprecated(since = "3.3.1", forRemoval = true)
public PrometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) {
this(prometheusRegistry, null);
}

/**
* Creates a new {@link PrometheusScrapeEndpoint}.
* @param prometheusRegistry the Prometheus registry to use
* @param expositionFormatsProperties the properties used to configure Prometheus'
* {@link ExpositionFormats}
*/
public PrometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry, Properties expositionFormatsProperties) {
this.prometheusRegistry = prometheusRegistry;
PrometheusProperties prometheusProperties = (expositionFormatsProperties != null)
? PrometheusPropertiesLoader.load(expositionFormatsProperties) : PrometheusPropertiesLoader.load();
this.expositionFormats = ExpositionFormats.init(prometheusProperties.getExporterProperties());
}

@ReadOperation(producesFrom = PrometheusOutputFormat.class)
Expand All @@ -57,7 +83,7 @@ public WebEndpointResponse<byte[]> scrape(PrometheusOutputFormat format, @Nullab
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(this.nextMetricsScrapeSize);
MetricSnapshots metricSnapshots = (includedNames != null)
? this.prometheusRegistry.scrape(includedNames::contains) : this.prometheusRegistry.scrape();
format.write(outputStream, metricSnapshots);
format.write(this.expositionFormats, outputStream, metricSnapshots);
byte[] content = outputStream.toByteArray();
this.nextMetricsScrapeSize = content.length + METRICS_SCRAPE_CHARS_EXTRA;
return new WebEndpointResponse<>(content, format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.actuate.metrics.export.prometheus;

import java.util.Properties;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -141,7 +143,7 @@ static class TestConfiguration {

@Bean
PrometheusScrapeEndpoint prometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) {
return new PrometheusScrapeEndpoint(prometheusRegistry);
return new PrometheusScrapeEndpoint(prometheusRegistry, new Properties());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.actuate.metrics.export.prometheus;

import java.util.Properties;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
Expand Down Expand Up @@ -183,7 +185,7 @@ static class TestConfiguration {

@Bean
PrometheusScrapeEndpoint prometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) {
return new PrometheusScrapeEndpoint(prometheusRegistry);
return new PrometheusScrapeEndpoint(prometheusRegistry, new Properties());
}

@Bean
Expand Down

0 comments on commit 3c37e45

Please sign in to comment.