44 lines
1.3 KiB
Go
44 lines
1.3 KiB
Go
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"
|
|
)
|
|
|
|
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())
|
|
}
|
|
|
|
// Register registers custom collectors to registry.
|
|
// Should be used with metrics from other packages.
|
|
func Register(customCollectors ...prometheus.Collector) {
|
|
registry.MustRegister(customCollectors...)
|
|
}
|
|
|
|
func MustRegister(c prometheus.Collector, descs ...Description) {
|
|
registry.MustRegister(c)
|
|
registeredDescriptionsMtx.Lock()
|
|
defer registeredDescriptionsMtx.Unlock()
|
|
registeredDescriptions = append(registeredDescriptions, descs...)
|
|
}
|
|
|
|
// Handler returns an http.Handler for the local registry.
|
|
func Handler() http.Handler {
|
|
promhttp.Handler()
|
|
return promhttp.InstrumentMetricHandler(
|
|
registry,
|
|
promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
|
|
}
|