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)
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)

View File

@ -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
}