Commit e399e495 authored by Adrian Sutton's avatar Adrian Sutton

op-node: Use a continuously updated histogram rather than replacing it on each update.

parent 7de5042c
package metrics
import (
"sync/atomic"
"github.com/prometheus/client_golang/prometheus"
)
type ReplaceableHistogramVec struct {
current *atomic.Value
opts prometheus.HistogramOpts
variableLabels []string
}
func NewReplaceableHistogramVec(registry *prometheus.Registry, opts prometheus.HistogramOpts, variableLabels []string) *ReplaceableHistogramVec {
metric := &ReplaceableHistogramVec{
current: &atomic.Value{},
opts: opts,
variableLabels: variableLabels,
}
metric.current.Store(prometheus.NewHistogramVec(opts, variableLabels))
registry.MustRegister(metric)
return metric
}
func (c *ReplaceableHistogramVec) Replace(updater func(h *prometheus.HistogramVec)) {
h := prometheus.NewHistogramVec(c.opts, c.variableLabels)
updater(h)
c.current.Store(h)
}
func (c *ReplaceableHistogramVec) Describe(ch chan<- *prometheus.Desc) {
collector, ok := c.current.Load().(prometheus.Collector)
if ok {
collector.Describe(ch)
}
}
func (c *ReplaceableHistogramVec) Collect(ch chan<- prometheus.Metric) {
collector, ok := c.current.Load().(prometheus.Collector)
if ok {
collector.Collect(ch)
}
}
...@@ -142,7 +142,7 @@ type Metrics struct { ...@@ -142,7 +142,7 @@ type Metrics struct {
IPUnbans prometheus.Counter IPUnbans prometheus.Counter
Dials *prometheus.CounterVec Dials *prometheus.CounterVec
Accepts *prometheus.CounterVec Accepts *prometheus.CounterVec
PeerScoresHistogram *ReplaceableHistogramVec PeerScoresHistogram *prometheus.HistogramVec
ChannelInputBytes prometheus.Counter ChannelInputBytes prometheus.Counter
...@@ -324,7 +324,7 @@ func NewMetrics(procName string) *Metrics { ...@@ -324,7 +324,7 @@ func NewMetrics(procName string) *Metrics {
}, []string{ }, []string{
"band", "band",
}), }),
PeerScoresHistogram: NewReplaceableHistogramVec(registry, prometheus.HistogramOpts{ PeerScoresHistogram: factory.NewHistogramVec(prometheus.HistogramOpts{
Namespace: ns, Namespace: ns,
Name: "peer_scores_histogram", Name: "peer_scores_histogram",
Help: "Histogram of currrently connected peer scores", Help: "Histogram of currrently connected peer scores",
...@@ -462,17 +462,15 @@ func NewMetrics(procName string) *Metrics { ...@@ -462,17 +462,15 @@ func NewMetrics(procName string) *Metrics {
// SetPeerScores updates the peer score [prometheus.GaugeVec]. // SetPeerScores updates the peer score [prometheus.GaugeVec].
// This takes a map of labels to scores. // This takes a map of labels to scores.
func (m *Metrics) SetPeerScores(scores map[string]float64, allScores []store.PeerScores) { func (m *Metrics) SetPeerScores(scores map[string]float64, allScores []store.PeerScores) {
m.PeerScoresHistogram.Replace(func(h *prometheus.HistogramVec) { for _, scores := range allScores {
for _, scores := range allScores { m.PeerScoresHistogram.WithLabelValues("total").Observe(scores.Gossip.Total)
h.WithLabelValues("total").Observe(scores.Gossip.Total) m.PeerScoresHistogram.WithLabelValues("ipColocation").Observe(scores.Gossip.IPColocationFactor)
h.WithLabelValues("ipColocation").Observe(scores.Gossip.IPColocationFactor) m.PeerScoresHistogram.WithLabelValues("behavioralPenalty").Observe(scores.Gossip.BehavioralPenalty)
h.WithLabelValues("behavioralPenalty").Observe(scores.Gossip.BehavioralPenalty) m.PeerScoresHistogram.WithLabelValues("blocksFirstMessage").Observe(scores.Gossip.Blocks.FirstMessageDeliveries)
h.WithLabelValues("blocksFirstMessage").Observe(scores.Gossip.Blocks.FirstMessageDeliveries) m.PeerScoresHistogram.WithLabelValues("blocksTimeInMesh").Observe(scores.Gossip.Blocks.TimeInMesh)
h.WithLabelValues("blocksTimeInMesh").Observe(scores.Gossip.Blocks.TimeInMesh) m.PeerScoresHistogram.WithLabelValues("blocksMessageDeliveries").Observe(scores.Gossip.Blocks.MeshMessageDeliveries)
h.WithLabelValues("blocksMessageDeliveries").Observe(scores.Gossip.Blocks.MeshMessageDeliveries) m.PeerScoresHistogram.WithLabelValues("blocksInvalidMessageDeliveries").Observe(scores.Gossip.Blocks.InvalidMessageDeliveries)
h.WithLabelValues("blocksInvalidMessageDeliveries").Observe(scores.Gossip.Blocks.InvalidMessageDeliveries) }
}
})
for label, score := range scores { for label, score := range scores {
m.PeerScores.WithLabelValues(label).Set(score) m.PeerScores.WithLabelValues(label).Set(score)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment