From 891381c371a988696f9cbc75316693d6850b1d53 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Tue, 13 Mar 2018 17:16:56 +0000 Subject: [PATCH] Use pre-existing metric for log messages --- promrus.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/promrus.go b/promrus.go index 2b435c1..dcde57f 100644 --- a/promrus.go +++ b/promrus.go @@ -24,13 +24,17 @@ func NewPrometheusHook() (*PrometheusHook, error) { for _, level := range supportedLevels { counterVec.WithLabelValues(level.String()) } - // Try to unregister the counter vector, in case already registered for some reason, - // e.g. double initialisation/configuration done by mistake by the end-user. - prometheus.Unregister(counterVec) - // Try to register the counter vector: err := prometheus.Register(counterVec) + // If another library already registered the same metric, use it if err != nil { - return nil, err + ar, ok := err.(prometheus.AlreadyRegisteredError) + if !ok { + return nil, err + } + counterVec, ok = ar.ExistingCollector.(*prometheus.CounterVec) + if !ok { + return nil, err + } } return &PrometheusHook{ counterVec: counterVec,