Skip to content

Commit

Permalink
Add ability to do trace debugging of every request/response
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
  • Loading branch information
peterbroadhurst committed Apr 30, 2022
1 parent a41fc19 commit 0e0d1fd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"secp",
"signermsgs",
"stretchr",
"Tracef",
"Warnf",
"wsclient"
],
Expand Down
13 changes: 8 additions & 5 deletions internal/rpcserver/rpchandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (s *rpcServer) rpcHandler(w http.ResponseWriter, r *http.Request) {
return
}

log.L(ctx).Tracef("RPC --> %s", b)

if s.sniffFirstByte(b) == '[' {
s.handleRPCBatch(ctx, w, b)
return
Expand All @@ -54,10 +56,10 @@ func (s *rpcServer) rpcHandler(w http.ResponseWriter, r *http.Request) {
}
rpcResponse, err := s.processRPC(ctx, &rpcRequest)
if err != nil {
s.replyRPC(w, rpcResponse, http.StatusInternalServerError)
s.replyRPC(ctx, w, rpcResponse, http.StatusInternalServerError)
return
}
s.replyRPC(w, rpcResponse, http.StatusOK)
s.replyRPC(ctx, w, rpcResponse, http.StatusOK)

}

Expand All @@ -68,12 +70,13 @@ func (s *rpcServer) replyRPCParseError(ctx context.Context, w http.ResponseWrite
fftypes.JSONAnyPtr("1"), // we couldn't parse the request ID
rpcbackend.RPCCodeInvalidRequest,
)
s.replyRPC(w, rpcError, http.StatusBadRequest)
s.replyRPC(ctx, w, rpcError, http.StatusBadRequest)
}

func (s *rpcServer) replyRPC(w http.ResponseWriter, result interface{}, status int) {
func (s *rpcServer) replyRPC(ctx context.Context, w http.ResponseWriter, result interface{}, status int) {
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(result)
log.L(ctx).Tracef("RPC <-- %s", b)
w.Header().Set("Content-Length", strconv.Itoa(len(b)))
w.WriteHeader(status)
_, _ = w.Write(b)
Expand Down Expand Up @@ -121,5 +124,5 @@ func (s *rpcServer) handleRPCBatch(ctx context.Context, w http.ResponseWriter, b
status = http.StatusInternalServerError
}
}
s.replyRPC(w, rpcResponses, status)
s.replyRPC(ctx, w, rpcResponses, status)
}

0 comments on commit 0e0d1fd

Please sign in to comment.