From 0d624176de0f9d43b76c810f913859147511e83f Mon Sep 17 00:00:00 2001 From: Abdul Rabbani Date: Wed, 25 May 2022 10:12:38 -0400 Subject: [PATCH] Handle duplicate registrations --- pkg/beaconclient/healthcheck_test.go | 10 ++++++++-- pkg/beaconclient/metrics.go | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/beaconclient/healthcheck_test.go b/pkg/beaconclient/healthcheck_test.go index 06ba2dd..df92398 100644 --- a/pkg/beaconclient/healthcheck_test.go +++ b/pkg/beaconclient/healthcheck_test.go @@ -25,9 +25,15 @@ import ( var _ = Describe("Healthcheck", func() { var ( - BC = beaconclient.CreateBeaconClient(context.Background(), "http", "localhost", 5052, 10) - errBc = beaconclient.CreateBeaconClient(context.Background(), "http", "blah-blah", 1010, 10) + BC *beaconclient.BeaconClient + errBc *beaconclient.BeaconClient ) + + BeforeEach(func() { + BC = beaconclient.CreateBeaconClient(context.Background(), "http", "localhost", 5052, 10) + errBc = beaconclient.CreateBeaconClient(context.Background(), "http", "blah-blah", 1010, 10) + + }) Describe("Connecting to the lighthouse client", Label("integration"), func() { Context("When the client is running", func() { It("We should connect successfully", func() { diff --git a/pkg/beaconclient/metrics.go b/pkg/beaconclient/metrics.go index 4a56fe6..6156e2d 100644 --- a/pkg/beaconclient/metrics.go +++ b/pkg/beaconclient/metrics.go @@ -20,6 +20,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/sirupsen/logrus" + "github.com/vulcanize/ipld-ethcl-indexer/pkg/loghelper" ) //Create a metric struct and register each channel with prometheus @@ -44,7 +45,7 @@ func CreateBeaconClientMetrics() *BeaconClientMetrics { } func prometheusRegisterHelper(name string, help string, varPointer *uint64) { - prometheus.MustRegister(prometheus.NewCounterFunc( + err := prometheus.Register(prometheus.NewCounterFunc( prometheus.CounterOpts{ Namespace: "beacon_client", Subsystem: "", @@ -55,6 +56,9 @@ func prometheusRegisterHelper(name string, help string, varPointer *uint64) { func() float64 { return float64(atomic.LoadUint64(varPointer)) })) + if err != nil && err.Error() != "duplicate metrics collector registration attempted" { + loghelper.LogError(err).WithField("name", name).Error("Unable to register counter.") + } } // A structure utilized for keeping track of various metrics. Currently, mostly used in testing.