Skip to content

Commit f0053d2

Browse files
committed
Merge pull request #"Tweak performance for Prometheus scraping endpoint"\n\nSee gh-30085 from
* 2.6.x-prometheus-performance: Polish "Tweak performance for Prometheus scraping endpoint"\n\nSee gh-30085 Tweak performance for Prometheus scraping endpoint\n\nCloses gh-"Tweak performance for Prometheus scraping endpoint"\n\nSee gh-30085
2 parents a71d9f5 + 93f31ee commit f0053d2

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/export/prometheus/PrometheusScrapeEndpoint.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,29 @@
4242
@WebEndpoint(id = "prometheus")
4343
public class PrometheusScrapeEndpoint {
4444

45+
private static final int METRICS_SCRAPE_CHARS_EXTRA = 1024;
46+
4547
private final CollectorRegistry collectorRegistry;
4648

49+
private volatile int nextMetricsScrapeSize = 16;
50+
4751
public PrometheusScrapeEndpoint(CollectorRegistry collectorRegistry) {
4852
this.collectorRegistry = collectorRegistry;
4953
}
5054

5155
@ReadOperation(producesFrom = TextOutputFormat.class)
5256
public WebEndpointResponse<String> scrape(TextOutputFormat format, @Nullable Set<String> includedNames) {
5357
try {
54-
Writer writer = new StringWriter();
58+
Writer writer = new StringWriter(this.nextMetricsScrapeSize);
5559
Enumeration<MetricFamilySamples> samples = (includedNames != null)
5660
? this.collectorRegistry.filteredMetricFamilySamples(includedNames)
5761
: this.collectorRegistry.metricFamilySamples();
5862
format.write(writer, samples);
59-
return new WebEndpointResponse<>(writer.toString(), format);
63+
64+
String scrapePage = writer.toString();
65+
this.nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA;
66+
67+
return new WebEndpointResponse<>(scrapePage, format);
6068
}
6169
catch (IOException ex) {
6270
// This actually never happens since StringWriter doesn't throw an IOException

0 commit comments

Comments
 (0)