github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/blockchain/metrics.go (about)

     1  package blockchain
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  	"github.com/prometheus/client_golang/prometheus/promauto"
     8  	types "github.com/prysmaticlabs/eth2-types"
     9  	"github.com/prysmaticlabs/prysm/beacon-chain/core/epoch/precompute"
    10  	iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
    11  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
    12  	"github.com/prysmaticlabs/prysm/proto/interfaces"
    13  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
    14  	"github.com/prysmaticlabs/prysm/shared/params"
    15  )
    16  
    17  var (
    18  	beaconSlot = promauto.NewGauge(prometheus.GaugeOpts{
    19  		Name: "beacon_slot",
    20  		Help: "Latest slot of the beacon chain state",
    21  	})
    22  	beaconHeadSlot = promauto.NewGauge(prometheus.GaugeOpts{
    23  		Name: "beacon_head_slot",
    24  		Help: "Slot of the head block of the beacon chain",
    25  	})
    26  	clockTimeSlot = promauto.NewGauge(prometheus.GaugeOpts{
    27  		Name: "beacon_clock_time_slot",
    28  		Help: "The current slot based on the genesis time and current clock",
    29  	})
    30  
    31  	headFinalizedEpoch = promauto.NewGauge(prometheus.GaugeOpts{
    32  		Name: "head_finalized_epoch",
    33  		Help: "Last finalized epoch of the head state",
    34  	})
    35  	headFinalizedRoot = promauto.NewGauge(prometheus.GaugeOpts{
    36  		Name: "head_finalized_root",
    37  		Help: "Last finalized root of the head state",
    38  	})
    39  	beaconFinalizedEpoch = promauto.NewGauge(prometheus.GaugeOpts{
    40  		Name: "beacon_finalized_epoch",
    41  		Help: "Last finalized epoch of the processed state",
    42  	})
    43  	beaconFinalizedRoot = promauto.NewGauge(prometheus.GaugeOpts{
    44  		Name: "beacon_finalized_root",
    45  		Help: "Last finalized root of the processed state",
    46  	})
    47  	beaconCurrentJustifiedEpoch = promauto.NewGauge(prometheus.GaugeOpts{
    48  		Name: "beacon_current_justified_epoch",
    49  		Help: "Current justified epoch of the processed state",
    50  	})
    51  	beaconCurrentJustifiedRoot = promauto.NewGauge(prometheus.GaugeOpts{
    52  		Name: "beacon_current_justified_root",
    53  		Help: "Current justified root of the processed state",
    54  	})
    55  	beaconPrevJustifiedEpoch = promauto.NewGauge(prometheus.GaugeOpts{
    56  		Name: "beacon_previous_justified_epoch",
    57  		Help: "Previous justified epoch of the processed state",
    58  	})
    59  	beaconPrevJustifiedRoot = promauto.NewGauge(prometheus.GaugeOpts{
    60  		Name: "beacon_previous_justified_root",
    61  		Help: "Previous justified root of the processed state",
    62  	})
    63  	validatorsCount = promauto.NewGaugeVec(prometheus.GaugeOpts{
    64  		Name: "validator_count",
    65  		Help: "The total number of validators",
    66  	}, []string{"state"})
    67  	validatorsBalance = promauto.NewGaugeVec(prometheus.GaugeOpts{
    68  		Name: "validators_total_balance",
    69  		Help: "The total balance of validators, in GWei",
    70  	}, []string{"state"})
    71  	validatorsEffectiveBalance = promauto.NewGaugeVec(prometheus.GaugeOpts{
    72  		Name: "validators_total_effective_balance",
    73  		Help: "The total effective balance of validators, in GWei",
    74  	}, []string{"state"})
    75  	currentEth1DataDepositCount = promauto.NewGauge(prometheus.GaugeOpts{
    76  		Name: "current_eth1_data_deposit_count",
    77  		Help: "The current eth1 deposit count in the last processed state eth1data field.",
    78  	})
    79  	stateTrieReferences = promauto.NewGaugeVec(prometheus.GaugeOpts{
    80  		Name: "field_references",
    81  		Help: "The number of states a particular field is shared with.",
    82  	}, []string{"state"})
    83  	prevEpochActiveBalances = promauto.NewGauge(prometheus.GaugeOpts{
    84  		Name: "beacon_prev_epoch_active_gwei",
    85  		Help: "The total amount of ether, in gwei, that was active for voting of previous epoch",
    86  	})
    87  	prevEpochSourceBalances = promauto.NewGauge(prometheus.GaugeOpts{
    88  		Name: "beacon_prev_epoch_source_gwei",
    89  		Help: "The total amount of ether, in gwei, that has been used in voting attestation source of previous epoch",
    90  	})
    91  	prevEpochTargetBalances = promauto.NewGauge(prometheus.GaugeOpts{
    92  		Name: "beacon_prev_epoch_target_gwei",
    93  		Help: "The total amount of ether, in gwei, that has been used in voting attestation target of previous epoch",
    94  	})
    95  	prevEpochHeadBalances = promauto.NewGauge(prometheus.GaugeOpts{
    96  		Name: "beacon_prev_epoch_head_gwei",
    97  		Help: "The total amount of ether, in gwei, that has been used in voting attestation head of previous epoch",
    98  	})
    99  	reorgCount = promauto.NewCounter(prometheus.CounterOpts{
   100  		Name: "beacon_reorg_total",
   101  		Help: "Count the number of times beacon chain has a reorg",
   102  	})
   103  	saveOrphanedAttCount = promauto.NewCounter(prometheus.CounterOpts{
   104  		Name: "saved_orphaned_att_total",
   105  		Help: "Count the number of times an orphaned attestation is saved",
   106  	})
   107  	attestationInclusionDelay = promauto.NewHistogram(
   108  		prometheus.HistogramOpts{
   109  			Name:    "attestation_inclusion_delay_slots",
   110  			Help:    "The number of slots between att.Slot and block.Slot",
   111  			Buckets: []float64{1, 2, 3, 4, 6, 32, 64},
   112  		},
   113  	)
   114  )
   115  
   116  // reportSlotMetrics reports slot related metrics.
   117  func reportSlotMetrics(stateSlot, headSlot, clockSlot types.Slot, finalizedCheckpoint *ethpb.Checkpoint) {
   118  	clockTimeSlot.Set(float64(clockSlot))
   119  	beaconSlot.Set(float64(stateSlot))
   120  	beaconHeadSlot.Set(float64(headSlot))
   121  	if finalizedCheckpoint != nil {
   122  		headFinalizedEpoch.Set(float64(finalizedCheckpoint.Epoch))
   123  		headFinalizedRoot.Set(float64(bytesutil.ToLowInt64(finalizedCheckpoint.Root)))
   124  	}
   125  }
   126  
   127  // reportEpochMetrics reports epoch related metrics.
   128  func reportEpochMetrics(ctx context.Context, postState, headState iface.BeaconState) error {
   129  	currentEpoch := types.Epoch(postState.Slot() / params.BeaconConfig().SlotsPerEpoch)
   130  
   131  	// Validator instances
   132  	pendingInstances := 0
   133  	activeInstances := 0
   134  	slashingInstances := 0
   135  	slashedInstances := 0
   136  	exitingInstances := 0
   137  	exitedInstances := 0
   138  	// Validator balances
   139  	pendingBalance := uint64(0)
   140  	activeBalance := uint64(0)
   141  	activeEffectiveBalance := uint64(0)
   142  	exitingBalance := uint64(0)
   143  	exitingEffectiveBalance := uint64(0)
   144  	slashingBalance := uint64(0)
   145  	slashingEffectiveBalance := uint64(0)
   146  
   147  	for i, validator := range postState.Validators() {
   148  		bal, err := postState.BalanceAtIndex(types.ValidatorIndex(i))
   149  		if err != nil {
   150  			log.Errorf("Could not load validator balance: %v", err)
   151  			continue
   152  		}
   153  		if validator.Slashed {
   154  			if currentEpoch < validator.ExitEpoch {
   155  				slashingInstances++
   156  				slashingBalance += bal
   157  				slashingEffectiveBalance += validator.EffectiveBalance
   158  			} else {
   159  				slashedInstances++
   160  			}
   161  			continue
   162  		}
   163  		if validator.ExitEpoch != params.BeaconConfig().FarFutureEpoch {
   164  			if currentEpoch < validator.ExitEpoch {
   165  				exitingInstances++
   166  				exitingBalance += bal
   167  				exitingEffectiveBalance += validator.EffectiveBalance
   168  			} else {
   169  				exitedInstances++
   170  			}
   171  			continue
   172  		}
   173  		if currentEpoch < validator.ActivationEpoch {
   174  			pendingInstances++
   175  			pendingBalance += bal
   176  			continue
   177  		}
   178  		activeInstances++
   179  		activeBalance += bal
   180  		activeEffectiveBalance += validator.EffectiveBalance
   181  	}
   182  	activeInstances += exitingInstances + slashingInstances
   183  	activeBalance += exitingBalance + slashingBalance
   184  	activeEffectiveBalance += exitingEffectiveBalance + slashingEffectiveBalance
   185  
   186  	validatorsCount.WithLabelValues("Pending").Set(float64(pendingInstances))
   187  	validatorsCount.WithLabelValues("Active").Set(float64(activeInstances))
   188  	validatorsCount.WithLabelValues("Exiting").Set(float64(exitingInstances))
   189  	validatorsCount.WithLabelValues("Exited").Set(float64(exitedInstances))
   190  	validatorsCount.WithLabelValues("Slashing").Set(float64(slashingInstances))
   191  	validatorsCount.WithLabelValues("Slashed").Set(float64(slashedInstances))
   192  	validatorsBalance.WithLabelValues("Pending").Set(float64(pendingBalance))
   193  	validatorsBalance.WithLabelValues("Active").Set(float64(activeBalance))
   194  	validatorsBalance.WithLabelValues("Exiting").Set(float64(exitingBalance))
   195  	validatorsBalance.WithLabelValues("Slashing").Set(float64(slashingBalance))
   196  	validatorsEffectiveBalance.WithLabelValues("Active").Set(float64(activeEffectiveBalance))
   197  	validatorsEffectiveBalance.WithLabelValues("Exiting").Set(float64(exitingEffectiveBalance))
   198  	validatorsEffectiveBalance.WithLabelValues("Slashing").Set(float64(slashingEffectiveBalance))
   199  
   200  	// Last justified slot
   201  	beaconCurrentJustifiedEpoch.Set(float64(postState.CurrentJustifiedCheckpoint().Epoch))
   202  	beaconCurrentJustifiedRoot.Set(float64(bytesutil.ToLowInt64(postState.CurrentJustifiedCheckpoint().Root)))
   203  
   204  	// Last previous justified slot
   205  	beaconPrevJustifiedEpoch.Set(float64(postState.PreviousJustifiedCheckpoint().Epoch))
   206  	beaconPrevJustifiedRoot.Set(float64(bytesutil.ToLowInt64(postState.PreviousJustifiedCheckpoint().Root)))
   207  
   208  	// Last finalized slot
   209  	beaconFinalizedEpoch.Set(float64(postState.FinalizedCheckpointEpoch()))
   210  	beaconFinalizedRoot.Set(float64(bytesutil.ToLowInt64(postState.FinalizedCheckpoint().Root)))
   211  	currentEth1DataDepositCount.Set(float64(postState.Eth1Data().DepositCount))
   212  
   213  	// Validator participation should be viewed on the canonical chain.
   214  	v, b, err := precompute.New(ctx, headState)
   215  	if err != nil {
   216  		return err
   217  	}
   218  	_, b, err = precompute.ProcessAttestations(ctx, headState, v, b)
   219  	if err != nil {
   220  		return err
   221  	}
   222  	prevEpochActiveBalances.Set(float64(b.ActivePrevEpoch))
   223  	prevEpochSourceBalances.Set(float64(b.PrevEpochAttested))
   224  	prevEpochTargetBalances.Set(float64(b.PrevEpochTargetAttested))
   225  	prevEpochHeadBalances.Set(float64(b.PrevEpochHeadAttested))
   226  
   227  	refMap := postState.FieldReferencesCount()
   228  	for name, val := range refMap {
   229  		stateTrieReferences.WithLabelValues(name).Set(float64(val))
   230  	}
   231  
   232  	return nil
   233  }
   234  
   235  func reportAttestationInclusion(blk interfaces.BeaconBlock) {
   236  	for _, att := range blk.Body().Attestations() {
   237  		attestationInclusionDelay.Observe(float64(blk.Slot() - att.Data.Slot))
   238  	}
   239  }