rpc: use zap.Logger
This commit is contained in:
parent
aecdf470e7
commit
289cb1c1d9
5 changed files with 40 additions and 43 deletions
|
@ -320,7 +320,7 @@ func startServer(ctx *cli.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
server := network.NewServer(serverConfig, chain, log)
|
server := network.NewServer(serverConfig, chain, log)
|
||||||
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server)
|
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPC, server, log)
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
|
|
||||||
go server.Start(errChan)
|
go server.Start(errChan)
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -11,7 +11,6 @@ require (
|
||||||
github.com/nspcc-dev/rfc6979 v0.1.0
|
github.com/nspcc-dev/rfc6979 v0.1.0
|
||||||
github.com/pkg/errors v0.8.1
|
github.com/pkg/errors v0.8.1
|
||||||
github.com/prometheus/client_golang v1.2.1
|
github.com/prometheus/client_golang v1.2.1
|
||||||
github.com/sirupsen/logrus v1.4.2
|
|
||||||
github.com/stretchr/testify v1.4.0
|
github.com/stretchr/testify v1.4.0
|
||||||
github.com/syndtr/goleveldb v0.0.0-20180307113352-169b1b37be73
|
github.com/syndtr/goleveldb v0.0.0-20180307113352-169b1b37be73
|
||||||
github.com/urfave/cli v1.20.0
|
github.com/urfave/cli v1.20.0
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -73,7 +73,7 @@ func (r *Request) Params() (*Params, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteErrorResponse writes an error response to the ResponseWriter.
|
// WriteErrorResponse writes an error response to the ResponseWriter.
|
||||||
func (r Request) WriteErrorResponse(w http.ResponseWriter, err error) {
|
func (s *Server) WriteErrorResponse(r *Request, w http.ResponseWriter, err error) {
|
||||||
jsonErr, ok := err.(*Error)
|
jsonErr, ok := err.(*Error)
|
||||||
if !ok {
|
if !ok {
|
||||||
jsonErr = NewInternalServerError("Internal server error", err)
|
jsonErr = NewInternalServerError("Internal server error", err)
|
||||||
|
@ -85,32 +85,34 @@ func (r Request) WriteErrorResponse(w http.ResponseWriter, err error) {
|
||||||
ID: r.RawID,
|
ID: r.RawID,
|
||||||
}
|
}
|
||||||
|
|
||||||
logFields := log.Fields{
|
logFields := []zap.Field{
|
||||||
"err": jsonErr.Cause,
|
zap.Error(jsonErr.Cause),
|
||||||
"method": r.Method,
|
zap.String("method", r.Method),
|
||||||
}
|
|
||||||
params, err := r.Params()
|
|
||||||
if err == nil {
|
|
||||||
logFields["params"] = *params
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithFields(logFields).Error("Error encountered with rpc request")
|
params, err := r.Params()
|
||||||
|
if err == nil {
|
||||||
|
logFields = append(logFields, zap.Any("params", params))
|
||||||
|
}
|
||||||
|
|
||||||
|
s.log.Error("Error encountered with rpc request", logFields...)
|
||||||
|
|
||||||
w.WriteHeader(jsonErr.HTTPCode)
|
w.WriteHeader(jsonErr.HTTPCode)
|
||||||
r.writeServerResponse(w, response)
|
s.writeServerResponse(r, w, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteResponse encodes the response and writes it to the ResponseWriter.
|
// WriteResponse encodes the response and writes it to the ResponseWriter.
|
||||||
func (r Request) WriteResponse(w http.ResponseWriter, result interface{}) {
|
func (s *Server) WriteResponse(r *Request, w http.ResponseWriter, result interface{}) {
|
||||||
response := Response{
|
response := Response{
|
||||||
JSONRPC: r.JSONRPC,
|
JSONRPC: r.JSONRPC,
|
||||||
Result: result,
|
Result: result,
|
||||||
ID: r.RawID,
|
ID: r.RawID,
|
||||||
}
|
}
|
||||||
|
|
||||||
r.writeServerResponse(w, response)
|
s.writeServerResponse(r, w, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Request) writeServerResponse(w http.ResponseWriter, response Response) {
|
func (s *Server) writeServerResponse(r *Request, w http.ResponseWriter, response Response) {
|
||||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
if r.enableCORSWorkaround {
|
if r.enableCORSWorkaround {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
@ -120,12 +122,9 @@ func (r Request) writeServerResponse(w http.ResponseWriter, response Response) {
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
err := encoder.Encode(response)
|
err := encoder.Encode(response)
|
||||||
|
|
||||||
logFields := log.Fields{
|
|
||||||
"err": err,
|
|
||||||
"method": r.Method,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithFields(logFields).Error("Error encountered while encoding response")
|
s.log.Error("Error encountered while encoding response",
|
||||||
|
zap.String("err", err.Error()),
|
||||||
|
zap.String("method", r.Method))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/rpc/wrappers"
|
"github.com/CityOfZion/neo-go/pkg/rpc/wrappers"
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -26,6 +26,7 @@ type (
|
||||||
chain core.Blockchainer
|
chain core.Blockchainer
|
||||||
config config.RPCConfig
|
config config.RPCConfig
|
||||||
coreServer *network.Server
|
coreServer *network.Server
|
||||||
|
log *zap.Logger
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,7 +35,7 @@ var invalidBlockHeightError = func(index int, height int) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer creates a new Server struct.
|
// NewServer creates a new Server struct.
|
||||||
func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *network.Server) Server {
|
func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *network.Server, log *zap.Logger) Server {
|
||||||
httpServer := &http.Server{
|
httpServer := &http.Server{
|
||||||
Addr: conf.Address + ":" + strconv.FormatUint(uint64(conf.Port), 10),
|
Addr: conf.Address + ":" + strconv.FormatUint(uint64(conf.Port), 10),
|
||||||
}
|
}
|
||||||
|
@ -44,6 +45,7 @@ func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *netwo
|
||||||
chain: chain,
|
chain: chain,
|
||||||
config: conf,
|
config: conf,
|
||||||
coreServer: coreServer,
|
coreServer: coreServer,
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,13 +53,11 @@ func NewServer(chain core.Blockchainer, conf config.RPCConfig, coreServer *netwo
|
||||||
// listening on the configured port.
|
// listening on the configured port.
|
||||||
func (s *Server) Start(errChan chan error) {
|
func (s *Server) Start(errChan chan error) {
|
||||||
if !s.config.Enabled {
|
if !s.config.Enabled {
|
||||||
log.Info("RPC server is not enabled")
|
s.log.Info("RPC server is not enabled")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.Handler = http.HandlerFunc(s.requestHandler)
|
s.Handler = http.HandlerFunc(s.requestHandler)
|
||||||
log.WithFields(log.Fields{
|
s.log.Info("starting rpc-server", zap.String("endpoint", s.Addr))
|
||||||
"endpoint": s.Addr,
|
|
||||||
}).Info("starting rpc-server")
|
|
||||||
|
|
||||||
errChan <- s.ListenAndServe()
|
errChan <- s.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
@ -65,9 +65,7 @@ func (s *Server) Start(errChan chan error) {
|
||||||
// Shutdown overrides the http.Server Shutdown
|
// Shutdown overrides the http.Server Shutdown
|
||||||
// method.
|
// method.
|
||||||
func (s *Server) Shutdown() error {
|
func (s *Server) Shutdown() error {
|
||||||
log.WithFields(log.Fields{
|
s.log.Info("shutting down rpc-server", zap.String("endpoint", s.Addr))
|
||||||
"endpoint": s.Addr,
|
|
||||||
}).Info("shutting down rpc-server")
|
|
||||||
return s.Server.Shutdown(context.Background())
|
return s.Server.Shutdown(context.Background())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +73,8 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request
|
||||||
req := NewRequest(s.config.EnableCORSWorkaround)
|
req := NewRequest(s.config.EnableCORSWorkaround)
|
||||||
|
|
||||||
if httpRequest.Method != "POST" {
|
if httpRequest.Method != "POST" {
|
||||||
req.WriteErrorResponse(
|
s.WriteErrorResponse(
|
||||||
|
req,
|
||||||
w,
|
w,
|
||||||
NewInvalidParamsError(
|
NewInvalidParamsError(
|
||||||
fmt.Sprintf("Invalid method '%s', please retry with 'POST'", httpRequest.Method), nil,
|
fmt.Sprintf("Invalid method '%s', please retry with 'POST'", httpRequest.Method), nil,
|
||||||
|
@ -86,13 +85,13 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request
|
||||||
|
|
||||||
err := req.DecodeData(httpRequest.Body)
|
err := req.DecodeData(httpRequest.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.WriteErrorResponse(w, NewParseError("Problem parsing JSON-RPC request body", err))
|
s.WriteErrorResponse(req, w, NewParseError("Problem parsing JSON-RPC request body", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
reqParams, err := req.Params()
|
reqParams, err := req.Params()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
req.WriteErrorResponse(w, NewInvalidParamsError("Problem parsing request parameters", err))
|
s.WriteErrorResponse(req, w, NewInvalidParamsError("Problem parsing request parameters", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,10 +99,9 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) methodHandler(w http.ResponseWriter, req *Request, reqParams Params) {
|
func (s *Server) methodHandler(w http.ResponseWriter, req *Request, reqParams Params) {
|
||||||
log.WithFields(log.Fields{
|
s.log.Info("processing rpc request",
|
||||||
"method": req.Method,
|
zap.String("method", req.Method),
|
||||||
"params": fmt.Sprintf("%v", reqParams),
|
zap.String("params", fmt.Sprintf("%v", reqParams)))
|
||||||
}).Info("processing rpc request")
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
results interface{}
|
results interface{}
|
||||||
|
@ -268,11 +266,11 @@ Methods:
|
||||||
}
|
}
|
||||||
|
|
||||||
if resultsErr != nil {
|
if resultsErr != nil {
|
||||||
req.WriteErrorResponse(w, resultsErr)
|
s.WriteErrorResponse(req, w, resultsErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
req.WriteResponse(w, results)
|
s.WriteResponse(req, w, results)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) getrawtransaction(reqParams Params) (interface{}, error) {
|
func (s *Server) getrawtransaction(reqParams Params) (interface{}, error) {
|
||||||
|
|
|
@ -181,7 +181,8 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
|
||||||
require.NoError(t, err, "could not load config")
|
require.NoError(t, err, "could not load config")
|
||||||
|
|
||||||
memoryStore := storage.NewMemoryStore()
|
memoryStore := storage.NewMemoryStore()
|
||||||
chain, err := core.NewBlockchain(memoryStore, cfg.ProtocolConfiguration, zaptest.NewLogger(t))
|
logger := zaptest.NewLogger(t)
|
||||||
|
chain, err := core.NewBlockchain(memoryStore, cfg.ProtocolConfiguration, logger)
|
||||||
require.NoError(t, err, "could not create chain")
|
require.NoError(t, err, "could not create chain")
|
||||||
|
|
||||||
go chain.Run()
|
go chain.Run()
|
||||||
|
@ -199,8 +200,8 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu
|
||||||
}
|
}
|
||||||
|
|
||||||
serverConfig := network.NewServerConfig(cfg)
|
serverConfig := network.NewServerConfig(cfg)
|
||||||
server := network.NewServer(serverConfig, chain, zaptest.NewLogger(t))
|
server := network.NewServer(serverConfig, chain, logger)
|
||||||
rpcServer := NewServer(chain, cfg.ApplicationConfiguration.RPC, server)
|
rpcServer := NewServer(chain, cfg.ApplicationConfiguration.RPC, server, logger)
|
||||||
handler := http.HandlerFunc(rpcServer.requestHandler)
|
handler := http.HandlerFunc(rpcServer.requestHandler)
|
||||||
|
|
||||||
return chain, handler
|
return chain, handler
|
||||||
|
|
Loading…
Reference in a new issue