Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Adds blockTimeout as a configurable param (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansermino authored Nov 3, 2020
1 parent 985a90f commit b698aff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
23 changes: 10 additions & 13 deletions metrics/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)),
}
}

Expand Down Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions metrics/types/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b698aff

Please sign in to comment.