Skip to content

[ISSUE #3009] change MetricsHandler#get to try-resources #4800

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

Merged
merged 1 commit into from
Mar 21, 2024
Merged
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 @@ -97,76 +97,71 @@
* @throws IOException if an I/O error occurs while handling the request
*/
void get(HttpExchange httpExchange) throws IOException {
OutputStream out = httpExchange.getResponseBody();
httpExchange.getResponseHeaders().add(EventMeshConstants.CONTENT_TYPE, EventMeshConstants.APPLICATION_JSON);
httpExchange.getResponseHeaders().add(EventMeshConstants.HANDLER_ORIGIN, "*");

try {
GetMetricsResponse getMetricsResponse = new GetMetricsResponse(
httpSummaryMetrics.maxHTTPTPS(),
httpSummaryMetrics.avgHTTPTPS(),
httpSummaryMetrics.maxHTTPCost(),
httpSummaryMetrics.avgHTTPCost(),
httpSummaryMetrics.avgHTTPBodyDecodeCost(),
httpSummaryMetrics.getHttpDiscard(),
httpSummaryMetrics.maxSendBatchMsgTPS(),
httpSummaryMetrics.avgSendBatchMsgTPS(),
httpSummaryMetrics.getSendBatchMsgNumSum(),
httpSummaryMetrics.getSendBatchMsgFailNumSum(),
httpSummaryMetrics.getSendBatchMsgFailRate(),
httpSummaryMetrics.getSendBatchMsgDiscardNumSum(),
httpSummaryMetrics.maxSendMsgTPS(),
httpSummaryMetrics.avgSendMsgTPS(),
httpSummaryMetrics.getSendMsgNumSum(),
httpSummaryMetrics.getSendMsgFailNumSum(),
httpSummaryMetrics.getSendMsgFailRate(),
httpSummaryMetrics.getReplyMsgNumSum(),
httpSummaryMetrics.getReplyMsgFailNumSum(),
httpSummaryMetrics.maxPushMsgTPS(),
httpSummaryMetrics.avgPushMsgTPS(),
httpSummaryMetrics.getHttpPushMsgNumSum(),
httpSummaryMetrics.getHttpPushFailNumSum(),
httpSummaryMetrics.getHttpPushMsgFailRate(),
httpSummaryMetrics.maxHTTPPushLatency(),
httpSummaryMetrics.avgHTTPPushLatency(),
httpSummaryMetrics.getBatchMsgQueueSize(),
httpSummaryMetrics.getSendMsgQueueSize(),
httpSummaryMetrics.getPushMsgQueueSize(),
httpSummaryMetrics.getHttpRetryQueueSize(),
httpSummaryMetrics.avgBatchSendMsgCost(),
httpSummaryMetrics.avgSendMsgCost(),
httpSummaryMetrics.avgReplyMsgCost(),

tcpSummaryMetrics.getRetrySize(),
tcpSummaryMetrics.getClient2eventMeshTPS(),
tcpSummaryMetrics.getEventMesh2mqTPS(),
tcpSummaryMetrics.getMq2eventMeshTPS(),
tcpSummaryMetrics.getEventMesh2clientTPS(),
tcpSummaryMetrics.getAllTPS(),
tcpSummaryMetrics.getAllConnections(),
tcpSummaryMetrics.getSubTopicNum());
String result = JsonUtils.toJSONString(getMetricsResponse);
byte[] bytes = Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET);
httpExchange.sendResponseHeaders(200, bytes.length);
out.write(bytes);
} catch (Exception e) {
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.flush();
String stackTrace = writer.toString();

Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error);
byte[] bytes = Objects.requireNonNull(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);
try (OutputStream out = httpExchange.getResponseBody()) {

Check warning on line 103 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java#L103

Added line #L103 was not covered by tests
try {
GetMetricsResponse getMetricsResponse = new GetMetricsResponse(
httpSummaryMetrics.maxHTTPTPS(),
httpSummaryMetrics.avgHTTPTPS(),
httpSummaryMetrics.maxHTTPCost(),
httpSummaryMetrics.avgHTTPCost(),
httpSummaryMetrics.avgHTTPBodyDecodeCost(),
httpSummaryMetrics.getHttpDiscard(),
httpSummaryMetrics.maxSendBatchMsgTPS(),
httpSummaryMetrics.avgSendBatchMsgTPS(),
httpSummaryMetrics.getSendBatchMsgNumSum(),
httpSummaryMetrics.getSendBatchMsgFailNumSum(),
httpSummaryMetrics.getSendBatchMsgFailRate(),
httpSummaryMetrics.getSendBatchMsgDiscardNumSum(),
httpSummaryMetrics.maxSendMsgTPS(),
httpSummaryMetrics.avgSendMsgTPS(),
httpSummaryMetrics.getSendMsgNumSum(),
httpSummaryMetrics.getSendMsgFailNumSum(),
httpSummaryMetrics.getSendMsgFailRate(),
httpSummaryMetrics.getReplyMsgNumSum(),
httpSummaryMetrics.getReplyMsgFailNumSum(),
httpSummaryMetrics.maxPushMsgTPS(),
httpSummaryMetrics.avgPushMsgTPS(),
httpSummaryMetrics.getHttpPushMsgNumSum(),
httpSummaryMetrics.getHttpPushFailNumSum(),
httpSummaryMetrics.getHttpPushMsgFailRate(),
httpSummaryMetrics.maxHTTPPushLatency(),
httpSummaryMetrics.avgHTTPPushLatency(),
httpSummaryMetrics.getBatchMsgQueueSize(),
httpSummaryMetrics.getSendMsgQueueSize(),
httpSummaryMetrics.getPushMsgQueueSize(),
httpSummaryMetrics.getHttpRetryQueueSize(),
httpSummaryMetrics.avgBatchSendMsgCost(),
httpSummaryMetrics.avgSendMsgCost(),
httpSummaryMetrics.avgReplyMsgCost(),

Check warning on line 138 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java#L105-L138

Added lines #L105 - L138 were not covered by tests

tcpSummaryMetrics.getRetrySize(),
tcpSummaryMetrics.getClient2eventMeshTPS(),
tcpSummaryMetrics.getEventMesh2mqTPS(),
tcpSummaryMetrics.getMq2eventMeshTPS(),
tcpSummaryMetrics.getEventMesh2clientTPS(),
tcpSummaryMetrics.getAllTPS(),
tcpSummaryMetrics.getAllConnections(),
tcpSummaryMetrics.getSubTopicNum());
String result = JsonUtils.toJSONString(getMetricsResponse);
byte[] bytes = Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET);
httpExchange.sendResponseHeaders(200, bytes.length);
out.write(bytes);
} catch (Exception e) {
try (StringWriter writer = new StringWriter()) {
try (PrintWriter printWriter = new PrintWriter(writer)) {
e.printStackTrace(printWriter);

Check warning on line 155 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java#L140-L155

Added lines #L140 - L155 were not covered by tests
}

String stackTrace = writer.toString();

Check warning on line 158 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java#L158

Added line #L158 was not covered by tests

Error error = new Error(e.toString(), stackTrace);
String result = JsonUtils.toJSONString(error);
byte[] bytes = Objects.requireNonNull(result).getBytes(Constants.DEFAULT_CHARSET);
httpExchange.sendResponseHeaders(500, bytes.length);
out.write(bytes);

Check warning on line 164 in eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-runtime/src/main/java/org/apache/eventmesh/runtime/admin/handler/MetricsHandler.java#L160-L164

Added lines #L160 - L164 were not covered by tests
}
}
}
Expand Down
Loading