Skip to content

Commit

Permalink
fixed calling registration multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitp-be committed Jan 27, 2025
1 parent 5330d2b commit 6a47429
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
12 changes: 11 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,18 @@ func main() {

go nw.watch(logger, watcher, cfgCont, ctx)

// register exporter only once
err = prometheus.Register(exporter)
if err != nil {
if !prometheus.Unregister(exporter) {
_ = level.Error(logger).Log("msg", "Exporter can't be unregistered")
return
}
prometheus.MustRegister(exporter)
}

metricHandlerFunc := collector.MetricHandler(exporter, *maxRequests, logger)
http.Handle(*metricsPath, promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, utils.AddHSTSHeader(metricHandlerFunc)))
http.Handle(*metricsPath, utils.AddHSTSHeader(promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, metricHandlerFunc)))
http.Handle("/-/ready", utils.AddHSTSHeader(readinessChecker()))
http.Handle("/-/healthy", utils.AddHSTSHeader(healthChecker()))

Expand Down
9 changes: 0 additions & 9 deletions pkg/collector/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ func MetricHandler(exporter *Exporter, maxRequests int, logger log.Logger) http.
exporter.Mutex.RLock()
defer exporter.Mutex.RUnlock()

err := prometheus.Register(exporter)
if err != nil {
if !prometheus.Unregister(exporter) {
_ = level.Error(logger).Log("msg", "Exporter can't be unregistered")
return
}
prometheus.MustRegister(exporter)
}

// Delegate http serving to Prometheus client library, which will call collector.Collect.
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
ErrorLog: stdlog.New(log.NewStdlibAdapter(level.Debug(logger)), "prom_log: ", 0),
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetNamespace() string {
return "monitoring"
}

// HSTS header
// add HSTS header
func AddHSTSHeader(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Scheme == "https" {
Expand Down

0 comments on commit 6a47429

Please sign in to comment.