Skip to content

Commit

Permalink
Handle gatherer correctly
Browse files Browse the repository at this point in the history
We need to recreate the handler for the gatherers every time we add a
new one.
  • Loading branch information
drPytho committed Oct 22, 2020
1 parent ae48f5d commit fe2b277
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

This comment has been minimized.

Copy link
@peterhellberg

peterhellberg Feb 12, 2021

Typo in the field name (promHander should be promHandler)

}

// 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 fe2b277

Please sign in to comment.