Skip to content

[ISSUE #3009] Method manually handles closing an auto-closeable resource [MetricsHandler] #3547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ void preflight(HttpExchange httpExchange) throws IOException {
* GET /metrics Return a response that contains a summary of metrics
*/
void get(HttpExchange httpExchange) throws IOException {
OutputStream out = httpExchange.getResponseBody();
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");

try {
try (OutputStream out = httpExchange.getResponseBody()) {
httpExchange.getResponseHeaders().add("Content-Type", "application/json");
httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
GetMetricsResponse getMetricsResponse = new GetMetricsResponse(
httpSummaryMetrics.maxHTTPTPS(),
httpSummaryMetrics.avgHTTPTPS(),
Expand Down Expand Up @@ -133,16 +131,8 @@ void get(HttpExchange httpExchange) throws IOException {
Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error);
byte[] bytes = result.getBytes(Constants.DEFAULT_CHARSET);
httpExchange.sendResponseHeaders(500, bytes.length);
out.write(bytes);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
log.warn("out close failed...", e);
}
}
httpExchange.sendResponseHeaders(500, 0);
log.error(result, e);
}
}

Expand Down