github.com/koko1123/flow-go-1@v0.29.6/module/metrics/ping.go (about)

     1  package metrics
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  	"github.com/prometheus/client_golang/prometheus/promauto"
     8  
     9  	"github.com/koko1123/flow-go-1/model/flow"
    10  )
    11  
    12  type PingCollector struct {
    13  	reachable       *prometheus.GaugeVec
    14  	sealedHeight    *prometheus.GaugeVec
    15  	hotstuffCurView *prometheus.GaugeVec
    16  }
    17  
    18  func NewPingCollector() *PingCollector {
    19  	pc := &PingCollector{
    20  		reachable: promauto.NewGaugeVec(prometheus.GaugeOpts{
    21  			Name:      "node_reachable",
    22  			Namespace: namespaceNetwork,
    23  			Subsystem: subsystemGossip,
    24  			Help:      "report whether a node is reachable",
    25  		}, []string{LabelNodeID, LabelNodeAddress, LabelNodeRole, LabelNodeInfo}),
    26  		sealedHeight: promauto.NewGaugeVec(prometheus.GaugeOpts{
    27  			Name:      "sealed_height",
    28  			Namespace: namespaceNetwork,
    29  			Subsystem: subsystemGossip,
    30  			Help:      "the last sealed height of a node",
    31  		}, []string{LabelNodeID, LabelNodeAddress, LabelNodeRole, LabelNodeInfo, LabelNodeVersion}),
    32  		hotstuffCurView: promauto.NewGaugeVec(prometheus.GaugeOpts{
    33  			Name:      "hotstuff_curview",
    34  			Namespace: namespaceNetwork,
    35  			Subsystem: subsystemGossip,
    36  			Help:      "the hotstuff current view",
    37  		}, []string{LabelNodeID, LabelNodeAddress, LabelNodeInfo}),
    38  	}
    39  
    40  	return pc
    41  }
    42  
    43  func (pc *PingCollector) NodeReachable(node *flow.Identity, nodeInfo string, rtt time.Duration) {
    44  	var rttValue float64
    45  	if rtt > 0 {
    46  		rttValue = float64(rtt.Milliseconds())
    47  	} else {
    48  		rttValue = -1
    49  	}
    50  
    51  	pc.reachable.With(prometheus.Labels{
    52  		LabelNodeID:      node.NodeID.String(),
    53  		LabelNodeAddress: node.Address,
    54  		LabelNodeRole:    node.Role.String(),
    55  		LabelNodeInfo:    nodeInfo}).
    56  		Set(rttValue)
    57  }
    58  
    59  func (pc *PingCollector) NodeInfo(node *flow.Identity, nodeInfo string, version string, sealedHeight uint64, hotstuffCurView uint64) {
    60  	pc.sealedHeight.With(prometheus.Labels{
    61  		LabelNodeID:      node.NodeID.String(),
    62  		LabelNodeAddress: node.Address,
    63  		LabelNodeRole:    node.Role.String(),
    64  		LabelNodeInfo:    nodeInfo,
    65  		LabelNodeVersion: version}).
    66  		Set(float64(sealedHeight))
    67  
    68  	// we only need this metrics from consensus nodes.
    69  	// since non-consensus nodes will report this metrics as well, and the value will always be 0,
    70  	// we can exclude metrics from non-consensus nodes by checking if the value is above 0.
    71  	// consensus nodes will start this metrics value with 0 as well, and won't report, but that's
    72  	// OK, because their hotstuff view will quickly go up and start reporting.
    73  	if hotstuffCurView > 0 {
    74  		pc.hotstuffCurView.With(prometheus.Labels{
    75  			LabelNodeID:      node.NodeID.String(),
    76  			LabelNodeAddress: node.Address,
    77  			LabelNodeInfo:    nodeInfo,
    78  		}).
    79  			Set(float64(hotstuffCurView))
    80  	}
    81  }