Skip to content

Commit

Permalink
Merge pull request #5 from validaoxyz/feat/add-block-time-histogram
Browse files Browse the repository at this point in the history
feat: add block time histogram in milliseconds
  • Loading branch information
murakamikaze authored Oct 6, 2024
2 parents 75b2b0e + 3d7f0f7 commit 40ee984
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ var (
0.015, 0.02, 0.03, 0.05, 0.075, 0.1, 0.15, 0.2, 0.25, 0.3, 0.4, 0.5,
},
})

HLBlockTimeHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "hl_block_time_milliseconds",
Help: "Histogram of time between blocks in milliseconds",
Buckets: []float64{
10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
120, 140, 160, 180, 200, 220, 240, 260, 280, 300,
350, 400, 450, 500, 600, 700, 800, 900, 1000,
1500, 2000, 3000, 5000, 10000,
},
})
)

// RegisterMetrics registers all Prometheus metrics
Expand All @@ -130,4 +141,5 @@ func RegisterMetrics() {
prometheus.MustRegister(HLSoftwareUpToDate)
prometheus.MustRegister(HLLatestBlockTimeGauge)
prometheus.MustRegister(HLApplyDurationHistogram)
prometheus.MustRegister(HLBlockTimeHistogram)
}
18 changes: 17 additions & 1 deletion internal/monitors/block_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/validaoxyz/hyperliquid-exporter/internal/utils"
)

var lastBlockTime time.Time

// StartBlockMonitor starts monitoring block time logs
func StartBlockMonitor(cfg config.Config) {
go func() {
Expand Down Expand Up @@ -154,7 +156,7 @@ func parseBlockTimeLine(line string) {
}

// Parse block_time to Unix timestamp
layout := "2006-01-02T15:04:05.999"
layout := "2006-01-02T15:04:05.999999999"
parsedTime, err := time.Parse(layout, blockTime)
if err != nil {
logger.Error("Error parsing block time: %v", err)
Expand All @@ -166,6 +168,20 @@ func parseBlockTimeLine(line string) {

unixTimestamp := float64(parsedTime.Unix())

// Calculate block time difference
if !lastBlockTime.IsZero() {
blockTimeDiff := parsedTime.Sub(lastBlockTime).Milliseconds()
if blockTimeDiff > 0 {
metrics.HLBlockTimeHistogram.Observe(float64(blockTimeDiff))
logger.Debug("Block time difference: %d milliseconds", blockTimeDiff)
} else {
logger.Warning("Invalid block time difference: %d milliseconds", blockTimeDiff)
}
}

// Update last block time
lastBlockTime = parsedTime

// Update metrics
metrics.HLBlockHeightGauge.Set(height)
metrics.HLApplyDurationGauge.Set(applyDuration)
Expand Down

0 comments on commit 40ee984

Please sign in to comment.