From 2550e46269b05aa89127b06c1e18d51ebadd9da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Fri, 26 Mar 2021 16:14:12 +0200 Subject: [PATCH] eth/protocols, metrics: use resetting histograms for rare packets --- eth/protocols/eth/handler.go | 6 +++++- eth/protocols/snap/handler.go | 6 +++++- metrics/resetting_sample.go | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 metrics/resetting_sample.go diff --git a/eth/protocols/eth/handler.go b/eth/protocols/eth/handler.go index de5a38dcce98..0dc3de98982a 100644 --- a/eth/protocols/eth/handler.go +++ b/eth/protocols/eth/handler.go @@ -246,7 +246,11 @@ func handleMessage(backend Backend, peer *Peer) error { if metrics.Enabled { h := fmt.Sprintf("%s/%s/%d/%#02x", p2p.HandleHistName, ProtocolName, peer.Version(), msg.Code) defer func(start time.Time) { - sampler := func() metrics.Sample { return metrics.NewExpDecaySample(1028, 0.015) } + sampler := func() metrics.Sample { + return metrics.ResettingSample( + metrics.NewExpDecaySample(1028, 0.015), + ) + } metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(time.Since(start).Microseconds()) }(time.Now()) } diff --git a/eth/protocols/snap/handler.go b/eth/protocols/snap/handler.go index 6622cd871803..4169306c255f 100644 --- a/eth/protocols/snap/handler.go +++ b/eth/protocols/snap/handler.go @@ -134,7 +134,11 @@ func handleMessage(backend Backend, peer *Peer) error { if metrics.Enabled { h := fmt.Sprintf("%s/%s/%d/%#02x", p2p.HandleHistName, ProtocolName, peer.Version(), msg.Code) defer func(start time.Time) { - sampler := func() metrics.Sample { return metrics.NewExpDecaySample(1028, 0.015) } + sampler := func() metrics.Sample { + return metrics.ResettingSample( + metrics.NewExpDecaySample(1028, 0.015), + ) + } metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(time.Since(start).Microseconds()) }(time.Now()) } diff --git a/metrics/resetting_sample.go b/metrics/resetting_sample.go new file mode 100644 index 000000000000..43c1129cd0bc --- /dev/null +++ b/metrics/resetting_sample.go @@ -0,0 +1,24 @@ +package metrics + +// ResettingSample converts an ordinary sample into one that resets whenever its +// snapshot is retrieved. This will break for multi-monitor systems, but when only +// a single metric is being pushed out, this ensure that low-frequency events don't +// skew th charts indefinitely. +func ResettingSample(sample Sample) Sample { + return &resettingSample{ + Sample: sample, + } +} + +// resettingSample is a simple wrapper around a sample that resets it upon the +// snapshot retrieval. +type resettingSample struct { + Sample +} + +// Snapshot returns a read-only copy of the sample with the original reset. +func (rs *resettingSample) Snapshot() Sample { + s := rs.Sample.Snapshot() + rs.Sample.Clear() + return s +}