solana_exporter/cmd/solana-exporter/config.go

193 lines
4.9 KiB
Go
Raw Normal View History

2024-10-15 03:30:53 -07:00
package main
import (
"context"
2024-10-15 03:30:53 -07:00
"flag"
"fmt"
"github.com/asymmetric-research/solana-exporter/pkg/rpc"
"github.com/asymmetric-research/solana-exporter/pkg/slog"
2024-10-15 03:30:53 -07:00
"time"
)
type (
arrayFlags []string
ExporterConfig struct {
HttpTimeout time.Duration
RpcUrl string
ListenAddress string
NodeKeys []string
VoteKeys []string
2024-10-15 03:30:53 -07:00
BalanceAddresses []string
ComprehensiveSlotTracking bool
2024-10-15 08:50:04 -07:00
MonitorBlockSizes bool
2024-10-25 03:54:57 -07:00
LightMode bool
SlotPace time.Duration
2024-10-15 03:30:53 -07:00
}
)
func (i *arrayFlags) String() string {
return fmt.Sprint(*i)
}
func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func NewExporterConfig(
ctx context.Context,
httpTimeout time.Duration,
2024-10-15 03:30:53 -07:00
rpcUrl string,
listenAddress string,
nodeKeys []string,
balanceAddresses []string,
comprehensiveSlotTracking bool,
2024-10-15 08:50:04 -07:00
monitorBlockSizes bool,
2024-10-25 03:54:57 -07:00
lightMode bool,
slotPace time.Duration,
2024-10-25 03:54:57 -07:00
) (*ExporterConfig, error) {
2024-10-25 02:50:16 -07:00
logger := slog.Get()
logger.Infow(
"Setting up export config with ",
"httpTimeout", httpTimeout.Seconds(),
2024-10-25 02:50:16 -07:00
"rpcUrl", rpcUrl,
"listenAddress", listenAddress,
"nodeKeys", nodeKeys,
"balanceAddresses", balanceAddresses,
"comprehensiveSlotTracking", comprehensiveSlotTracking,
"monitorBlockSizes", monitorBlockSizes,
2024-10-25 03:54:57 -07:00
"lightMode", lightMode,
2024-10-25 02:50:16 -07:00
)
if lightMode {
if comprehensiveSlotTracking {
2024-11-05 10:54:24 -08:00
return nil, fmt.Errorf("'-light-mode' is incompatible with `-comprehensive-slot-tracking`")
}
if monitorBlockSizes {
2024-11-05 10:54:24 -08:00
return nil, fmt.Errorf("'-light-mode' is incompatible with `-monitor-block-sizes`")
}
if len(nodeKeys) > 0 {
2024-11-05 10:54:24 -08:00
return nil, fmt.Errorf("'-light-mode' is incompatible with `-nodekey`")
}
if len(balanceAddresses) > 0 {
2024-11-05 10:54:24 -08:00
return nil, fmt.Errorf("'-light-mode' is incompatible with `-balance-addresses`")
}
2024-10-25 03:54:57 -07:00
}
2024-10-27 04:04:45 -07:00
// get votekeys from rpc:
ctx, cancel := context.WithTimeout(ctx, httpTimeout)
defer cancel()
client := rpc.NewRPCClient(rpcUrl, httpTimeout)
voteKeys, err := GetAssociatedVoteAccounts(ctx, client, rpc.CommitmentFinalized, nodeKeys)
if err != nil {
return nil, fmt.Errorf("error getting vote accounts: %w", err)
}
2024-10-25 03:54:57 -07:00
config := ExporterConfig{
HttpTimeout: httpTimeout,
2024-10-15 03:30:53 -07:00
RpcUrl: rpcUrl,
ListenAddress: listenAddress,
NodeKeys: nodeKeys,
VoteKeys: voteKeys,
2024-10-15 03:30:53 -07:00
BalanceAddresses: balanceAddresses,
ComprehensiveSlotTracking: comprehensiveSlotTracking,
2024-10-15 08:50:04 -07:00
MonitorBlockSizes: monitorBlockSizes,
2024-10-25 03:54:57 -07:00
LightMode: lightMode,
SlotPace: slotPace,
2024-10-15 03:30:53 -07:00
}
2024-10-25 03:54:57 -07:00
return &config, nil
2024-10-15 03:30:53 -07:00
}
func NewExporterConfigFromCLI(ctx context.Context) (*ExporterConfig, error) {
2024-10-15 03:30:53 -07:00
var (
httpTimeout int
rpcUrl string
listenAddress string
nodekeys arrayFlags
balanceAddresses arrayFlags
comprehensiveSlotTracking bool
2024-10-15 08:50:04 -07:00
monitorBlockSizes bool
2024-10-25 03:54:57 -07:00
lightMode bool
slotPace int
2024-10-15 03:30:53 -07:00
)
flag.IntVar(
&httpTimeout,
"http-timeout",
60,
"HTTP timeout to use, in seconds.",
)
flag.StringVar(
&rpcUrl,
"rpc-url",
"http://localhost:8899",
"Solana RPC URL (including protocol and path), "+
"e.g., 'http://localhost:8899' or 'https://api.mainnet-beta.solana.com'",
)
flag.StringVar(
&listenAddress,
"listen-address",
":8080",
"Listen address",
)
flag.Var(
&nodekeys,
"nodekey",
"Solana nodekey (identity account) representing validator to monitor - can set multiple times.",
)
flag.Var(
&balanceAddresses,
"balance-address",
"Address to monitor SOL balances for, in addition to the identity and vote accounts of the "+
"provided nodekeys - can be set multiple times.",
)
flag.BoolVar(
&comprehensiveSlotTracking,
"comprehensive-slot-tracking",
false,
"Set this flag to track solana_leader_slots_by_epoch for ALL validators. "+
"Warning: this will lead to potentially thousands of new Prometheus metrics being created every epoch.",
)
2024-10-15 08:50:04 -07:00
flag.BoolVar(
&monitorBlockSizes,
"monitor-block-sizes",
false,
"Set this flag to track block sizes (number of transactions) for the configured validators. "+
"Warning: this might grind the RPC node.",
)
2024-10-25 03:54:57 -07:00
flag.BoolVar(
&lightMode,
"light-mode",
false,
"Set this flag to enable light-mode. In light mode, only metrics specific to the node being queried "+
"are reported (i.e., metrics such as inflation rewards which are visible from any RPC node, "+
"are not reported).",
)
flag.IntVar(
&slotPace,
"slot-pace",
1,
"This is the time between slot-watching metric collections, defaults to 1s.",
)
2024-10-15 03:30:53 -07:00
flag.Parse()
2024-10-25 03:54:57 -07:00
config, err := NewExporterConfig(
ctx,
time.Duration(httpTimeout)*time.Second,
2024-10-25 03:54:57 -07:00
rpcUrl,
listenAddress,
nodekeys,
balanceAddresses,
comprehensiveSlotTracking,
monitorBlockSizes,
lightMode,
time.Duration(slotPace)*time.Second,
2024-10-15 08:50:04 -07:00
)
2024-10-25 03:54:57 -07:00
if err != nil {
return nil, err
}
return config, nil
2024-10-15 03:30:53 -07:00
}