Skip to content

Commit 4618df7

Browse files
authored
Make sure GZIPOutputStream is always closed (prometheus#598)
Signed-off-by: Andrey Turbanov <turbanoff@gmail.com>
1 parent 3a54fdf commit 4618df7

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

  • simpleclient_httpserver/src/main/java/io/prometheus/client/exporter

‎simpleclient_httpserver/src/main/java/io/prometheus/client/exporter/HTTPServer.java‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
import java.net.InetSocketAddress;
1111
import java.net.URLDecoder;
1212
import java.nio.charset.Charset;
13+
import java.util.HashSet;
1314
import java.util.List;
1415
import java.util.Set;
15-
import java.util.HashSet;
16-
import java.util.concurrent.ExecutorService;
1716
import java.util.concurrent.ExecutionException;
17+
import java.util.concurrent.ExecutorService;
1818
import java.util.concurrent.Executors;
1919
import java.util.concurrent.FutureTask;
2020
import java.util.concurrent.ThreadFactory;
2121
import java.util.concurrent.atomic.AtomicInteger;
2222
import java.util.zip.GZIPOutputStream;
2323

24+
import com.sun.net.httpserver.HttpExchange;
2425
import com.sun.net.httpserver.HttpHandler;
2526
import com.sun.net.httpserver.HttpServer;
26-
import com.sun.net.httpserver.HttpExchange;
2727

2828
/**
2929
* Expose Prometheus metrics using a plain Java HttpServer.
@@ -37,6 +37,7 @@
3737
* */
3838
public class HTTPServer {
3939
private static class LocalByteArray extends ThreadLocal<ByteArrayOutputStream> {
40+
@Override
4041
protected ByteArrayOutputStream initialValue()
4142
{
4243
return new ByteArrayOutputStream(1 << 20);
@@ -47,15 +48,15 @@ protected ByteArrayOutputStream initialValue()
4748
* Handles Metrics collections from the given registry.
4849
*/
4950
static class HTTPMetricHandler implements HttpHandler {
50-
private CollectorRegistry registry;
51+
private final CollectorRegistry registry;
5152
private final LocalByteArray response = new LocalByteArray();
5253
private final static String HEALTHY_RESPONSE = "Exporter is Healthy.";
5354

5455
HTTPMetricHandler(CollectorRegistry registry) {
5556
this.registry = registry;
5657
}
5758

58-
59+
@Override
5960
public void handle(HttpExchange t) throws IOException {
6061
String query = t.getRequestURI().getRawQuery();
6162

@@ -70,18 +71,18 @@ public void handle(HttpExchange t) throws IOException {
7071
registry.filteredMetricFamilySamples(parseQuery(query)));
7172
}
7273

73-
osw.flush();
7474
osw.close();
75-
response.flush();
76-
response.close();
7775
t.getResponseHeaders().set("Content-Type",
7876
TextFormat.CONTENT_TYPE_004);
7977
if (shouldUseCompression(t)) {
8078
t.getResponseHeaders().set("Content-Encoding", "gzip");
8179
t.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
8280
final GZIPOutputStream os = new GZIPOutputStream(t.getResponseBody());
83-
response.writeTo(os);
84-
os.close();
81+
try {
82+
response.writeTo(os);
83+
} finally {
84+
os.close();
85+
}
8586
} else {
8687
t.getResponseHeaders().set("Content-Length",
8788
String.valueOf(response.size()));
@@ -100,7 +101,7 @@ protected static boolean shouldUseCompression(HttpExchange exchange) {
100101
for (String encodingHeader : encodingHeaders) {
101102
String[] encodings = encodingHeader.split(",");
102103
for (String encoding : encodings) {
103-
if (encoding.trim().toLowerCase().equals("gzip")) {
104+
if (encoding.trim().equalsIgnoreCase("gzip")) {
104105
return true;
105106
}
106107
}

0 commit comments

Comments
 (0)