Skip to content

Commit

Permalink
feat(lib/grandpa) implement grandpa finality round metrics (ChainSafe…
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
EclesioMeloJunior authored and timwu20 committed Dec 6, 2021
1 parent 9540299 commit 97b0305
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
4 changes: 2 additions & 2 deletions dot/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions dot/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
15 changes: 15 additions & 0 deletions lib/grandpa/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
Expand Down
21 changes: 21 additions & 0 deletions lib/grandpa/grandpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
package grandpa

import (
"context"
"io/ioutil"
"math/big"
"math/rand"
"sort"
"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"
Expand All @@ -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
Expand Down Expand Up @@ -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))
}

0 comments on commit 97b0305

Please sign in to comment.