[#164] metrics: Fill local registry explicitly

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-04-05 11:44:13 +03:00 committed by Evgenii Stratonikov
parent 59822f7fb4
commit 015d62425b
7 changed files with 72 additions and 32 deletions

View file

@ -2,7 +2,7 @@ package main
import ( import (
metricsconfig "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-node/config/metrics" metricsconfig "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-node/config/metrics"
"github.com/prometheus/client_golang/prometheus/promhttp" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/metrics"
) )
func metricsComponent(c *cfg) (*httpComponent, bool) { func metricsComponent(c *cfg) (*httpComponent, bool) {
@ -12,7 +12,7 @@ func metricsComponent(c *cfg) (*httpComponent, bool) {
c.dynamicConfiguration.metrics = new(httpComponent) c.dynamicConfiguration.metrics = new(httpComponent)
c.dynamicConfiguration.metrics.cfg = c c.dynamicConfiguration.metrics.cfg = c
c.dynamicConfiguration.metrics.name = "metrics" c.dynamicConfiguration.metrics.name = "metrics"
c.dynamicConfiguration.metrics.handler = promhttp.Handler() c.dynamicConfiguration.metrics.handler = metrics.Handler()
updated = true updated = true
} }

View file

@ -72,19 +72,19 @@ func newEngineGaugeVector(name, help string, labels []string) *prometheus.GaugeV
} }
func (m engineMetrics) register() { func (m engineMetrics) register() {
prometheus.MustRegister(m.listContainersDuration) mustRegister(m.listContainersDuration)
prometheus.MustRegister(m.estimateContainerSizeDuration) mustRegister(m.estimateContainerSizeDuration)
prometheus.MustRegister(m.deleteDuration) mustRegister(m.deleteDuration)
prometheus.MustRegister(m.existsDuration) mustRegister(m.existsDuration)
prometheus.MustRegister(m.getDuration) mustRegister(m.getDuration)
prometheus.MustRegister(m.headDuration) mustRegister(m.headDuration)
prometheus.MustRegister(m.inhumeDuration) mustRegister(m.inhumeDuration)
prometheus.MustRegister(m.putDuration) mustRegister(m.putDuration)
prometheus.MustRegister(m.rangeDuration) mustRegister(m.rangeDuration)
prometheus.MustRegister(m.searchDuration) mustRegister(m.searchDuration)
prometheus.MustRegister(m.listObjectsDuration) mustRegister(m.listObjectsDuration)
prometheus.MustRegister(m.containerSize) mustRegister(m.containerSize)
prometheus.MustRegister(m.payloadSize) mustRegister(m.payloadSize)
} }
func (m engineMetrics) AddListContainersDuration(d time.Duration) { func (m engineMetrics) AddListContainersDuration(d time.Duration) {

View file

@ -27,8 +27,8 @@ func NewInnerRingMetrics() InnerRingServiceMetrics {
}) })
) )
prometheus.MustRegister(epoch) mustRegister(epoch)
prometheus.MustRegister(health) mustRegister(health)
return InnerRingServiceMetrics{ return InnerRingServiceMetrics{
epoch: epoch, epoch: epoch,

View file

@ -27,7 +27,7 @@ func NewNodeMetrics() *NodeMetrics {
Name: "epoch", Name: "epoch",
Help: "Current epoch as seen by inner-ring node.", Help: "Current epoch as seen by inner-ring node.",
}) })
prometheus.MustRegister(epoch) mustRegister(epoch)
return &NodeMetrics{ return &NodeMetrics{
objectServiceMetrics: objectService, objectServiceMetrics: objectService,

View file

@ -65,8 +65,8 @@ func newObjectMethodCallCounter(name string) methodCount {
} }
func (m methodCount) mustRegister() { func (m methodCount) mustRegister() {
prometheus.MustRegister(m.success) mustRegister(m.success)
prometheus.MustRegister(m.total) mustRegister(m.total)
} }
func (m methodCount) Inc(success bool) { func (m methodCount) Inc(success bool) {
@ -135,19 +135,19 @@ func (m objectServiceMetrics) register() {
m.rangeCounter.mustRegister() m.rangeCounter.mustRegister()
m.rangeHashCounter.mustRegister() m.rangeHashCounter.mustRegister()
prometheus.MustRegister(m.getDuration) mustRegister(m.getDuration)
prometheus.MustRegister(m.putDuration) mustRegister(m.putDuration)
prometheus.MustRegister(m.headDuration) mustRegister(m.headDuration)
prometheus.MustRegister(m.searchDuration) mustRegister(m.searchDuration)
prometheus.MustRegister(m.deleteDuration) mustRegister(m.deleteDuration)
prometheus.MustRegister(m.rangeDuration) mustRegister(m.rangeDuration)
prometheus.MustRegister(m.rangeHashDuration) mustRegister(m.rangeHashDuration)
prometheus.MustRegister(m.putPayload) mustRegister(m.putPayload)
prometheus.MustRegister(m.getPayload) mustRegister(m.getPayload)
prometheus.MustRegister(m.shardMetrics) mustRegister(m.shardMetrics)
prometheus.MustRegister(m.shardsReadonly) mustRegister(m.shardsReadonly)
} }
func (m objectServiceMetrics) IncGetReqCounter(success bool) { func (m objectServiceMetrics) IncGetReqCounter(success bool) {

40
pkg/metrics/registry.go Normal file
View 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()
}

View file

@ -20,7 +20,7 @@ func newStateMetrics() stateMetrics {
} }
func (m stateMetrics) register() { func (m stateMetrics) register() {
prometheus.MustRegister(m.healthCheck) mustRegister(m.healthCheck)
} }
func (m stateMetrics) SetHealth(s int32) { func (m stateMetrics) SetHealth(s int32) {