Merge branch '2.5.x' into 2.6.x

Closes gh-30125
This commit is contained in:
Moritz Halbritter 2022-03-09 12:10:05 +01:00
commit 16f069ef47
1 changed files with 10 additions and 2 deletions

View File

@ -42,8 +42,12 @@ import org.springframework.lang.Nullable;
@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 class PrometheusScrapeEndpoint {
@ReadOperation(producesFrom = TextOutputFormat.class)
public WebEndpointResponse<String> scrape(TextOutputFormat format, @Nullable Set<String> includedNames) {
try {
Writer writer = new StringWriter();
Writer writer = new StringWriter(this.nextMetricsScrapeSize);
Enumeration<MetricFamilySamples> 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