Skip to content

Commit ce9a8a4

Browse files
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
1 parent 7ecada2 commit ce9a8a4

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

dot/metrics/collector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ func (c *Collector) startCollectGauges() {
8080
m := g.CollectGauge()
8181

8282
for label, value := range m {
83-
pooltx := ethmetrics.GetOrRegisterGauge(label, nil)
84-
pooltx.Update(value)
83+
gauge := ethmetrics.GetOrRegisterGauge(label, nil)
84+
gauge.Update(value)
8585
}
8686
}
8787
}

dot/node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node,
331331

332332
if cfg.Global.PublishMetrics {
333333
c := metrics.NewCollector(context.Background())
334+
c.AddGauge(fg)
334335
c.AddGauge(stateSrvc)
335336

336337
go c.Start()

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ require (
3939
github.com/nanobox-io/golang-scribble v0.0.0-20190309225732-aa3e7c118975
4040
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416
4141
github.com/perlin-network/life v0.0.0-20191203030451-05c0e0f7eaea
42+
github.com/shirou/gopsutil v3.21.6+incompatible // indirect
4243
github.com/stretchr/testify v1.7.0
44+
github.com/tklauser/go-sysconf v0.3.6 // indirect
4345
github.com/urfave/cli v1.22.1
4446
github.com/wasmerio/go-ext-wasm v0.3.2-0.20200326095750-0a32be6068ec
4547
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb
273273
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
274274
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
275275
github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
276-
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
277276
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
278277
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
279278
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
940939
github.com/segmentio/kafka-go v0.1.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
941940
github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfPOCvTvk+EJo=
942941
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
943-
github.com/shirou/gopsutil v2.20.5+incompatible h1:tYH07UPoQt0OCQdgWWMgYHy3/a9bcxNpBIysykNIP7I=
944942
github.com/shirou/gopsutil v2.20.5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
943+
github.com/shirou/gopsutil v3.21.6+incompatible h1:mmZtAlWSd8U2HeRTjswbnDLPxqsEoK01NK+GZ1P+nEM=
944+
github.com/shirou/gopsutil v3.21.6+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
945945
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
946946
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
947947
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
10111011
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM=
10121012
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
10131013
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
1014+
github.com/tklauser/go-sysconf v0.3.6 h1:oc1sJWvKkmvIxhDHeKWvZS4f6AW+YcoguSfRF2/Hmo4=
1015+
github.com/tklauser/go-sysconf v0.3.6/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=
1016+
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
1017+
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
10141018
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
10151019
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc=
10161020
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
12421246
golang.org/x/sys v0.0.0-20210105210732-16f7687f5001/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12431247
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12441248
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1249+
golang.org/x/sys v0.0.0-20210316164454-77fc1eacc6aa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12451250
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12461251
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
12471252
golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

lib/grandpa/grandpa.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ import (
3434
log "github.com/ChainSafe/log15"
3535
)
3636

37+
const (
38+
finalityGrandpaRoundMetrics = "gossamer/finality/grandpa/round"
39+
)
40+
3741
var (
3842
interval = time.Second // TODO: make this configurable; currently 1s is same as substrate; total round length is then 2s
3943
logger = log.New("pkg", "grandpa")
@@ -231,6 +235,16 @@ func (s *Service) authorities() []*types.Authority {
231235
return ad
232236
}
233237

238+
// CollectGauge returns the map between metrics label and value
239+
func (s *Service) CollectGauge() map[string]int64 {
240+
s.roundLock.Lock()
241+
defer s.roundLock.Unlock()
242+
243+
return map[string]int64{
244+
finalityGrandpaRoundMetrics: int64(s.state.round),
245+
}
246+
}
247+
234248
// updateAuthorities updates the grandpa voter set, increments the setID, and resets the round numbers
235249
func (s *Service) updateAuthorities() error {
236250
currSetID, err := s.grandpaState.GetCurrentSetID()
@@ -279,6 +293,7 @@ func (s *Service) initiate() error {
279293
s.roundLock.Lock()
280294
s.state.round++
281295
logger.Trace("incrementing grandpa round", "next round", s.state.round)
296+
282297
if s.tracker != nil {
283298
s.tracker.stop()
284299
}

lib/grandpa/grandpa_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package grandpa
1818

1919
import (
20+
"context"
2021
"io/ioutil"
2122
"math/big"
2223
"math/rand"
2324
"sort"
2425
"testing"
2526
"time"
2627

28+
"github.com/ChainSafe/gossamer/dot/metrics"
2729
"github.com/ChainSafe/gossamer/dot/state"
2830
"github.com/ChainSafe/gossamer/dot/types"
2931
"github.com/ChainSafe/gossamer/lib/common"
@@ -35,6 +37,8 @@ import (
3537
"github.com/stretchr/testify/require"
3638

3739
. "github.com/ChainSafe/gossamer/lib/grandpa/mocks"
40+
41+
ethmetrics "github.com/ethereum/go-ethereum/metrics"
3842
)
3943

4044
// testGenesisHeader is a test block header
@@ -1115,3 +1119,20 @@ func TestGrandpa_NonAuthority(t *testing.T) {
11151119
require.Equal(t, uint64(2), gs.state.round)
11161120
require.Equal(t, uint64(0), gs.state.setID)
11171121
}
1122+
func TestFinalRoundGaugeMetric(t *testing.T) {
1123+
gs, _ := newTestService(t)
1124+
ethmetrics.Enabled = true
1125+
1126+
gs.state.round = uint64(180)
1127+
ctx, cancel := context.WithCancel(context.Background())
1128+
defer cancel()
1129+
1130+
coll := metrics.NewCollector(ctx)
1131+
coll.AddGauge(gs)
1132+
1133+
go coll.Start()
1134+
1135+
time.Sleep(metrics.Refresh + time.Second)
1136+
gauge := ethmetrics.GetOrRegisterGauge(finalityGrandpaRoundMetrics, nil)
1137+
require.Equal(t, gauge.Value(), int64(180))
1138+
}

0 commit comments

Comments
 (0)