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() // registeredDescriptionsMtx protects collectors slice. // It should not be acessed concurrently, but we can easily forget this in future, thus this mutex. registeredDescriptionsMtx sync.Mutex registeredDescriptions []Description ) func init() { registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})) registry.MustRegister(collectors.NewGoCollector()) } func mustRegister[T prometheus.Collector](cs ...metric[T]) { for i := range cs { registry.MustRegister(cs[i].value) } registeredDescriptionsMtx.Lock() for i := range cs { registeredDescriptions = append(registeredDescriptions, cs[i].desc) } registeredDescriptionsMtx.Unlock() }