Skip to content

Commit

Permalink
fixes #8848 -- scrape validator_(total|active) (#8858)
Browse files Browse the repository at this point in the history
fixes #8848 -- scrape validator_total, validator_active
  • Loading branch information
kasey authored May 3, 2021
1 parent dace0f9 commit a090650
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions shared/clientstats/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
deps = [
"@com_github_prometheus_client_model//go:go_default_library",
"@com_github_prometheus_prom2json//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)
Expand Down
20 changes: 19 additions & 1 deletion shared/clientstats/scrapers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

dto "github.com/prometheus/client_model/go"
"github.com/prometheus/prom2json"
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -228,13 +229,30 @@ func populateBeaconNodeStats(pf metricMap) (BeaconNodeStats, error) {
return bs, nil
}

func populateValidatorStats(pf map[string]*dto.MetricFamily) (ValidatorStats, error) {
func statusIsActive(statusCode int64) bool {
s := eth.ValidatorStatus(statusCode)
return s.String() == "ACTIVE"
}

func populateValidatorStats(pf metricMap) (ValidatorStats, error) {
var err error
vs := ValidatorStats{}
vs.CommonStats, err = populateCommonStats(pf)
if err != nil {
return vs, err
}
vs.APIMessage = populateAPIMessage(ValidatorProcessName)

f, err := pf.getFamily("validator_statuses")
if err != nil {
return vs, err
}
for _, m := range f.Metric {
if statusIsActive(int64(m.Gauge.GetValue())) {
vs.ValidatorActive += 1
}
vs.ValidatorTotal += 1
}

return vs, nil
}
61 changes: 59 additions & 2 deletions shared/clientstats/scrapers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestFalseEth2Synced(t *testing.T) {

func TestValidatorScraper(t *testing.T) {
vScraper := validatorScraper{}
vScraper.tripper = &mockRT{body: prometheusTestBody}
vScraper.tripper = &mockRT{body: statusFixtureOneOfEach + prometheusTestBody}
r, err := vScraper.Scrape()
assert.NoError(t, err, "Unexpected error calling validatorScraper.Scrape")
vs := &ValidatorStats{}
Expand All @@ -77,6 +77,34 @@ func TestValidatorScraper(t *testing.T) {
assert.Equal(t, int64(1619586241), vs.ClientBuild)
assert.Equal(t, "v1.3.8-hotfix+6c0942", vs.ClientVersion)
assert.Equal(t, "prysm", vs.ClientName)
assert.Equal(t, int64(7), vs.ValidatorTotal)
assert.Equal(t, int64(1), vs.ValidatorActive)
}

func TestValidatorScraperAllActive(t *testing.T) {
vScraper := validatorScraper{}
vScraper.tripper = &mockRT{body: statusFixtureAllActive + prometheusTestBody}
r, err := vScraper.Scrape()
assert.NoError(t, err, "Unexpected error calling validatorScraper.Scrape")
vs := &ValidatorStats{}
err = json.NewDecoder(r).Decode(vs)
assert.NoError(t, err, "Unexpected error decoding result of validatorScraper.Scrape")
// CommonStats
assert.Equal(t, int64(4), vs.ValidatorTotal)
assert.Equal(t, int64(4), vs.ValidatorActive)
}

func TestValidatorScraperNoneActive(t *testing.T) {
vScraper := validatorScraper{}
vScraper.tripper = &mockRT{body: statusFixtureNoneActive + prometheusTestBody}
r, err := vScraper.Scrape()
assert.NoError(t, err, "Unexpected error calling validatorScraper.Scrape")
vs := &ValidatorStats{}
err = json.NewDecoder(r).Decode(vs)
assert.NoError(t, err, "Unexpected error decoding result of validatorScraper.Scrape")
// CommonStats
assert.Equal(t, int64(6), vs.ValidatorTotal)
assert.Equal(t, int64(0), vs.ValidatorActive)
}

func mockNowFunc(fixedTime time.Time) func() time.Time {
Expand All @@ -90,7 +118,7 @@ func TestValidatorAPIMessageDefaults(t *testing.T) {
// 1+e6 ns per ms, so 123456789 ns rounded down should be 123 ms
nowMillis := int64(1619811114123)
vScraper := validatorScraper{}
vScraper.tripper = &mockRT{body: prometheusTestBody}
vScraper.tripper = &mockRT{body: statusFixtureOneOfEach + prometheusTestBody}
r, err := vScraper.Scrape()
assert.NoError(t, err, "unexpected error from validatorScraper.Scrape()")

Expand Down Expand Up @@ -165,3 +193,32 @@ p2p_peer_count{state="Connecting"} 0
p2p_peer_count{state="Disconnected"} 62
p2p_peer_count{state="Disconnecting"} 0
`

var statusFixtureOneOfEach = `# HELP validator_statuses validator statuses: 0 UNKNOWN, 1 DEPOSITED, 2 PENDING, 3 ACTIVE, 4 EXITING, 5 SLASHING, 6 EXITED
# TYPE validator_statuses gauge
validator_statuses{pubkey="pk0"} 0
validator_statuses{pubkey="pk1"} 1
validator_statuses{pubkey="pk2"} 2
validator_statuses{pubkey="pk3"} 3
validator_statuses{pubkey="pk4"} 4
validator_statuses{pubkey="pk5"} 5
validator_statuses{pubkey="pk6"} 6
`

var statusFixtureAllActive = `# HELP validator_statuses validator statuses: 0 UNKNOWN, 1 DEPOSITED, 2 PENDING, 3 ACTIVE, 4 EXITING, 5 SLASHING, 6 EXITED
# TYPE validator_statuses gauge
validator_statuses{pubkey="pk0"} 3
validator_statuses{pubkey="pk1"} 3
validator_statuses{pubkey="pk2"} 3
validator_statuses{pubkey="pk3"} 3
`

var statusFixtureNoneActive = `# HELP validator_statuses validator statuses: 0 UNKNOWN, 1 DEPOSITED, 2 PENDING, 3 ACTIVE, 4 EXITING, 5 SLASHING, 6 EXITED
# TYPE validator_statuses gauge
validator_statuses{pubkey="pk0"} 0
validator_statuses{pubkey="pk1"} 1
validator_statuses{pubkey="pk2"} 2
validator_statuses{pubkey="pk3"} 4
validator_statuses{pubkey="pk4"} 5
validator_statuses{pubkey="pk5"} 6
`

0 comments on commit a090650

Please sign in to comment.