Skip to content

Commit

Permalink
Fixed ctx issue, changed to track failures instead of success
Browse files Browse the repository at this point in the history
  • Loading branch information
natewalck committed Feb 28, 2020
1 parent 353c5d8 commit 7db3911
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
11 changes: 8 additions & 3 deletions lib/jsonrpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (handlers) getSpan(ctx context.Context, req request) (context.Context, *tra
func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer)), rpcError rpcErrFunc, done func(keepCtx bool), chOut chanOut) {
// Not sure if we need to sanitize the incoming req.Method or not.
ctx, span := h.getSpan(ctx, req)
ctx, _ = tag.New(context.Background(), tag.Insert(metrics.RPCMethod, req.Method))
ctx, _ = tag.New(ctx, tag.Insert(metrics.RPCMethod, req.Method))
defer span.End()

handler, ok := h[req.Method]
Expand All @@ -169,6 +169,7 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer

if len(req.Params) != handler.nParams {
rpcError(w, &req, rpcInvalidParams, fmt.Errorf("wrong param count"))
stats.Record(ctx, metrics.RPCRequestError.M(1))
done(false)
return
}
Expand All @@ -178,6 +179,7 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer

if chOut == nil && outCh {
rpcError(w, &req, rpcMethodNotFound, fmt.Errorf("method '%s' not supported in this mode (no out channel support)", req.Method))
stats.Record(ctx, metrics.RPCRequestError.M(1))
return
}

Expand All @@ -191,6 +193,7 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
rp := reflect.New(handler.paramReceivers[i])
if err := json.NewDecoder(bytes.NewReader(req.Params[i].data)).Decode(rp.Interface()); err != nil {
rpcError(w, &req, rpcParseError, xerrors.Errorf("unmarshaling params for '%s': %w", handler.handlerFunc, err))
stats.Record(ctx, metrics.RPCRequestError.M(1))
return
}

Expand All @@ -202,12 +205,12 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
callResult, err := doCall(req.Method, handler.handlerFunc, callParams)
if err != nil {
rpcError(w, &req, 0, xerrors.Errorf("fatal error calling '%s': %w", req.Method, err))
stats.Record(ctx, metrics.RPCRequestError.M(1))
return
}
if req.ID == nil {
return // notification
}
stats.Record(ctx, metrics.RPCRequestSuccess.M(1))

///////////////////

Expand All @@ -220,6 +223,7 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
err := callResult[handler.errOut].Interface()
if err != nil {
log.Warnf("error in RPC call to '%s': %+v", req.Method, err)
stats.Record(ctx, metrics.RPCResponseError.M(1))
resp.Error = &respError{
Code: 1,
Message: err.(error).Error(),
Expand All @@ -241,6 +245,7 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
}

log.Warnf("failed to setup channel in RPC call to '%s': %+v", req.Method, err)
stats.Record(ctx, metrics.RPCResponseError.M(1))
resp.Error = &respError{
Code: 1,
Message: err.(error).Error(),
Expand All @@ -250,8 +255,8 @@ func (h handlers) handle(ctx context.Context, req request, w func(func(io.Writer
w(func(w io.Writer) {
if err := json.NewEncoder(w).Encode(resp); err != nil {
log.Error(err)
stats.Record(ctx, metrics.RPCResponseError.M(1))
return
}
stats.Record(ctx, metrics.RPCResponseSuccess.M(1))
})
}
18 changes: 9 additions & 9 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ var (

// Measures
var (
LotusInfo = stats.Int64("info", "Arbitrary counter to tag lotus info to", stats.UnitDimensionless)
ChainHeight = stats.Int64("chain/height", "Current Height of the chain", stats.UnitDimensionless)
ChainNodeHeight = stats.Int64("chain/node_height", "Current Height of the node", stats.UnitDimensionless)
PeerCount = stats.Int64("peer/count", "Current number of FIL peers", stats.UnitDimensionless)
RPCInvalidMethod = stats.Int64("rpc/invalid_method", "Total number of invalid RPC methods called", stats.UnitDimensionless)
RPCRequestSuccess = stats.Int64("rpc/request_success", "Total number of successful requests handled", stats.UnitDimensionless)
RPCResponseSuccess = stats.Int64("rpc/response_success", "Total number of succeessful responses handled", stats.UnitDimensionless)
LotusInfo = stats.Int64("info", "Arbitrary counter to tag lotus info to", stats.UnitDimensionless)
ChainHeight = stats.Int64("chain/height", "Current Height of the chain", stats.UnitDimensionless)
ChainNodeHeight = stats.Int64("chain/node_height", "Current Height of the node", stats.UnitDimensionless)
PeerCount = stats.Int64("peer/count", "Current number of FIL peers", stats.UnitDimensionless)
RPCInvalidMethod = stats.Int64("rpc/invalid_method", "Total number of invalid RPC methods called", stats.UnitDimensionless)
RPCRequestError = stats.Int64("rpc/request_error", "Total number of request errors handled", stats.UnitDimensionless)
RPCResponseError = stats.Int64("rpc/response_error", "Total number of responses errors handled", stats.UnitDimensionless)
)

// DefaultViews is an array of Consensus views for metric gathering purposes
Expand All @@ -48,12 +48,12 @@ var DefaultViews = []*view.View{
TagKeys: []tag.Key{RPCMethod},
},
&view.View{
Measure: RPCRequestSuccess,
Measure: RPCRequestError,
Aggregation: view.Count(),
TagKeys: []tag.Key{RPCMethod},
},
&view.View{
Measure: RPCResponseSuccess,
Measure: RPCResponseError,
Aggregation: view.Count(),
TagKeys: []tag.Key{RPCMethod},
},
Expand Down

0 comments on commit 7db3911

Please sign in to comment.