cluster/delegate: Replace labels to const to reduce hardcode (#1724)

Signed-off-by: yeya24 <ben.ye@daocloud.io>
This commit is contained in:
Ye Ben 2019-01-28 17:17:55 +08:00 committed by Max Inden
parent fc1153560d
commit 5f8eaf9560
1 changed files with 22 additions and 18 deletions

View File

@ -24,8 +24,12 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
// Maximum number of messages to be held in the queue.
const maxQueueSize = 4096
const (
// Maximum number of messages to be held in the queue.
maxQueueSize = 4096
fullState = "full_state"
update = "update"
)
// delegate implements memberlist.Delegate and memberlist.EventDelegate
// and broadcasts its peer's state in the cluster.
@ -92,14 +96,14 @@ func newDelegate(l log.Logger, reg prometheus.Registerer, p *Peer, retransmit in
return float64(bcast.NumQueued())
})
messagesReceived.WithLabelValues("full_state")
messagesReceivedSize.WithLabelValues("full_state")
messagesReceived.WithLabelValues("update")
messagesReceivedSize.WithLabelValues("update")
messagesSent.WithLabelValues("full_state")
messagesSentSize.WithLabelValues("full_state")
messagesSent.WithLabelValues("update")
messagesSentSize.WithLabelValues("update")
messagesReceived.WithLabelValues(fullState)
messagesReceivedSize.WithLabelValues(fullState)
messagesReceived.WithLabelValues(update)
messagesReceivedSize.WithLabelValues(update)
messagesSent.WithLabelValues(fullState)
messagesSentSize.WithLabelValues(fullState)
messagesSent.WithLabelValues(update)
messagesSentSize.WithLabelValues(update)
reg.MustRegister(messagesReceived, messagesReceivedSize, messagesSent, messagesSentSize,
gossipClusterMembers, peerPosition, healthScore, messagesQueued, messagesPruned)
@ -127,8 +131,8 @@ func (d *delegate) NodeMeta(limit int) []byte {
// NotifyMsg is the callback invoked when a user-level gossip message is received.
func (d *delegate) NotifyMsg(b []byte) {
d.messagesReceived.WithLabelValues("update").Inc()
d.messagesReceivedSize.WithLabelValues("update").Add(float64(len(b)))
d.messagesReceived.WithLabelValues(update).Inc()
d.messagesReceivedSize.WithLabelValues(update).Add(float64(len(b)))
var p clusterpb.Part
if err := proto.Unmarshal(b, &p); err != nil {
@ -149,9 +153,9 @@ func (d *delegate) NotifyMsg(b []byte) {
// GetBroadcasts is called when user data messages can be broadcasted.
func (d *delegate) GetBroadcasts(overhead, limit int) [][]byte {
msgs := d.bcast.GetBroadcasts(overhead, limit)
d.messagesSent.WithLabelValues("update").Add(float64(len(msgs)))
d.messagesSent.WithLabelValues(update).Add(float64(len(msgs)))
for _, m := range msgs {
d.messagesSentSize.WithLabelValues("update").Add(float64(len(m)))
d.messagesSentSize.WithLabelValues(update).Add(float64(len(m)))
}
return msgs
}
@ -175,14 +179,14 @@ func (d *delegate) LocalState(_ bool) []byte {
level.Warn(d.logger).Log("msg", "encode local state", "err", err)
return nil
}
d.messagesSent.WithLabelValues("full_state").Inc()
d.messagesSentSize.WithLabelValues("full_state").Add(float64(len(b)))
d.messagesSent.WithLabelValues(fullState).Inc()
d.messagesSentSize.WithLabelValues(fullState).Add(float64(len(b)))
return b
}
func (d *delegate) MergeRemoteState(buf []byte, _ bool) {
d.messagesReceived.WithLabelValues("full_state").Inc()
d.messagesReceivedSize.WithLabelValues("full_state").Add(float64(len(buf)))
d.messagesReceived.WithLabelValues(fullState).Inc()
d.messagesReceivedSize.WithLabelValues(fullState).Add(float64(len(buf)))
var fs clusterpb.FullState
if err := proto.Unmarshal(buf, &fs); err != nil {