mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Tweak performance for Prometheus scraping endpoint
Reduce the number of times capacity growth is needed inside the StringWriter. A typical default SpringBoot Prometheus page has more than 11k characters. Best performance results when no capacity growth is needed at all, so base it on previous metrics page size plus some room for possible extra metric info. See gh-30085
This commit is contained in:
parent
a71d9f5088
commit
e2ebb56911
@ -42,8 +42,12 @@ import org.springframework.lang.Nullable;
|
|||||||
@WebEndpoint(id = "prometheus")
|
@WebEndpoint(id = "prometheus")
|
||||||
public class PrometheusScrapeEndpoint {
|
public class PrometheusScrapeEndpoint {
|
||||||
|
|
||||||
|
private static final int METRICS_SCRAPE_CHARS_EXTRA = 1024;
|
||||||
|
|
||||||
private final CollectorRegistry collectorRegistry;
|
private final CollectorRegistry collectorRegistry;
|
||||||
|
|
||||||
|
private volatile int nextMetricsScrapeSize = 16;
|
||||||
|
|
||||||
public PrometheusScrapeEndpoint(CollectorRegistry collectorRegistry) {
|
public PrometheusScrapeEndpoint(CollectorRegistry collectorRegistry) {
|
||||||
this.collectorRegistry = collectorRegistry;
|
this.collectorRegistry = collectorRegistry;
|
||||||
}
|
}
|
||||||
@ -51,12 +55,16 @@ public class PrometheusScrapeEndpoint {
|
|||||||
@ReadOperation(producesFrom = TextOutputFormat.class)
|
@ReadOperation(producesFrom = TextOutputFormat.class)
|
||||||
public WebEndpointResponse<String> scrape(TextOutputFormat format, @Nullable Set<String> includedNames) {
|
public WebEndpointResponse<String> scrape(TextOutputFormat format, @Nullable Set<String> includedNames) {
|
||||||
try {
|
try {
|
||||||
Writer writer = new StringWriter();
|
Writer writer = new StringWriter(nextMetricsScrapeSize);
|
||||||
Enumeration<MetricFamilySamples> samples = (includedNames != null)
|
Enumeration<MetricFamilySamples> samples = (includedNames != null)
|
||||||
? this.collectorRegistry.filteredMetricFamilySamples(includedNames)
|
? this.collectorRegistry.filteredMetricFamilySamples(includedNames)
|
||||||
: this.collectorRegistry.metricFamilySamples();
|
: this.collectorRegistry.metricFamilySamples();
|
||||||
format.write(writer, samples);
|
format.write(writer, samples);
|
||||||
return new WebEndpointResponse<>(writer.toString(), format);
|
|
||||||
|
String scrapePage = writer.toString();
|
||||||
|
nextMetricsScrapeSize = scrapePage.length() + METRICS_SCRAPE_CHARS_EXTRA;
|
||||||
|
|
||||||
|
return new WebEndpointResponse<>(scrapePage, format);
|
||||||
}
|
}
|
||||||
catch (IOException ex) {
|
catch (IOException ex) {
|
||||||
// This actually never happens since StringWriter doesn't throw an IOException
|
// This actually never happens since StringWriter doesn't throw an IOException
|
||||||
|
Loading…
Reference in New Issue
Block a user