Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Jun 10, 2024
1 parent 781d7b0 commit 6947d33
Show file tree
Hide file tree
Showing 8 changed files with 47 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);
}

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

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
import io.micrometer.prometheusmetrics.PrometheusConfig;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
Expand Down Expand Up @@ -78,7 +79,7 @@ PrometheusScrapeEndpoint endpoint() {
PrometheusMeterRegistry meterRegistry = new PrometheusMeterRegistry((key) -> null, prometheusRegistry,
Clock.SYSTEM);
new JvmMemoryMetrics().bindTo(meterRegistry);
return new PrometheusScrapeEndpoint(prometheusRegistry);
return new PrometheusScrapeEndpoint(prometheusRegistry, PrometheusConfig.DEFAULT);
}

}
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);
}

@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);
}

}
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 @@ -20,6 +20,10 @@
import java.io.IOException;
import java.util.Set;

import io.micrometer.prometheusmetrics.PrometheusConfig;
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,24 @@ 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
*/
@Deprecated(since = "3.3.1", forRemoval = true)
public PrometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry) {
this(prometheusRegistry, PrometheusConfig.DEFAULT);
}

public PrometheusScrapeEndpoint(PrometheusRegistry prometheusRegistry, PrometheusConfig prometheusConfig) {
this.prometheusRegistry = prometheusRegistry;
PrometheusProperties properties = PrometheusPropertiesLoader.load(prometheusConfig.prometheusProperties());
this.expositionFormats = ExpositionFormats.init(properties.getExporterProperties());
}

@ReadOperation(producesFrom = PrometheusOutputFormat.class)
Expand All @@ -57,7 +75,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 @@ -19,6 +19,7 @@
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.prometheusmetrics.PrometheusConfig;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
import io.prometheus.metrics.expositionformats.PrometheusProtobufWriter;
Expand Down Expand Up @@ -141,7 +142,7 @@ static class TestConfiguration {

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

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.prometheusmetrics.PrometheusConfig;
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.metrics.expositionformats.OpenMetricsTextFormatWriter;
Expand Down Expand Up @@ -183,7 +184,7 @@ static class TestConfiguration {

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

@Bean
Expand Down

0 comments on commit 6947d33

Please sign in to comment.