Skip to content

Commit

Permalink
Merge pull request #30085 from stokpop
Browse files Browse the repository at this point in the history
* pr-30085:
  Polish "Tweak performance for Prometheus scraping endpoint"
  Tweak performance for Prometheus scraping endpoint

Closes gh-30085
  • Loading branch information
mhalbritter committed Mar 9, 2022
2 parents fb45b2b + d14980e commit e84e517
Showing 1 changed file with 10 additions and 2 deletions.
Expand Up @@ -42,21 +42,29 @@
@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;
}

@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
Expand Down

0 comments on commit e84e517

Please sign in to comment.