From b698affdac7edc0d2d9d813a3d7c672bc6bba75e Mon Sep 17 00:00:00 2001 From: David Ansermino Date: Tue, 3 Nov 2020 10:38:01 -0500 Subject: [PATCH] Adds blockTimeout as a configurable param (#12) --- metrics/health/health.go | 23 ++++++++++------------- metrics/types/prometheus.go | 6 +++--- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/metrics/health/health.go b/metrics/health/health.go index e0542ff..309b607 100644 --- a/metrics/health/health.go +++ b/metrics/health/health.go @@ -15,14 +15,11 @@ import ( log "github.com/ChainSafe/log15" ) -// After this duration with no changes a chain will return an error -const BlockTimeout = 20 - type httpMetricServer struct { - port int - timeDelay int - chains []core.Chain - stats []ChainInfo + port int + blockTimeout int // After this duration (seconds) with no change in block height a chain will be considered unhealthy + chains []core.Chain + stats []ChainInfo } type httpResponse struct { @@ -36,12 +33,12 @@ type ChainInfo struct { LastUpdated time.Time `json:"lastUpdated"` } -func NewHealthServer(port int, chains []core.Chain) *httpMetricServer { +func NewHealthServer(port int, chains []core.Chain, blockTimeout int) *httpMetricServer { return &httpMetricServer{ - port: port, - chains: chains, - timeDelay: BlockTimeout, - stats: make([]ChainInfo, len(chains)), + port: port, + chains: chains, + blockTimeout: blockTimeout, + stats: make([]ChainInfo, len(chains)), } } @@ -69,7 +66,7 @@ func (s httpMetricServer) HealthStatus(w http.ResponseWriter, _ *http.Request) { if current.Height.Cmp(prev.Height) == 1 { s.stats[i].LastUpdated = current.LastUpdated s.stats[i].Height = current.Height - } else if int(timeDiff.Seconds()) >= s.timeDelay { // Error if we exceeded the time limit + } else if int(timeDiff.Seconds()) >= s.blockTimeout { // Error if we exceeded the time limit response := &httpResponse{ Chains: []ChainInfo{}, Error: fmt.Sprintf("chain %d height hasn't changed for %f seconds. Current Height: %s", prev.ChainId, timeDiff.Seconds(), current.Height), diff --git a/metrics/types/prometheus.go b/metrics/types/prometheus.go index 462fee3..816dcc3 100644 --- a/metrics/types/prometheus.go +++ b/metrics/types/prometheus.go @@ -10,10 +10,10 @@ import ( ) type ChainMetrics struct { - BlocksProcessed prometheus.Counter + BlocksProcessed prometheus.Counter LatestProcessedBlock prometheus.Gauge - LatestKnownBlock prometheus.Gauge - VotesSubmitted prometheus.Counter + LatestKnownBlock prometheus.Gauge + VotesSubmitted prometheus.Counter } func NewChainMetrics(chain string) *ChainMetrics {