Solana node version metric (#9)
This commit is contained in:
parent
5873f07cb1
commit
16c8b10324
|
@ -11,15 +11,14 @@ import (
|
|||
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
httpTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
rpcAddr = flag.String("rpcURI", "", "Solana RPC URI (including protocol and path)")
|
||||
addr = flag.String("addr", ":8080", "Listen address")
|
||||
votePubkey = flag.String("votepubkey", "", "Validator vote address (will only return results of this address)")
|
||||
rpcAddr = flag.String("rpcURI", "", "Solana RPC URI (including protocol and path)")
|
||||
addr = flag.String("addr", ":8080", "Listen address")
|
||||
votePubkey = flag.String("votepubkey", "", "Validator vote address (will only return results of this address)")
|
||||
)
|
||||
|
||||
func init() {
|
||||
|
@ -34,6 +33,7 @@ type solanaCollector struct {
|
|||
validatorLastVote *prometheus.Desc
|
||||
validatorRootSlot *prometheus.Desc
|
||||
validatorDelinquent *prometheus.Desc
|
||||
solanaVersion *prometheus.Desc
|
||||
}
|
||||
|
||||
func NewSolanaCollector(rpcAddr string) *solanaCollector {
|
||||
|
@ -59,11 +59,16 @@ func NewSolanaCollector(rpcAddr string) *solanaCollector {
|
|||
"solana_validator_delinquent",
|
||||
"Whether a validator is delinquent",
|
||||
[]string{"pubkey", "nodekey"}, nil),
|
||||
solanaVersion: prometheus.NewDesc(
|
||||
"solana_node_version",
|
||||
"Node version of solana",
|
||||
[]string{"version"}, nil),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *solanaCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- c.totalValidatorsDesc
|
||||
ch <- c.solanaVersion
|
||||
}
|
||||
|
||||
func (c *solanaCollector) mustEmitMetrics(ch chan<- prometheus.Metric, response *rpc.GetVoteAccountsResponse) {
|
||||
|
@ -109,6 +114,14 @@ func (c *solanaCollector) Collect(ch chan<- prometheus.Metric) {
|
|||
} else {
|
||||
c.mustEmitMetrics(ch, accs)
|
||||
}
|
||||
|
||||
version, err := c.rpcClient.GetVersion(ctx)
|
||||
|
||||
if err != nil {
|
||||
ch <- prometheus.NewInvalidMetric(c.solanaVersion, err)
|
||||
} else {
|
||||
ch <- prometheus.MustNewConstMetric(c.solanaVersion, prometheus.GaugeValue, 1, *version)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package rpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
type (
|
||||
GetVersionResponse struct {
|
||||
Result struct {
|
||||
Version string `json:"solana-core"`
|
||||
} `json:"result"`
|
||||
Error rpcError `json:"error"`
|
||||
}
|
||||
)
|
||||
|
||||
func (c *RPCClient) GetVersion(ctx context.Context) (*string, error) {
|
||||
body, err := c.rpcRequest(ctx, formatRPCRequest("getVersion", []interface{}{}))
|
||||
|
||||
if body == nil {
|
||||
return nil, fmt.Errorf("RPC call failed: Body empty")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("RPC call failed: %w", err)
|
||||
}
|
||||
|
||||
klog.V(2).Infof("version response: %v", string(body))
|
||||
|
||||
var resp GetVersionResponse
|
||||
if err = json.Unmarshal(body, &resp); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response body: %w", err)
|
||||
}
|
||||
|
||||
if resp.Error.Code != 0 {
|
||||
return nil, fmt.Errorf("RPC error: %d %v", resp.Error.Code, resp.Error.Message)
|
||||
}
|
||||
|
||||
return &resp.Result.Version, nil
|
||||
}
|
Loading…
Reference in New Issue