fixed duplicate register issue

This commit is contained in:
Matt Johnstone 2024-10-15 12:56:36 +02:00
parent 7bfab5fae3
commit 0985203997
No known key found for this signature in database
GPG Key ID: BE985FBB9BE7D3BB
2 changed files with 26 additions and 12 deletions

View File

@ -209,15 +209,15 @@ func main() {
} }
client := rpc.NewRPCClient(config.RpcUrl, config.HttpTimeout) client := rpc.NewRPCClient(config.RpcUrl, config.HttpTimeout)
ctx_, cancel := context.WithTimeout(ctx, config.HttpTimeout) votekeys, err := GetAssociatedVoteAccounts(ctx, client, rpc.CommitmentFinalized, config.NodeKeys)
defer cancel()
votekeys, err := GetAssociatedVoteAccounts(ctx_, client, rpc.CommitmentFinalized, config.NodeKeys)
if err != nil { if err != nil {
klog.Fatalf("Failed to get associated vote accounts for %v: %v", config.NodeKeys, err) klog.Fatalf("Failed to get associated vote accounts for %v: %v", config.NodeKeys, err)
} }
collector := NewSolanaCollector(client, slotPacerSchedule, config.BalanceAddresses, config.NodeKeys, votekeys) collector := NewSolanaCollector(client, slotPacerSchedule, config.BalanceAddresses, config.NodeKeys, votekeys)
slotWatcher := NewSlotWatcher(client, config.NodeKeys, votekeys, config.ComprehensiveSlotTracking) slotWatcher := NewSlotWatcher(client, config.NodeKeys, votekeys, config.ComprehensiveSlotTracking)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go slotWatcher.WatchSlots(ctx, collector.slotPace) go slotWatcher.WatchSlots(ctx, collector.slotPace)
prometheus.MustRegister(collector) prometheus.MustRegister(collector)

View File

@ -107,15 +107,29 @@ func NewSlotWatcher(
), ),
} }
// register: // register:
prometheus.MustRegister(watcher.TotalTransactionsMetric) for _, collector := range []prometheus.Collector{
prometheus.MustRegister(watcher.SlotHeightMetric) watcher.TotalTransactionsMetric,
prometheus.MustRegister(watcher.EpochNumberMetric) watcher.SlotHeightMetric,
prometheus.MustRegister(watcher.EpochFirstSlotMetric) watcher.EpochNumberMetric,
prometheus.MustRegister(watcher.EpochLastSlotMetric) watcher.EpochFirstSlotMetric,
prometheus.MustRegister(watcher.LeaderSlotsTotalMetric) watcher.EpochLastSlotMetric,
prometheus.MustRegister(watcher.LeaderSlotsByEpochMetric) watcher.LeaderSlotsTotalMetric,
prometheus.MustRegister(watcher.InflationRewardsMetric) watcher.LeaderSlotsByEpochMetric,
prometheus.MustRegister(watcher.FeeRewardsMetric) 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 return &watcher
} }