github.com/amazechain/amc@v0.1.3/internal/p2p/pubsub_tracer.go (about)

     1  package p2p
     2  
     3  import (
     4  	pubsub "github.com/libp2p/go-libp2p-pubsub"
     5  	"github.com/libp2p/go-libp2p/core/host"
     6  	"github.com/libp2p/go-libp2p/core/peer"
     7  	"github.com/libp2p/go-libp2p/core/protocol"
     8  	"github.com/prometheus/client_golang/prometheus"
     9  )
    10  
    11  var _ = pubsub.RawTracer(gossipTracer{})
    12  
    13  // This tracer is used to implement metrics collection for messages received
    14  // and broadcasted through gossipsub.
    15  type gossipTracer struct {
    16  	host host.Host
    17  }
    18  
    19  // AddPeer .
    20  func (g gossipTracer) AddPeer(p peer.ID, proto protocol.ID) {
    21  	// no-op
    22  }
    23  
    24  // RemovePeer .
    25  func (g gossipTracer) RemovePeer(p peer.ID) {
    26  	// no-op
    27  }
    28  
    29  // Join .
    30  func (g gossipTracer) Join(topic string) {
    31  	pubsubTopicsActive.WithLabelValues(topic).Set(1)
    32  }
    33  
    34  // Leave .
    35  func (g gossipTracer) Leave(topic string) {
    36  	pubsubTopicsActive.WithLabelValues(topic).Set(0)
    37  }
    38  
    39  // Graft .
    40  func (g gossipTracer) Graft(p peer.ID, topic string) {
    41  	pubsubTopicsGraft.WithLabelValues(topic).Inc()
    42  }
    43  
    44  // Prune .
    45  func (g gossipTracer) Prune(p peer.ID, topic string) {
    46  	pubsubTopicsPrune.WithLabelValues(topic).Inc()
    47  }
    48  
    49  // ValidateMessage .
    50  func (g gossipTracer) ValidateMessage(msg *pubsub.Message) {
    51  	pubsubMessageValidate.WithLabelValues(*msg.Topic).Inc()
    52  }
    53  
    54  // DeliverMessage .
    55  func (g gossipTracer) DeliverMessage(msg *pubsub.Message) {
    56  	pubsubMessageDeliver.WithLabelValues(*msg.Topic).Inc()
    57  }
    58  
    59  // RejectMessage .
    60  func (g gossipTracer) RejectMessage(msg *pubsub.Message, reason string) {
    61  	pubsubMessageReject.WithLabelValues(*msg.Topic).Inc()
    62  }
    63  
    64  // DuplicateMessage .
    65  func (g gossipTracer) DuplicateMessage(msg *pubsub.Message) {
    66  	pubsubMessageDuplicate.WithLabelValues(*msg.Topic).Inc()
    67  }
    68  
    69  // UndeliverableMessage .
    70  func (g gossipTracer) UndeliverableMessage(msg *pubsub.Message) {
    71  	pubsubMessageUndeliverable.WithLabelValues(*msg.Topic).Inc()
    72  }
    73  
    74  // ThrottlePeer .
    75  func (g gossipTracer) ThrottlePeer(p peer.ID) {
    76  	agent := agentFromPid(p, g.host.Peerstore())
    77  	pubsubPeerThrottle.WithLabelValues(agent).Inc()
    78  }
    79  
    80  // RecvRPC .
    81  func (g gossipTracer) RecvRPC(rpc *pubsub.RPC) {
    82  	setMetricFromRPC(pubsubRPCSubRecv, pubsubRPCRecv, rpc)
    83  }
    84  
    85  // SendRPC .
    86  func (g gossipTracer) SendRPC(rpc *pubsub.RPC, p peer.ID) {
    87  	setMetricFromRPC(pubsubRPCSubSent, pubsubRPCSent, rpc)
    88  }
    89  
    90  // DropRPC .
    91  func (g gossipTracer) DropRPC(rpc *pubsub.RPC, p peer.ID) {
    92  	setMetricFromRPC(pubsubRPCSubDrop, pubsubRPCDrop, rpc)
    93  }
    94  
    95  func setMetricFromRPC(ctr prometheus.Counter, gauge *prometheus.CounterVec, rpc *pubsub.RPC) {
    96  	ctr.Add(float64(len(rpc.Subscriptions)))
    97  	if rpc.Control != nil {
    98  		gauge.WithLabelValues("graft").Add(float64(len(rpc.Control.Graft)))
    99  		gauge.WithLabelValues("prune").Add(float64(len(rpc.Control.Prune)))
   100  		gauge.WithLabelValues("ihave").Add(float64(len(rpc.Control.Ihave)))
   101  		gauge.WithLabelValues("iwant").Add(float64(len(rpc.Control.Iwant)))
   102  	}
   103  }