Moved blockHeight to slots.go.

This commit is contained in:
l.subbotin 2024-10-23 23:48:37 +02:00
parent 199b6694d5
commit 6794ebc881
No known key found for this signature in database
GPG Key ID: EBEF03B4E1049D77
5 changed files with 19 additions and 49 deletions

View File

@ -53,7 +53,6 @@ type SolanaCollector struct {
numSlotsBehind *prometheus.Desc
minimumLedgerSlot *prometheus.Desc
firstAvailableBlock *prometheus.Desc
blockHeight *prometheus.Desc
}
func NewSolanaCollector(provider rpc.Provider, slotPace time.Duration, balanceAddresses []string, nodekeys []string, votekeys []string, identity *string) *SolanaCollector {
@ -128,12 +127,6 @@ func NewSolanaCollector(provider rpc.Provider, slotPace time.Duration, balanceAd
[]string{IdentityLabel},
nil,
),
blockHeight: prometheus.NewDesc(
"solana_block_height",
"The current block height of the node.",
[]string{IdentityLabel},
nil,
),
}
return collector
}
@ -150,7 +143,6 @@ func (c *SolanaCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.numSlotsBehind
ch <- c.minimumLedgerSlot
ch <- c.firstAvailableBlock
ch <- c.blockHeight
}
func (c *SolanaCollector) collectVoteAccounts(ctx context.Context, ch chan<- prometheus.Metric) {
@ -241,17 +233,6 @@ func (c *SolanaCollector) collectFirstAvailableBlock(ctx context.Context, ch cha
ch <- prometheus.MustNewConstMetric(c.firstAvailableBlock, prometheus.GaugeValue, float64(*block), *c.identity)
}
func (c *SolanaCollector) collectBlockHeight(ctx context.Context, ch chan<- prometheus.Metric) {
height, err := c.rpcClient.GetBlockHeight(ctx)
if err != nil {
klog.Errorf("failed to get block height: %v", err)
ch <- prometheus.NewInvalidMetric(c.blockHeight, err)
return
}
ch <- prometheus.MustNewConstMetric(c.blockHeight, prometheus.GaugeValue, float64(*height), *c.identity)
}
func (c *SolanaCollector) collectBalances(ctx context.Context, ch chan<- prometheus.Metric) {
balances, err := FetchBalances(ctx, c.rpcClient, c.balanceAddresses)
@ -315,7 +296,6 @@ func (c *SolanaCollector) Collect(ch chan<- prometheus.Metric) {
c.collectHealth(ctx, ch)
c.collectMinimumLedgerSlot(ctx, ch)
c.collectFirstAvailableBlock(ctx, ch)
c.collectBlockHeight(ctx, ch)
}
func main() {
@ -340,7 +320,7 @@ func main() {
}
collector := NewSolanaCollector(client, slotPacerSchedule, config.BalanceAddresses, config.NodeKeys, votekeys, identity)
slotWatcher := NewSlotWatcher(
client, config.NodeKeys, votekeys, config.ComprehensiveSlotTracking, config.MonitorBlockSizes,
client, config.NodeKeys, votekeys, *identity, config.ComprehensiveSlotTracking, config.MonitorBlockSizes,
)
ctx, cancel := context.WithCancel(ctx)
defer cancel()

View File

@ -191,12 +191,6 @@ func (c *staticRPCClient) GetIdentity(ctx context.Context) (*string, error) {
return &nodeIdentity, nil
}
//goland:noinspection GoUnusedParameter
func (c *staticRPCClient) GetBlockHeight(ctx context.Context) (*int64, error) {
blockHeight := int64(1233)
return &blockHeight, nil
}
//goland:noinspection GoUnusedParameter
func (c *staticRPCClient) GetFirstAvailableBlock(ctx context.Context) (*int64, error) {
firstAvailiableBlock := int64(33)
@ -416,12 +410,6 @@ func (c *dynamicRPCClient) GetIdentity(ctx context.Context) (*string, error) {
return &nodeIdentity, nil
}
//goland:noinspection GoUnusedParameter
func (c *dynamicRPCClient) GetBlockHeight(ctx context.Context) (*int64, error) {
blockHeight := int64(1233)
return &blockHeight, nil
}
//goland:noinspection GoUnusedParameter
func (c *dynamicRPCClient) GetFirstAvailableBlock(ctx context.Context) (*int64, error) {
firstAvailiableBlock := int64(33)

View File

@ -23,6 +23,7 @@ type SlotWatcher struct {
// config:
nodekeys []string
votekeys []string
identity string
comprehensiveSlotTracking bool
monitorBlockSizes bool
@ -48,12 +49,14 @@ type SlotWatcher struct {
InflationRewardsMetric *prometheus.GaugeVec
FeeRewardsMetric *prometheus.CounterVec
BlockSizeMetric *prometheus.GaugeVec
BlockHeight *prometheus.GaugeVec
}
func NewSlotWatcher(
client rpc.Provider,
nodekeys []string,
votekeys []string,
identity string,
comprehensiveSlotTracking bool,
monitorBlockSizes bool,
) *SlotWatcher {
@ -61,6 +64,7 @@ func NewSlotWatcher(
client: client,
nodekeys: nodekeys,
votekeys: votekeys,
identity: identity,
comprehensiveSlotTracking: comprehensiveSlotTracking,
monitorBlockSizes: monitorBlockSizes,
// metrics:
@ -119,6 +123,13 @@ func NewSlotWatcher(
},
[]string{NodekeyLabel},
),
BlockHeight: prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "solana_block_height",
Help: "The current block height of the node.",
},
[]string{IdentityLabel},
),
}
// register:
for _, collector := range []prometheus.Collector{
@ -132,6 +143,7 @@ func NewSlotWatcher(
watcher.InflationRewardsMetric,
watcher.FeeRewardsMetric,
watcher.BlockSizeMetric,
watcher.BlockHeight,
} {
if err := prometheus.Register(collector); err != nil {
var (
@ -176,6 +188,7 @@ func (c *SlotWatcher) WatchSlots(ctx context.Context, pace time.Duration) {
c.TotalTransactionsMetric.Set(float64(epochInfo.TransactionCount))
c.SlotHeightMetric.Set(float64(epochInfo.AbsoluteSlot))
c.BlockHeight.WithLabelValues(c.identity).Set(float64(epochInfo.BlockHeight))
// if we get here, then the tracking numbers are set, so this is a "normal" run.
// start by checking if we have progressed since last run:

View File

@ -93,7 +93,7 @@ func TestSolanaCollector_WatchSlots_Static(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
nodeIdentity, _ := client.GetIdentity(ctx)
collector := NewSolanaCollector(&client, 100*time.Millisecond, nil, identities, votekeys, nodeIdentity)
watcher := NewSlotWatcher(&client, identities, votekeys, false, false)
watcher := NewSlotWatcher(&client, identities, votekeys, *nodeIdentity, false, false)
// reset metrics before running tests:
watcher.LeaderSlotsTotalMetric.Reset()
watcher.LeaderSlotsByEpochMetric.Reset()
@ -164,7 +164,7 @@ func TestSolanaCollector_WatchSlots_Dynamic(t *testing.T) {
runCtx, runCancel := context.WithCancel(context.Background())
nodeIdentity, _ := client.GetIdentity(runCtx)
collector := NewSolanaCollector(client, 300*time.Millisecond, nil, identities, votekeys, nodeIdentity)
watcher := NewSlotWatcher(client, identities, votekeys, false, false)
watcher := NewSlotWatcher(client, identities, votekeys, *nodeIdentity, false, false)
// reset metrics before running tests:
watcher.LeaderSlotsTotalMetric.Reset()
watcher.LeaderSlotsByEpochMetric.Reset()

View File

@ -78,7 +78,6 @@ type Provider interface {
GetIdentity(ctx context.Context) (*string, error)
GetMinimumLedgerSlot(ctx context.Context) (*int64, error)
GetFirstAvailableBlock(ctx context.Context) (*int64, error)
GetBlockHeight(ctx context.Context) (*int64, error)
}
func (c Commitment) MarshalJSON() ([]byte, error) {
@ -320,7 +319,7 @@ func (c *Client) GetHealth(ctx context.Context) (*string, error) {
return &resp.Result, nil
}
// GetIdentity Returns the identity pubkey for the current node
// GetIdentity returns the identity pubkey for the current node
// See API docs: https://solana.com/docs/rpc/http/getidentity
func (c *Client) GetIdentity(ctx context.Context) (*string, error) {
var resp response[Identity]
@ -330,7 +329,7 @@ func (c *Client) GetIdentity(ctx context.Context) (*string, error) {
return &resp.Result.Identity, nil
}
// MinimumLedgerSlot Returns the lowest slot that the node has information about in its ledger.
// MinimumLedgerSlot returns the lowest slot that the node has information about in its ledger.
// See API docs: https://solana.com/docs/rpc/http/minimumledgerslot
func (c *Client) GetMinimumLedgerSlot(ctx context.Context) (*int64, error) {
var resp response[int64]
@ -340,7 +339,7 @@ func (c *Client) GetMinimumLedgerSlot(ctx context.Context) (*int64, error) {
return &resp.Result, nil
}
// GetFirstAvailableBlock Returns the slot of the lowest confirmed block that has not been purged from the ledger
// GetFirstAvailableBlock returns the slot of the lowest confirmed block that has not been purged from the ledger
// See API docs: https://solana.com/docs/rpc/http/getfirstavailableblock
func (c *Client) GetFirstAvailableBlock(ctx context.Context) (*int64, error) {
var resp response[int64]
@ -349,13 +348,3 @@ func (c *Client) GetFirstAvailableBlock(ctx context.Context) (*int64, error) {
}
return &resp.Result, nil
}
// GetBlockHeight Returns the current block height of the node
// See API docs: https://solana.com/docs/rpc/http/getblockheight
func (c *Client) GetBlockHeight(ctx context.Context) (*int64, error) {
var resp response[int64]
if err := getResponse(ctx, c, "getBlockHeight", []any{}, &resp); err != nil {
return nil, err
}
return &resp.Result, nil
}