From 4995a12ca54ad06bb9227ead5d1d52d56f978e5a Mon Sep 17 00:00:00 2001 From: TymKh Date: Mon, 2 Jun 2025 18:33:35 +0200 Subject: [PATCH] blocksub metrics support --- blocksub/blocksub.go | 11 ++++++++--- blocksub/metrics.go | 11 +++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 blocksub/metrics.go diff --git a/blocksub/blocksub.go b/blocksub/blocksub.go index a5d52b3..cf62c93 100644 --- a/blocksub/blocksub.go +++ b/blocksub/blocksub.go @@ -28,9 +28,10 @@ type BlockSubscriber interface { } type BlockSub struct { - PollTimeout time.Duration // 10 seconds by default (8,640 requests per day) - SubTimeout time.Duration // 60 seconds by default, after this timeout the subscriber will reconnect - DebugOutput bool + PollTimeout time.Duration // 10 seconds by default (8,640 requests per day) + SubTimeout time.Duration // 60 seconds by default, after this timeout the subscriber will reconnect + DebugOutput bool + EnableMetrics bool ethNodeHTTPURI string // usually port 8545 ethNodeWebsocketURI string // usually port 8546 @@ -149,6 +150,10 @@ func (s *BlockSub) runListener() { case header := <-s.internalHeaderC: // use the new header if it's later or has a different hash than the previous known one if header.Number.Uint64() >= s.CurrentBlockNumber && header.Hash().Hex() != s.CurrentBlockHash { + + if s.EnableMetrics { + setBlockNumber(header.Number.Uint64()) + } s.CurrentHeader = header s.CurrentBlockNumber = header.Number.Uint64() s.CurrentBlockHash = header.Hash().Hex() diff --git a/blocksub/metrics.go b/blocksub/metrics.go new file mode 100644 index 0000000..38f830c --- /dev/null +++ b/blocksub/metrics.go @@ -0,0 +1,11 @@ +package blocksub + +import ( + "github.com/VictoriaMetrics/metrics" +) + +var blockNumberGauge = metrics.NewGauge(`goutils_blocksub_latest_block_number`, nil) + +func setBlockNumber(blockNumber uint64) { + blockNumberGauge.Set(float64(blockNumber)) +}