diff --git a/cmd/solana_exporter/exporter.go b/cmd/solana_exporter/exporter.go index 071a51b..01d3228 100644 --- a/cmd/solana_exporter/exporter.go +++ b/cmd/solana_exporter/exporter.go @@ -209,15 +209,15 @@ func main() { } client := rpc.NewRPCClient(config.RpcUrl, config.HttpTimeout) - ctx_, cancel := context.WithTimeout(ctx, config.HttpTimeout) - defer cancel() - votekeys, err := GetAssociatedVoteAccounts(ctx_, client, rpc.CommitmentFinalized, config.NodeKeys) + votekeys, err := GetAssociatedVoteAccounts(ctx, client, rpc.CommitmentFinalized, config.NodeKeys) if err != nil { klog.Fatalf("Failed to get associated vote accounts for %v: %v", config.NodeKeys, err) } collector := NewSolanaCollector(client, slotPacerSchedule, config.BalanceAddresses, config.NodeKeys, votekeys) slotWatcher := NewSlotWatcher(client, config.NodeKeys, votekeys, config.ComprehensiveSlotTracking) + ctx, cancel := context.WithCancel(ctx) + defer cancel() go slotWatcher.WatchSlots(ctx, collector.slotPace) prometheus.MustRegister(collector) diff --git a/cmd/solana_exporter/slots.go b/cmd/solana_exporter/slots.go index 1badb43..5b2c7e6 100644 --- a/cmd/solana_exporter/slots.go +++ b/cmd/solana_exporter/slots.go @@ -107,15 +107,29 @@ func NewSlotWatcher( ), } // register: - prometheus.MustRegister(watcher.TotalTransactionsMetric) - prometheus.MustRegister(watcher.SlotHeightMetric) - prometheus.MustRegister(watcher.EpochNumberMetric) - prometheus.MustRegister(watcher.EpochFirstSlotMetric) - prometheus.MustRegister(watcher.EpochLastSlotMetric) - prometheus.MustRegister(watcher.LeaderSlotsTotalMetric) - prometheus.MustRegister(watcher.LeaderSlotsByEpochMetric) - prometheus.MustRegister(watcher.InflationRewardsMetric) - prometheus.MustRegister(watcher.FeeRewardsMetric) + for _, collector := range []prometheus.Collector{ + watcher.TotalTransactionsMetric, + watcher.SlotHeightMetric, + watcher.EpochNumberMetric, + watcher.EpochFirstSlotMetric, + watcher.EpochLastSlotMetric, + watcher.LeaderSlotsTotalMetric, + watcher.LeaderSlotsByEpochMetric, + watcher.InflationRewardsMetric, + watcher.FeeRewardsMetric, + } { + if err := prometheus.Register(collector); err != nil { + var ( + alreadyRegisteredErr *prometheus.AlreadyRegisteredError + duplicateErr = strings.Contains(err.Error(), "duplicate metrics collector registration attempted") + ) + if errors.As(err, &alreadyRegisteredErr) || duplicateErr { + continue + } else { + klog.Fatal(fmt.Errorf("failed to register collector: %w", err)) + } + } + } return &watcher }