Skip to content

Commit 6d7ff6a

Browse files
committed
eth/protocols, metrics, p2p: add handler performance metrics
1 parent 54c0d57 commit 6d7ff6a

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

eth/protocols/eth/handler.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/core"
2626
"github.com/ethereum/go-ethereum/core/types"
27+
"github.com/ethereum/go-ethereum/metrics"
2728
"github.com/ethereum/go-ethereum/p2p"
2829
"github.com/ethereum/go-ethereum/p2p/enode"
2930
"github.com/ethereum/go-ethereum/p2p/enr"
@@ -241,7 +242,14 @@ func handleMessage(backend Backend, peer *Peer) error {
241242
} else if peer.Version() >= ETH66 {
242243
handlers = eth66
243244
}
244-
245+
// Track the emount of time it takes to serve the request and run the handler
246+
if metrics.Enabled {
247+
h := fmt.Sprintf("%s/%s/%d/%#02x", p2p.HandleHistName, ProtocolName, peer.Version(), msg.Code)
248+
defer func(start time.Time) {
249+
sampler := func() metrics.Sample { return metrics.NewExpDecaySample(1028, 0.015) }
250+
metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(time.Since(start).Microseconds())
251+
}(time.Now())
252+
}
245253
if handler := handlers[msg.Code]; handler != nil {
246254
return handler(backend, msg, peer)
247255
}

eth/protocols/snap/handler.go

+10
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ package snap
1919
import (
2020
"bytes"
2121
"fmt"
22+
"time"
2223

2324
"github.com/ethereum/go-ethereum/common"
2425
"github.com/ethereum/go-ethereum/core"
2526
"github.com/ethereum/go-ethereum/core/state"
2627
"github.com/ethereum/go-ethereum/light"
2728
"github.com/ethereum/go-ethereum/log"
29+
"github.com/ethereum/go-ethereum/metrics"
2830
"github.com/ethereum/go-ethereum/p2p"
2931
"github.com/ethereum/go-ethereum/p2p/enode"
3032
"github.com/ethereum/go-ethereum/p2p/enr"
@@ -128,6 +130,14 @@ func handleMessage(backend Backend, peer *Peer) error {
128130
}
129131
defer msg.Discard()
130132

133+
// Track the emount of time it takes to serve the request and run the handler
134+
if metrics.Enabled {
135+
h := fmt.Sprintf("%s/%s/%d/%#02x", p2p.HandleHistName, ProtocolName, peer.Version(), msg.Code)
136+
defer func(start time.Time) {
137+
sampler := func() metrics.Sample { return metrics.NewExpDecaySample(1028, 0.015) }
138+
metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(time.Since(start).Microseconds())
139+
}(time.Now())
140+
}
131141
// Handle the message depending on its contents
132142
switch {
133143
case msg.Code == GetAccountRangeMsg:

metrics/histogram.go

+9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ func GetOrRegisterHistogram(name string, r Registry, s Sample) Histogram {
2626
return r.GetOrRegister(name, func() Histogram { return NewHistogram(s) }).(Histogram)
2727
}
2828

29+
// GetOrRegisterHistogramLazy returns an existing Histogram or constructs and
30+
// registers a new StandardHistogram.
31+
func GetOrRegisterHistogramLazy(name string, r Registry, s func() Sample) Histogram {
32+
if nil == r {
33+
r = DefaultRegistry
34+
}
35+
return r.GetOrRegister(name, func() Histogram { return NewHistogram(s()) }).(Histogram)
36+
}
37+
2938
// NewHistogram constructs a new StandardHistogram from a Sample.
3039
func NewHistogram(s Sample) Histogram {
3140
if !Enabled {

p2p/metrics.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ import (
2525
)
2626

2727
const (
28+
// ingressMeterName is the prefix of the per-packet inbound metrics.
2829
ingressMeterName = "p2p/ingress"
29-
egressMeterName = "p2p/egress"
30+
31+
// egressMeterName is the prefix of the per-packet outbound metrics.
32+
egressMeterName = "p2p/egress"
33+
34+
// HandleHistName is the prefix of the per-packet serving time histograms.
35+
HandleHistName = "p2p/handle"
36+
37+
// WaitHistName is the prefix of the per-packet (req only) waiting time histograms.
38+
WaitHistName = "p2p/wait"
3039
)
3140

3241
var (

0 commit comments

Comments
 (0)