From 847de6ad719c94437b273c1704839766995c3bf7 Mon Sep 17 00:00:00 2001 From: Omar Date: Mon, 1 Nov 2021 22:24:20 +0600 Subject: [PATCH] feat(dot/telemetry): implement substrate_number_leaves metrics (#1926) --- .gitignore | 1 + dot/metrics/collector.go | 6 ++++-- dot/metrics/metrics.go | 6 +++--- dot/network/service.go | 6 ++---- dot/node.go | 2 +- dot/state/service.go | 8 ++++++-- dot/state/service_test.go | 5 +++-- lib/grandpa/grandpa_test.go | 2 +- scripts/ci.go | 1 + 9 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index b349c464f9..aef1849652 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ *.idea *.vscode *.sublime +*.devcontainer # Output of go coverage tool when used with LiteIDE *.out diff --git a/dot/metrics/collector.go b/dot/metrics/collector.go index 82f17e2336..9e2bbc291e 100644 --- a/dot/metrics/collector.go +++ b/dot/metrics/collector.go @@ -65,7 +65,8 @@ func (c *Collector) AddGauge(g GaugeMetrics) { } func (c *Collector) startCollectGauges() { - t := time.NewTicker(Refresh) + //TODO: Should we better add individual RefreshInterval for each `GaugeMetrics`or `label inside the gauges map`? + t := time.NewTicker(RefreshInterval) defer func() { t.Stop() c.wg.Done() @@ -89,6 +90,7 @@ func (c *Collector) startCollectGauges() { } func (c *Collector) startCollectProccessMetrics() { + //TODO: Should we better add individual RefreshInterval for each `GaugeMetrics`or `label inside the gauges map`? cpuStats := make([]*ethmetrics.CPUStats, 2) memStats := make([]*runtime.MemStats, 2) for i := 0; i < len(memStats); i++ { @@ -110,7 +112,7 @@ func (c *Collector) startCollectProccessMetrics() { memUsed = ethmetrics.GetOrRegisterGauge("system/memory/used", ethmetrics.DefaultRegistry) ) - t := time.NewTicker(Refresh) + t := time.NewTicker(RefreshInterval) defer func() { t.Stop() c.wg.Done() diff --git a/dot/metrics/metrics.go b/dot/metrics/metrics.go index 94c2067317..ae22c59e05 100644 --- a/dot/metrics/metrics.go +++ b/dot/metrics/metrics.go @@ -29,9 +29,9 @@ import ( var logger = log.New("pkg", "metrics") const ( - // Refresh is the refresh time for publishing metrics. - Refresh = time.Second * 10 - refreshFreq = int64(Refresh / time.Second) + // RefreshInterval is the refresh time for publishing metrics. + RefreshInterval = time.Second * 10 + refreshFreq = int64(RefreshInterval / time.Second) ) // PublishMetrics function will export the /metrics endpoint to prometheus process diff --git a/dot/network/service.go b/dot/network/service.go index 22ffbafa82..c192486859 100644 --- a/dot/network/service.go +++ b/dot/network/service.go @@ -301,7 +301,7 @@ func (s *Service) collectNetworkMetrics() { syncedBlocks.Update(num.Int64()) } - time.Sleep(gssmrmetrics.Refresh) + time.Sleep(gssmrmetrics.RefreshInterval) } } @@ -713,13 +713,11 @@ func (s *Service) NodeRoles() byte { return s.cfg.Roles } -// CollectGauge will be used to collect coutable metrics from network service +// CollectGauge will be used to collect countable metrics from network service func (s *Service) CollectGauge() map[string]int64 { var isSynced int64 if !s.syncer.IsSynced() { isSynced = 1 - } else { - isSynced = 0 } return map[string]int64{ diff --git a/dot/node.go b/dot/node.go index a574ad7cf7..8a67a3e2f3 100644 --- a/dot/node.go +++ b/dot/node.go @@ -212,7 +212,7 @@ func LoadGlobalNodeName(basepath string) (nodename string, err error) { // NewNode creates a new dot node from a dot node configuration func NewNode(cfg *Config, ks *keystore.GlobalKeystore, stopFunc func()) (*Node, error) { // set garbage collection percent to 10% - // can be overwritten by setting the GOGC env veriable, which defaults to 100 + // can be overwritten by setting the GOGC env variable, which defaults to 100 prev := debug.SetGCPercent(10) if prev != 100 { debug.SetGCPercent(prev) diff --git a/dot/state/service.go b/dot/state/service.go index 1d937d6ea0..95ecd487d6 100644 --- a/dot/state/service.go +++ b/dot/state/service.go @@ -32,8 +32,11 @@ import ( log "github.com/ChainSafe/log15" ) -const readyPoolTransactionsMetrics = "gossamer/ready/pool/transaction/metrics" -const readyPriorityQueueTransactions = "gossamer/ready/queue/transaction/metrics" +const ( + readyPoolTransactionsMetrics = "gossamer/ready/pool/transaction/metrics" + readyPriorityQueueTransactions = "gossamer/ready/queue/transaction/metrics" + substrateNumberLeaves = "gossamer/substrate_number_leaves/metrics" +) var logger = log.New("pkg", "state") @@ -358,5 +361,6 @@ func (s *Service) CollectGauge() map[string]int64 { return map[string]int64{ readyPoolTransactionsMetrics: int64(s.Transaction.pool.Len()), readyPriorityQueueTransactions: int64(s.Transaction.queue.Len()), + substrateNumberLeaves: int64(len(s.Block.Leaves())), } } diff --git a/dot/state/service_test.go b/dot/state/service_test.go index e8ab6ffba0..193cbb4a53 100644 --- a/dot/state/service_test.go +++ b/dot/state/service_test.go @@ -416,6 +416,7 @@ func TestStateServiceMetrics(t *testing.T) { ethmetrics.Enabled = true serv := NewService(config) serv.Transaction = NewTransactionState() + serv.Block = newTestBlockState(t, testGenesisHeader) m := metrics.NewCollector(context.Background()) m.AddGauge(serv) @@ -440,7 +441,7 @@ func TestStateServiceMetrics(t *testing.T) { hashes[i] = h } - time.Sleep(time.Second + metrics.Refresh) + time.Sleep(time.Second + metrics.RefreshInterval) gpool := ethmetrics.GetOrRegisterGauge(readyPoolTransactionsMetrics, nil) gqueue := ethmetrics.GetOrRegisterGauge(readyPriorityQueueTransactions, nil) @@ -450,7 +451,7 @@ func TestStateServiceMetrics(t *testing.T) { serv.Transaction.pool.Remove(hashes[0]) serv.Transaction.queue.Pop() - time.Sleep(time.Second + metrics.Refresh) + time.Sleep(time.Second + metrics.RefreshInterval) require.Equal(t, int64(1), gpool.Value()) require.Equal(t, int64(1), gqueue.Value()) } diff --git a/lib/grandpa/grandpa_test.go b/lib/grandpa/grandpa_test.go index 307a4b6d8b..b854b12f0a 100644 --- a/lib/grandpa/grandpa_test.go +++ b/lib/grandpa/grandpa_test.go @@ -1274,7 +1274,7 @@ func TestFinalRoundGaugeMetric(t *testing.T) { go coll.Start() - time.Sleep(metrics.Refresh + time.Second) + time.Sleep(metrics.RefreshInterval + time.Second) gauge := ethmetrics.GetOrRegisterGauge(finalityGrandpaRoundMetrics, nil) require.Equal(t, gauge.Value(), int64(180)) } diff --git a/scripts/ci.go b/scripts/ci.go index 457dcd5bba..06c22ad644 100755 --- a/scripts/ci.go +++ b/scripts/ci.go @@ -14,6 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the gossamer library. If not, see . +//go:build none // +build none package main