Skip to content

Commit

Permalink
Archive Count Metric
Browse files Browse the repository at this point in the history
  • Loading branch information
twoshark authored Feb 16, 2023
2 parents efd1a88 + 3520f9a commit 9de5e55
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
7 changes: 6 additions & 1 deletion dashboards/jsonnet/server_dashboard.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,18 @@ grafana.dashboard.new(
grafana.graphPanel.new(
'Upstreams Metrics',
datasource='$datasource',
span=2,
span=3,
)
.addTarget(
grafana.prometheus.target(
'upstreams_healthy',
)
)
.addTarget(
grafana.prometheus.target(
'upstreams_archive',
)
)
.addTarget(
grafana.prometheus.target(
'upstreams_configured',
Expand Down
7 changes: 7 additions & 0 deletions src/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type MetricSet struct {
StartUpTime *prometheus.HistogramVec
ConfiguredUpstreams prometheus.Gauge
HealthyUpstreams prometheus.Gauge
ArchiveUpstreams prometheus.Gauge
MaxBlock prometheus.Gauge
EthSyncingLatency *prometheus.HistogramVec
EthGetBlockNumberLatency *prometheus.HistogramVec
Expand Down Expand Up @@ -39,6 +40,12 @@ func Metrics() *MetricSet {
Help: "currently healthy upstream endpoints",
},
),
ArchiveUpstreams: promauto.NewGauge(
prometheus.GaugeOpts{
Name: "upstreams_archive",
Help: "count of healthy archive upstream endpoints",
},
),
MaxBlock: promauto.NewGauge(
prometheus.GaugeOpts{
Name: "max_ethereum_block",
Expand Down
3 changes: 2 additions & 1 deletion src/server/balanceProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func (bp *BalanceProxy) LiveHandler(c echo.Context) error {
// if there are any healthy upstreams, it will return a 200
// otherwise it will return a 503 and disable the ingress
func (bp *BalanceProxy) ReadyHandler(c echo.Context) error {
if bp.UpstreamManager.HealthyCount() > 0 {
counts := bp.UpstreamManager.ClientCounts()
if counts.Healthy > 0 {
return c.String(http.StatusOK, "ready")
}
return c.String(http.StatusServiceUnavailable, "no healthy upstreams")
Expand Down
1 change: 1 addition & 0 deletions src/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (suite *ServerTestSuite) TestMetricHandler() {
assert.True(suite.T(), strings.Contains(response, "startup_time_ms"))
assert.True(suite.T(), strings.Contains(response, "upstreams_configured"))
assert.True(suite.T(), strings.Contains(response, "upstreams_healthy"))
assert.True(suite.T(), strings.Contains(response, "upstreams_archive"))
assert.True(suite.T(), strings.Contains(response, "latency_eth_syncing"))
assert.True(suite.T(), strings.Contains(response, "latency_eth_get_block_number"))
assert.True(suite.T(), strings.Contains(response, "latency_eth_get_balance"))
Expand Down
24 changes: 19 additions & 5 deletions src/upstream/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,33 @@ func (m *Manager) StartHealthCheck() chan bool {
for i := range m.Clients {
m.Clients[i].EvaluatedHealthCheck()
}
metrics.Metrics().HealthyUpstreams.Set(float64(m.HealthyCount()))
clientCounts := m.ClientCounts()
metrics.Metrics().HealthyUpstreams.Set(float64(clientCounts.Healthy))
metrics.Metrics().ArchiveUpstreams.Set(float64(clientCounts.Archive))
}
}
}()
return quitHealthCheck
}

func (m *Manager) HealthyCount() int {
count := 0
type ClientCounts struct {
Archive int
Healthy int
}

func (m *Manager) ClientCounts() ClientCounts {
healthyCount := 0
archiveCount := 0
for _, client := range m.Clients {
if client.Healthy() {
count++
healthyCount++
}
if client.IsArchive() {
archiveCount++
}
}
return ClientCounts{
Archive: archiveCount,
Healthy: healthyCount,
}
return count
}
4 changes: 3 additions & 1 deletion src/upstream/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ func (m *Manager) ConnectAll() error {
break
}
}
metrics.Metrics().HealthyUpstreams.Set(float64(m.HealthyCount()))
clientCounts := m.ClientCounts()
metrics.Metrics().HealthyUpstreams.Set(float64(clientCounts.Healthy))
metrics.Metrics().ArchiveUpstreams.Set(float64(clientCounts.Archive))

if !anyAvailable {
return errNoUpstreamAvailable
Expand Down
1 change: 1 addition & 0 deletions src/upstream/manager_connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (suite *ManagerTestSuite) verifyConnectAllTestCase(testCase connectAllTestC
// infer `Healthy()` from HeathCheck and Dial errors
healthy := testCase.clientExpects[i].HealthCheck.Err == nil && testCase.clientExpects[i].Dial.Err == nil
client.EXPECT().Healthy().Return(healthy).AnyTimes()
client.EXPECT().IsArchive().Return(false).AnyTimes()
client.EXPECT().SetHealth(healthy).AnyTimes()
mgr.Clients[i] = client
}
Expand Down

0 comments on commit 9de5e55

Please sign in to comment.