diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java index eb179c543842..89cc2822d3e0 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java @@ -42,8 +42,12 @@ @WebEndpoint(id = "prometheus") public class PrometheusScrapeEndpoint { + private static final int METRICS_SCRAPE_CHARS_EXTRA = 1024; + private final CollectorRegistry collectorRegistry; + private volatile int nextMetricsScrapeSize = 16; + public PrometheusScrapeEndpoint(CollectorRegistry collectorRegistry) { this.collectorRegistry = collectorRegistry; } @@ -51,12 +55,16 @@ public PrometheusScrapeEndpoint(CollectorRegistry collectorRegistry) { @ReadOperation(producesFrom = TextOutputFormat.class) public WebEndpointResponse scrape(TextOutputFormat format, @Nullable Set includedNames) { try { - Writer writer = new StringWriter(); + Writer writer = new StringWriter(this.nextMetricsScrapeSize); Enumeration samples = (includedNames != null) ? this.collectorRegistry.filteredMetricFamilySamples(includedNames) : this.collectorRegistry.metricFamilySamples(); format.write(writer, samples); - return new WebEndpointResponse<>(writer.toString(), format); + + String scrapePage = writer.toString(); + this.nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA; + + return new WebEndpointResponse<>(scrapePage, format); } catch (IOException ex) { // This actually never happens since StringWriter doesn't throw an IOException