github.com/koko1123/flow-go-1@v0.29.6/network/p2p/p2pnode/gossipsubMetrics.go (about) 1 package p2pnode 2 3 import ( 4 pubsub "github.com/libp2p/go-libp2p-pubsub" 5 "github.com/libp2p/go-libp2p/core/peer" 6 "github.com/rs/zerolog" 7 8 "github.com/koko1123/flow-go-1/module" 9 ) 10 11 // GossipSubControlMessageMetrics is a metrics and observability wrapper component for the incoming RPCs to a 12 // GossipSub router. It records metrics on the number of control messages received in each RPC. 13 type GossipSubControlMessageMetrics struct { 14 metrics module.GossipSubRouterMetrics 15 logger zerolog.Logger 16 } 17 18 func NewGossipSubControlMessageMetrics(metrics module.GossipSubRouterMetrics, logger zerolog.Logger) *GossipSubControlMessageMetrics { 19 return &GossipSubControlMessageMetrics{ 20 logger: logger.With().Str("module", "gossipsub-control-message-metrics").Logger(), 21 metrics: metrics, 22 } 23 } 24 25 // ObserveRPC is invoked to record metrics on incoming RPC messages. 26 func (o *GossipSubControlMessageMetrics) ObserveRPC(from peer.ID, rpc *pubsub.RPC) { 27 lg := o.logger.With().Str("peer_id", from.String()).Logger() 28 includedMessages := len(rpc.GetPublish()) 29 30 ctl := rpc.GetControl() 31 if ctl == nil && includedMessages == 0 { 32 lg.Trace().Msg("received rpc with no control message and no publish messages") 33 return 34 } 35 36 iHaveCount := len(ctl.GetIhave()) 37 iWantCount := len(ctl.GetIwant()) 38 graftCount := len(ctl.GetGraft()) 39 pruneCount := len(ctl.GetPrune()) 40 41 lg.Trace(). 42 Int("iHaveCount", iHaveCount). 43 Int("iWantCount", iWantCount). 44 Int("graftCount", graftCount). 45 Int("pruneCount", pruneCount). 46 Int("included_message_count", includedMessages). 47 Msg("received rpc with control messages") 48 49 o.metrics.OnIHaveReceived(iHaveCount) 50 o.metrics.OnIWantReceived(iWantCount) 51 o.metrics.OnGraftReceived(graftCount) 52 o.metrics.OnPruneReceived(pruneCount) 53 o.metrics.OnPublishedGossipMessagesReceived(includedMessages) 54 }