mirror of https://github.com/libp2p/go-libp2p.git
Browse Source
* provided a WithRegisterer option for metrics * provide a libp2p.Option to setup metricsupdate-quic-go-v033
Sukun
2 years ago
committed by
GitHub
10 changed files with 234 additions and 91 deletions
@ -0,0 +1,20 @@ |
|||||
|
package metricshelper |
||||
|
|
||||
|
import ( |
||||
|
"errors" |
||||
|
|
||||
|
"github.com/prometheus/client_golang/prometheus" |
||||
|
) |
||||
|
|
||||
|
// RegisterCollectors registers the collectors with reg ignoring
|
||||
|
// reregistration error and panics on any other error
|
||||
|
func RegisterCollectors(reg prometheus.Registerer, collectors ...prometheus.Collector) { |
||||
|
for _, c := range collectors { |
||||
|
err := reg.Register(c) |
||||
|
if err != nil { |
||||
|
if ok := errors.As(err, &prometheus.AlreadyRegisteredError{}); !ok { |
||||
|
panic(err) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
package metricshelper |
||||
|
|
||||
|
import ( |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/prometheus/client_golang/prometheus" |
||||
|
|
||||
|
"github.com/stretchr/testify/require" |
||||
|
) |
||||
|
|
||||
|
func TestRegisterCollectors(t *testing.T) { |
||||
|
reg := prometheus.NewRegistry() |
||||
|
c1 := prometheus.NewCounter( |
||||
|
prometheus.CounterOpts{ |
||||
|
Name: "counter", |
||||
|
}, |
||||
|
) |
||||
|
c2 := prometheus.NewGauge( |
||||
|
prometheus.GaugeOpts{ |
||||
|
Namespace: "test", |
||||
|
Name: "gauge", |
||||
|
}, |
||||
|
) |
||||
|
// c3 == c1
|
||||
|
c3 := prometheus.NewCounter( |
||||
|
prometheus.CounterOpts{ |
||||
|
Name: "counter", |
||||
|
}, |
||||
|
) |
||||
|
require.NotPanics(t, func() { RegisterCollectors(reg, c1, c2) }) |
||||
|
require.NotPanics(t, func() { RegisterCollectors(reg, c3) }, "should not panic on duplicate registration") |
||||
|
} |
Loading…
Reference in new issue