Skip to content

Commit

Permalink
Merge pull request #25 from voi-oss/handle-register-gatherer-correctly
Browse files Browse the repository at this point in the history
Handle gatherer correctly
  • Loading branch information
drPytho authored Oct 22, 2020
2 parents ae48f5d + fe2b277 commit e0fec32
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

0 comments on commit e0fec32

Please sign in to comment.