github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/metrics/metrics.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package metrics 5 6 import ( 7 "time" 8 9 "github.com/prometheus/client_golang/prometheus" 10 11 "github.com/ava-labs/avalanchego/ids" 12 "github.com/ava-labs/avalanchego/utils/metric" 13 "github.com/ava-labs/avalanchego/utils/wrappers" 14 "github.com/ava-labs/avalanchego/vms/platformvm/block" 15 ) 16 17 var _ Metrics = (*metrics)(nil) 18 19 type Metrics interface { 20 metric.APIInterceptor 21 22 // Mark that the given block was accepted. 23 MarkAccepted(block.Block) error 24 // Mark that a validator set was created. 25 IncValidatorSetsCreated() 26 // Mark that a validator set was cached. 27 IncValidatorSetsCached() 28 // Mark that we spent the given time computing validator diffs. 29 AddValidatorSetsDuration(time.Duration) 30 // Mark that we computed a validator diff at a height with the given 31 // difference from the top. 32 AddValidatorSetsHeightDiff(uint64) 33 // Mark that this much stake is staked on the node. 34 SetLocalStake(uint64) 35 // Mark that this much stake is staked in the network. 36 SetTotalStake(uint64) 37 // Mark when this node will unstake from the Primary Network. 38 SetTimeUntilUnstake(time.Duration) 39 // Mark when this node will unstake from a subnet. 40 SetTimeUntilSubnetUnstake(subnetID ids.ID, timeUntilUnstake time.Duration) 41 } 42 43 func New(registerer prometheus.Registerer) (Metrics, error) { 44 blockMetrics, err := newBlockMetrics(registerer) 45 m := &metrics{ 46 blockMetrics: blockMetrics, 47 timeUntilUnstake: prometheus.NewGauge(prometheus.GaugeOpts{ 48 Name: "time_until_unstake", 49 Help: "Time (in ns) until this node leaves the Primary Network's validator set", 50 }), 51 timeUntilSubnetUnstake: prometheus.NewGaugeVec( 52 prometheus.GaugeOpts{ 53 Name: "time_until_unstake_subnet", 54 Help: "Time (in ns) until this node leaves the subnet's validator set", 55 }, 56 []string{"subnetID"}, 57 ), 58 localStake: prometheus.NewGauge(prometheus.GaugeOpts{ 59 Name: "local_staked", 60 Help: "Amount (in nAVAX) of AVAX staked on this node", 61 }), 62 totalStake: prometheus.NewGauge(prometheus.GaugeOpts{ 63 Name: "total_staked", 64 Help: "Amount (in nAVAX) of AVAX staked on the Primary Network", 65 }), 66 67 validatorSetsCached: prometheus.NewCounter(prometheus.CounterOpts{ 68 Name: "validator_sets_cached", 69 Help: "Total number of validator sets cached", 70 }), 71 validatorSetsCreated: prometheus.NewCounter(prometheus.CounterOpts{ 72 Name: "validator_sets_created", 73 Help: "Total number of validator sets created from applying difflayers", 74 }), 75 validatorSetsHeightDiff: prometheus.NewGauge(prometheus.GaugeOpts{ 76 Name: "validator_sets_height_diff_sum", 77 Help: "Total number of validator sets diffs applied for generating validator sets", 78 }), 79 validatorSetsDuration: prometheus.NewGauge(prometheus.GaugeOpts{ 80 Name: "validator_sets_duration_sum", 81 Help: "Total amount of time generating validator sets in nanoseconds", 82 }), 83 } 84 85 errs := wrappers.Errs{Err: err} 86 apiRequestMetrics, err := metric.NewAPIInterceptor(registerer) 87 errs.Add(err) 88 m.APIInterceptor = apiRequestMetrics 89 errs.Add( 90 registerer.Register(m.timeUntilUnstake), 91 registerer.Register(m.timeUntilSubnetUnstake), 92 registerer.Register(m.localStake), 93 registerer.Register(m.totalStake), 94 95 registerer.Register(m.validatorSetsCreated), 96 registerer.Register(m.validatorSetsCached), 97 registerer.Register(m.validatorSetsHeightDiff), 98 registerer.Register(m.validatorSetsDuration), 99 ) 100 101 return m, errs.Err 102 } 103 104 type metrics struct { 105 metric.APIInterceptor 106 107 blockMetrics *blockMetrics 108 109 timeUntilUnstake prometheus.Gauge 110 timeUntilSubnetUnstake *prometheus.GaugeVec 111 localStake prometheus.Gauge 112 totalStake prometheus.Gauge 113 114 validatorSetsCached prometheus.Counter 115 validatorSetsCreated prometheus.Counter 116 validatorSetsHeightDiff prometheus.Gauge 117 validatorSetsDuration prometheus.Gauge 118 } 119 120 func (m *metrics) MarkAccepted(b block.Block) error { 121 return b.Visit(m.blockMetrics) 122 } 123 124 func (m *metrics) IncValidatorSetsCreated() { 125 m.validatorSetsCreated.Inc() 126 } 127 128 func (m *metrics) IncValidatorSetsCached() { 129 m.validatorSetsCached.Inc() 130 } 131 132 func (m *metrics) AddValidatorSetsDuration(d time.Duration) { 133 m.validatorSetsDuration.Add(float64(d)) 134 } 135 136 func (m *metrics) AddValidatorSetsHeightDiff(d uint64) { 137 m.validatorSetsHeightDiff.Add(float64(d)) 138 } 139 140 func (m *metrics) SetLocalStake(s uint64) { 141 m.localStake.Set(float64(s)) 142 } 143 144 func (m *metrics) SetTotalStake(s uint64) { 145 m.totalStake.Set(float64(s)) 146 } 147 148 func (m *metrics) SetTimeUntilUnstake(timeUntilUnstake time.Duration) { 149 m.timeUntilUnstake.Set(float64(timeUntilUnstake)) 150 } 151 152 func (m *metrics) SetTimeUntilSubnetUnstake(subnetID ids.ID, timeUntilUnstake time.Duration) { 153 m.timeUntilSubnetUnstake.WithLabelValues(subnetID.String()).Set(float64(timeUntilUnstake)) 154 }