Skip to content

blocksub metrics support #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions blocksub/blocksub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions blocksub/metrics.go
Original file line number Diff line number Diff line change
@@ -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))
}