Skip to content

Commit

Permalink
rpc: swap out timer metrics to histograms (ethereum#25044)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan authored and JukLee0ira committed Dec 16, 2024
1 parent 78d180c commit 5eabd92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
4 changes: 2 additions & 2 deletions rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,12 @@ func (h *handler) handleCall(cp *callProc, msg *jsonrpcMessage) *jsonrpcMessage
if callb != h.unsubscribeCb {
rpcRequestGauge.Inc(1)
if answer.Error != nil {
failedReqeustGauge.Inc(1)
failedRequestGauge.Inc(1)
} else {
successfulRequestGauge.Inc(1)
}
rpcServingTimer.UpdateSince(start)
newRPCServingTimer(msg.Method, answer.Error == nil).UpdateSince(start)
updateServeTimeHistogram(msg.Method, answer.Error == nil, time.Since(start))
}
return answer
}
Expand Down
27 changes: 19 additions & 8 deletions rpc/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,33 @@ package rpc

import (
"fmt"
"time"

"github.com/XinFinOrg/XDPoSChain/metrics"
)

var (
rpcRequestGauge = metrics.NewRegisteredGauge("rpc/requests", nil)
successfulRequestGauge = metrics.NewRegisteredGauge("rpc/success", nil)
failedReqeustGauge = metrics.NewRegisteredGauge("rpc/failure", nil)
rpcServingTimer = metrics.NewRegisteredTimer("rpc/duration/all", nil)
failedRequestGauge = metrics.NewRegisteredGauge("rpc/failure", nil)

// serveTimeHistName is the prefix of the per-request serving time histograms.
serveTimeHistName = "rpc/duration"

rpcServingTimer = metrics.NewRegisteredTimer("rpc/duration/all", nil)
)

func newRPCServingTimer(method string, valid bool) metrics.Timer {
flag := "success"
if !valid {
flag = "failure"
// updateServeTimeHistogram tracks the serving time of a remote RPC call.
func updateServeTimeHistogram(method string, success bool, elapsed time.Duration) {
note := "success"
if !success {
note = "failure"
}
h := fmt.Sprintf("%s/%s/%s", serveTimeHistName, method, note)
sampler := func() metrics.Sample {
return metrics.ResettingSample(
metrics.NewExpDecaySample(1028, 0.015),
)
}
m := fmt.Sprintf("rpc/duration/%s/%s", method, flag)
return metrics.GetOrRegisterTimer(m, nil)
metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(elapsed.Microseconds())
}

0 comments on commit 5eabd92

Please sign in to comment.