diff --git a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java index 07641ff3df..18cc7b78be 100755 --- a/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java +++ b/eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java @@ -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(), @@ -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); } }