Skip to content

Commit ab8e41f

Browse files
authored
Update PrometheusScrapeHandler compression handling (#1493)
Resolves #1488 Don't create response buffer for compressed responses Signed-off-by: Jay DeLuca <[email protected]>
1 parent df522e9 commit ab8e41f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,22 @@ public void handleRequest(PrometheusHttpExchange exchange) throws IOException {
5757
if (writeDebugResponse(snapshots, exchange)) {
5858
return;
5959
}
60-
ByteArrayOutputStream responseBuffer =
61-
new ByteArrayOutputStream(lastResponseSize.get() + 1024);
6260
String acceptHeader = request.getHeader("Accept");
6361
ExpositionFormatWriter writer = expositionFormats.findWriter(acceptHeader);
64-
writer.write(responseBuffer, snapshots);
65-
lastResponseSize.set(responseBuffer.size());
6662
PrometheusHttpResponse response = exchange.getResponse();
6763
response.setHeader("Content-Type", writer.getContentType());
6864

6965
if (shouldUseCompression(request)) {
7066
response.setHeader("Content-Encoding", "gzip");
7167
try (GZIPOutputStream gzipOutputStream =
7268
new GZIPOutputStream(response.sendHeadersAndGetBody(200, 0))) {
73-
responseBuffer.writeTo(gzipOutputStream);
69+
writer.write(gzipOutputStream, snapshots);
7470
}
7571
} else {
72+
ByteArrayOutputStream responseBuffer =
73+
new ByteArrayOutputStream(lastResponseSize.get() + 1024);
74+
writer.write(responseBuffer, snapshots);
75+
lastResponseSize.set(responseBuffer.size());
7676
int contentLength = responseBuffer.size();
7777
if (contentLength > 0) {
7878
response.setHeader("Content-Length", String.valueOf(contentLength));

0 commit comments

Comments
 (0)