forked from TrueCloudLab/frostfs-node
[#164] metrics: Fill local registry explicitly
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
59822f7fb4
commit
015d62425b
7 changed files with 72 additions and 32 deletions
40
pkg/metrics/registry.go
Normal file
40
pkg/metrics/registry.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
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()
|
||||
// registeredCollectorsMtx protects collectors slice.
|
||||
// It should not be acessed concurrently, but we can easily forget this in future, thus this mutex.
|
||||
registeredCollectorsMtx sync.Mutex
|
||||
registeredCollectors []prometheus.Collector
|
||||
)
|
||||
|
||||
func init() {
|
||||
mustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
|
||||
mustRegister(collectors.NewGoCollector())
|
||||
}
|
||||
|
||||
func mustRegister(cs ...prometheus.Collector) {
|
||||
for i := range cs {
|
||||
registry.MustRegister(cs[i])
|
||||
}
|
||||
registeredCollectorsMtx.Lock()
|
||||
registeredCollectors = append(registeredCollectors, cs...)
|
||||
registeredCollectorsMtx.Unlock()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue