Skip to content

Commit

Permalink
Use properties from Prometheus config in PrometheusScrapeEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Jun 10, 2024
1 parent f883a26 commit ee7709c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 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 @@ -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(OutputStream outputStream, MetricSnapshots snapshots, ExpositionFormats expositionFormats)
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(OutputStream outputStream, MetricSnapshots snapshots, ExpositionFormats expositionFormats)
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(OutputStream outputStream, MetricSnapshots snapshots, ExpositionFormats expositionFormats)
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(OutputStream outputStream, MetricSnapshots snapshots, ExpositionFormats expositionFormats)
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(outputStream, metricSnapshots, this.expositionFormats);
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 ee7709c

Please sign in to comment.