From 6794ebc88137c60468b697a05c84f7c4cda4ffdc Mon Sep 17 00:00:00 2001 From: "l.subbotin" Date: Wed, 23 Oct 2024 23:48:37 +0200 Subject: [PATCH] Moved blockHeight to slots.go. --- cmd/solana_exporter/exporter.go | 22 +--------------------- cmd/solana_exporter/exporter_test.go | 12 ------------ cmd/solana_exporter/slots.go | 13 +++++++++++++ cmd/solana_exporter/slots_test.go | 4 ++-- pkg/rpc/client.go | 17 +++-------------- 5 files changed, 19 insertions(+), 49 deletions(-) diff --git a/cmd/solana_exporter/exporter.go b/cmd/solana_exporter/exporter.go index 85e6682..0bcf2c9 100644 --- a/cmd/solana_exporter/exporter.go +++ b/cmd/solana_exporter/exporter.go @@ -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() diff --git a/cmd/solana_exporter/exporter_test.go b/cmd/solana_exporter/exporter_test.go index 39254bf..4b9eed5 100644 --- a/cmd/solana_exporter/exporter_test.go +++ b/cmd/solana_exporter/exporter_test.go @@ -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) diff --git a/cmd/solana_exporter/slots.go b/cmd/solana_exporter/slots.go index cd56485..5abbbf9 100644 --- a/cmd/solana_exporter/slots.go +++ b/cmd/solana_exporter/slots.go @@ -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: diff --git a/cmd/solana_exporter/slots_test.go b/cmd/solana_exporter/slots_test.go index 19c633e..223fa21 100644 --- a/cmd/solana_exporter/slots_test.go +++ b/cmd/solana_exporter/slots_test.go @@ -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() diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 983f185..ea0fe7f 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -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 -}