From 97b0305ef18d72fb53bdf2dde150570dcbc24f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20Junior?= Date: Tue, 6 Jul 2021 16:05:19 -0400 Subject: [PATCH] feat(lib/grandpa) implement grandpa finality round metrics (#1655) * chore: implement grandpa finality round * pool ready transaction metrics * chore: add priority queue metrics * chore: fix lint * chore: add gauge collector interface * chore: fix lint * remove unused metrics timeout * chore: remove unused test * remove unused consts * chore: adding tests --- dot/metrics/collector.go | 4 ++-- dot/node.go | 1 + go.mod | 2 ++ go.sum | 9 +++++++-- lib/grandpa/grandpa.go | 15 +++++++++++++++ lib/grandpa/grandpa_test.go | 21 +++++++++++++++++++++ 6 files changed, 48 insertions(+), 4 deletions(-) diff --git a/dot/metrics/collector.go b/dot/metrics/collector.go index eec1a93254..82f17e2336 100644 --- a/dot/metrics/collector.go +++ b/dot/metrics/collector.go @@ -80,8 +80,8 @@ func (c *Collector) startCollectGauges() { m := g.CollectGauge() for label, value := range m { - pooltx := ethmetrics.GetOrRegisterGauge(label, nil) - pooltx.Update(value) + gauge := ethmetrics.GetOrRegisterGauge(label, nil) + gauge.Update(value) } } } diff --git a/dot/node.go b/dot/node.go index 15eba9210d..c20bcf7e7b 100644 --- a/dot/node.go +++ b/dot/node.go @@ -331,6 +331,7 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node, if cfg.Global.PublishMetrics { c := metrics.NewCollector(context.Background()) + c.AddGauge(fg) c.AddGauge(stateSrvc) go c.Start() diff --git a/go.mod b/go.mod index 579279b921..a3f36cadeb 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,9 @@ require ( github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975 github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416 github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea + github.com/shirou/gopsutil v3.21.6+incompatible // indirect github.com/stretchr/testify v1.7.0 + github.com/tklauser/go-sysconf v0.3.6 // indirect github.com/urfave/cli v1.22.1 github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 diff --git a/go.sum b/go.sum index a083d43080..69581e72c2 100644 --- a/go.sum +++ b/go.sum @@ -273,7 +273,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= -github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0= @@ -940,8 +939,9 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= -github.com/shirou/gopsutil v2.20.5+incompatible h1:tYH07UPoQt0OCQdgWWMgYHy3/a9bcxNpBIysykNIP7I= github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/shirou/gopsutil v3.21.6+incompatible h1:mmZtAlWSd8U2HeRTjswbnDLPxqsEoK01NK+GZ1P+nEM= +github.com/shirou/gopsutil v3.21.6+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY= github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM= github.com/shurcooL/github_flavored_markdown v0.0.0-20181002035957-2122de532470/go.mod h1:2dOwnU2uBioM+SGy2aZoq1f/Sd1l9OkAeAUvjSyvgU0= @@ -1011,6 +1011,10 @@ github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954 h1:xQdMZ1WLrgkk github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM= github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= +github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4= +github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI= +github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA= +github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc= github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4= @@ -1242,6 +1246,7 @@ golang.org/x/sys v0.0.0-20201221093633-bc327ba9c2f0/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210105210732-16f7687f5001/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/lib/grandpa/grandpa.go b/lib/grandpa/grandpa.go index dfc085fc0e..60f5d08611 100644 --- a/lib/grandpa/grandpa.go +++ b/lib/grandpa/grandpa.go @@ -34,6 +34,10 @@ import ( log "github.com/ChainSafe/log15" ) +const ( + finalityGrandpaRoundMetrics = "gossamer/finality/grandpa/round" +) + var ( interval = time.Second // TODO: make this configurable; currently 1s is same as substrate; total round length is then 2s logger = log.New("pkg", "grandpa") @@ -231,6 +235,16 @@ func (s *Service) authorities() []*types.Authority { return ad } +// CollectGauge returns the map between metrics label and value +func (s *Service) CollectGauge() map[string]int64 { + s.roundLock.Lock() + defer s.roundLock.Unlock() + + return map[string]int64{ + finalityGrandpaRoundMetrics: int64(s.state.round), + } +} + // updateAuthorities updates the grandpa voter set, increments the setID, and resets the round numbers func (s *Service) updateAuthorities() error { currSetID, err := s.grandpaState.GetCurrentSetID() @@ -279,6 +293,7 @@ func (s *Service) initiate() error { s.roundLock.Lock() s.state.round++ logger.Trace("incrementing grandpa round", "next round", s.state.round) + if s.tracker != nil { s.tracker.stop() } diff --git a/lib/grandpa/grandpa_test.go b/lib/grandpa/grandpa_test.go index 42dce94604..6485fbfc9c 100644 --- a/lib/grandpa/grandpa_test.go +++ b/lib/grandpa/grandpa_test.go @@ -17,6 +17,7 @@ package grandpa import ( + "context" "io/ioutil" "math/big" "math/rand" @@ -24,6 +25,7 @@ import ( "testing" "time" + "github.com/ChainSafe/gossamer/dot/metrics" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/common" @@ -35,6 +37,8 @@ import ( "github.com/stretchr/testify/require" . "github.com/ChainSafe/gossamer/lib/grandpa/mocks" + + ethmetrics "github.com/ethereum/go-ethereum/metrics" ) // testGenesisHeader is a test block header @@ -1115,3 +1119,20 @@ func TestGrandpa_NonAuthority(t *testing.T) { require.Equal(t, uint64(2), gs.state.round) require.Equal(t, uint64(0), gs.state.setID) } +func TestFinalRoundGaugeMetric(t *testing.T) { + gs, _ := newTestService(t) + ethmetrics.Enabled = true + + gs.state.round = uint64(180) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + coll := metrics.NewCollector(ctx) + coll.AddGauge(gs) + + go coll.Start() + + time.Sleep(metrics.Refresh + time.Second) + gauge := ethmetrics.GetOrRegisterGauge(finalityGrandpaRoundMetrics, nil) + require.Equal(t, gauge.Value(), int64(180)) +}