2023-04-05 08:44:13 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Handler returns an http.Handler for the local registry.
|
|
|
|
func Handler() http.Handler {
|
|
|
|
promhttp.Handler()
|
|
|
|
return promhttp.InstrumentMetricHandler(
|
|
|
|
registry,
|
|
|
|
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
registry = prometheus.NewRegistry()
|
2023-04-05 10:39:02 +00:00
|
|
|
// registeredDescriptionsMtx protects collectors slice.
|
2023-04-05 08:44:13 +00:00
|
|
|
// It should not be acessed concurrently, but we can easily forget this in future, thus this mutex.
|
2023-04-05 10:39:02 +00:00
|
|
|
registeredDescriptionsMtx sync.Mutex
|
|
|
|
registeredDescriptions []Description
|
2023-04-05 08:44:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2023-04-05 10:39:02 +00:00
|
|
|
registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
|
|
|
|
registry.MustRegister(collectors.NewGoCollector())
|
2023-04-05 08:44:13 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 10:39:02 +00:00
|
|
|
func mustRegister[T prometheus.Collector](cs ...metric[T]) {
|
2023-04-05 08:44:13 +00:00
|
|
|
for i := range cs {
|
2023-04-05 10:39:02 +00:00
|
|
|
registry.MustRegister(cs[i].value)
|
2023-04-05 08:44:13 +00:00
|
|
|
}
|
2023-04-05 10:39:02 +00:00
|
|
|
registeredDescriptionsMtx.Lock()
|
|
|
|
for i := range cs {
|
|
|
|
registeredDescriptions = append(registeredDescriptions, cs[i].desc)
|
|
|
|
}
|
|
|
|
registeredDescriptionsMtx.Unlock()
|
2023-04-05 08:44:13 +00:00
|
|
|
}
|