Skip to content

Commit

Permalink
Add trace to web socket JSON/RPC responses (hyperledger#6285)
Browse files Browse the repository at this point in the history
* Add trace to web socket JSON/RPC responses

Signed-off-by: Matthew Whitehead <[email protected]>

* Don't throw runtime exception if we can't parse JSON for trace

Signed-off-by: Matthew Whitehead <[email protected]>

---------

Signed-off-by: Matthew Whitehead <[email protected]>
Signed-off-by: Gabriel Fukushima <[email protected]>
  • Loading branch information
matthew1001 authored and gfukushima committed Dec 15, 2023
1 parent d322c8b commit 7b64676
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Optional;

import com.fasterxml.jackson.core.JsonGenerator.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
Expand All @@ -46,6 +47,10 @@

public class WebSocketMessageHandler {

private static final ObjectMapper jsonObjectMapper =
new ObjectMapper()
.registerModule(new Jdk8Module()); // Handle JDK8 Optionals (de)serialization

private static final Logger LOG = LoggerFactory.getLogger(WebSocketMessageHandler.class);
private static final ObjectWriter JSON_OBJECT_WRITER =
new ObjectMapper()
Expand Down Expand Up @@ -161,6 +166,7 @@ public void handle(
}

private void replyToClient(final ServerWebSocket websocket, final Object result) {
traceResponse(result);
try {
// underlying output stream lifecycle is managed by the json object writer
JSON_OBJECT_WRITER.writeValue(new JsonResponseStreamer(websocket), result);
Expand All @@ -172,4 +178,12 @@ private void replyToClient(final ServerWebSocket websocket, final Object result)
private JsonRpcResponse errorResponse(final Object id, final RpcErrorType error) {
return new JsonRpcErrorResponse(id, error);
}

private void traceResponse(final Object response) {
try {
LOG.trace(jsonObjectMapper.writeValueAsString(response));
} catch (JsonProcessingException e) {
LOG.error("Error tracing JSON-RPC response", e);
}
}
}

0 comments on commit 7b64676

Please sign in to comment.