diff --git a/options.go b/options.go index dd2a15e..a9adfe3 100644 --- a/options.go +++ b/options.go @@ -90,7 +90,7 @@ func WithMetricsHandler() Option { s.Router.Handle("/metrics", promhttp.InstrumentMetricHandler( s.internalRegister, /* Register */ - promhttp.HandlerFor(s.gatherers, promhttp.HandlerOpts{}))) + http.HandlerFunc(s.metricsHandler))) return nil } diff --git a/svc.go b/svc.go index 6ac0942..5786748 100644 --- a/svc.go +++ b/svc.go @@ -11,6 +11,7 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" "go.uber.org/zap" ) @@ -43,6 +44,7 @@ type SVC struct { gatherers prometheus.Gatherers internalRegister *prometheus.Registry + promHander http.Handler } // New instantiates a new service by parsing configuration and initializing a @@ -100,6 +102,7 @@ func (s *SVC) AddWorker(name string, w Worker) { } func (s *SVC) AddGatherer(gatherer prometheus.Gatherer) { + s.promHander = nil s.gatherers = append(s.gatherers, gatherer) } @@ -231,3 +234,10 @@ func (s *SVC) recoverWait(name string, wg *sync.WaitGroup, errors chan<- error) } } } + +func (s *SVC) metricsHandler(w http.ResponseWriter, r *http.Request) { + if s.promHander == nil { + s.promHander = promhttp.HandlerFor(s.gatherers, promhttp.HandlerOpts{}) + } + s.promHander.ServeHTTP(w, r) +}