prysm-pulse/beacon-chain/db/state_metrics.go
terence tsao 1b5b8a57e0 Remove unused proto schemas (#3005)
* Update io_kubernetes_build commit hash to 1246899

* Update dependency build_bazel_rules_nodejs to v0.33.1

* Update dependency com_github_hashicorp_golang_lru to v0.5.1

* Update libp2p

* Update io_bazel_rules_k8s commit hash to e68d5d7

* Starting to remove old protos

* Bazel build proto passes

* Fixing pb version

* Cleaned up core package

* Fixing tests

* 6 tests failing

* Update proto bugs

* Fixed incorrect validator ordering proto

* Sync with master

* Update go-ssz commit

* Removed bad copies from v1alpha1 folder

* add json spec json to pb handler

* add nested proto example

* proto/testing test works

* fix refactoring build failures

* use merged ssz

* push latest changes

* used forked json encoding

* used forked json encoding

* fix warning

* fix build issues

* fix test and lint

* fix build

* lint
2019-07-22 10:03:57 -04:00

112 lines
3.6 KiB
Go

package db
import (
"encoding/hex"
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)
var (
validatorBalancesGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "state_validator_balances",
Help: "Balances of validators, updated on epoch transition",
}, []string{
"validator",
})
validatorActivatedGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "state_validator_activated_epoch",
Help: "Activated epoch of validators, updated on epoch transition",
}, []string{
"validatorIndex",
})
validatorExitedGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "state_validator_exited_epoch",
Help: "Exited epoch of validators, updated on epoch transition",
}, []string{
"validatorIndex",
})
validatorSlashedGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "state_validator_slashed_epoch",
Help: "Slashed epoch of validators, updated on epoch transition",
}, []string{
"validatorIndex",
})
lastSlotGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "state_last_slot",
Help: "Last slot number of the processed state",
})
lastJustifiedEpochGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "state_last_justified_epoch",
Help: "Last justified epoch of the processed state",
})
lastPrevJustifiedEpochGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "state_last_prev_justified_epoch",
Help: "Last prev justified epoch of the processed state",
})
lastFinalizedEpochGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "state_last_finalized_epoch",
Help: "Last finalized epoch of the processed state",
})
activeValidatorsGauge = promauto.NewGauge(prometheus.GaugeOpts{
Name: "state_active_validators",
Help: "Total number of active validators",
})
)
func reportStateMetrics(state *pb.BeaconState) {
currentEpoch := state.Slot / params.BeaconConfig().SlotsPerEpoch
// Validator balances
for i, bal := range state.Balances {
validatorBalancesGauge.WithLabelValues(
"0x" + hex.EncodeToString(state.Validators[i].PublicKey), // Validator
).Set(float64(bal))
}
var active float64
for i, v := range state.Validators {
// Track individual Validator's activation epochs
validatorActivatedGauge.WithLabelValues(
strconv.Itoa(i), //Validator index
).Set(float64(v.ActivationEpoch))
// Track individual Validator's exited epochs
validatorExitedGauge.WithLabelValues(
strconv.Itoa(i), //Validator index
).Set(float64(v.ExitEpoch))
// Track individual Validator's slashed epochs
if v.Slashed {
validatorSlashedGauge.WithLabelValues(
strconv.Itoa(i), //Validator index
).Set(float64(v.WithdrawableEpoch - params.BeaconConfig().EpochsPerSlashingsVector))
} else {
validatorSlashedGauge.WithLabelValues(
strconv.Itoa(i), //Validator index
).Set(float64(params.BeaconConfig().FarFutureEpoch))
}
// Total number of active validators
if v.ActivationEpoch <= currentEpoch && currentEpoch < v.ExitEpoch {
active++
}
}
activeValidatorsGauge.Set(active)
// Slot number
lastSlotGauge.Set(float64(state.Slot))
// Last justified slot
if state.CurrentJustifiedCheckpoint != nil {
lastJustifiedEpochGauge.Set(float64(state.CurrentJustifiedCheckpoint.Epoch))
}
// Last previous justified slot
if state.PreviousJustifiedCheckpoint != nil {
lastPrevJustifiedEpochGauge.Set(float64(state.PreviousJustifiedCheckpoint.Epoch))
}
// Last finalized slot
if state.FinalizedCheckpoint != nil {
lastFinalizedEpochGauge.Set(float64(state.FinalizedCheckpoint.Epoch))
}
}