github.com/koko1123/flow-go-1@v0.29.6/module/metrics/libp2p_resource_manager.go (about) 1 package metrics 2 3 import ( 4 "strconv" 5 6 "github.com/libp2p/go-libp2p/core/network" 7 "github.com/libp2p/go-libp2p/core/peer" 8 "github.com/libp2p/go-libp2p/core/protocol" 9 rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" 10 "github.com/prometheus/client_golang/prometheus" 11 "github.com/prometheus/client_golang/prometheus/promauto" 12 "github.com/rs/zerolog" 13 14 "github.com/koko1123/flow-go-1/utils/logging" 15 ) 16 17 type LibP2PResourceManagerMetrics struct { 18 logger zerolog.Logger 19 // libp2p resource manager metrics 20 // connections 21 allowConnectionCount *prometheus.CounterVec 22 blockConnectionCount *prometheus.CounterVec 23 // streams 24 allowStreamCount *prometheus.CounterVec 25 blockStreamCount *prometheus.CounterVec 26 // peers 27 allowPeerCount prometheus.Counter 28 blockPeerCount prometheus.Counter 29 // protocol 30 allowProtocolCount prometheus.Counter 31 blockProtocolCount prometheus.Counter 32 blockProtocolPeerCount prometheus.Counter 33 // services 34 allowServiceCount prometheus.Counter 35 blockServiceCount prometheus.Counter 36 blockServicePeerCount prometheus.Counter 37 // memory 38 allowMemoryHistogram prometheus.Histogram 39 blockMemoryHistogram prometheus.Histogram 40 41 prefix string 42 } 43 44 var _ rcmgr.MetricsReporter = (*LibP2PResourceManagerMetrics)(nil) 45 46 func NewLibP2PResourceManagerMetrics(logger zerolog.Logger, prefix string) *LibP2PResourceManagerMetrics { 47 l := &LibP2PResourceManagerMetrics{logger: logger, prefix: prefix} 48 49 l.allowConnectionCount = promauto.NewCounterVec(prometheus.CounterOpts{ 50 Namespace: namespaceNetwork, 51 Subsystem: subsystemLibp2p, 52 Name: l.prefix + "resource_manager_allow_connection_total", 53 Help: "total number of connections allowed by the libp2p resource manager", 54 55 // labels are: "inbound", "outbound" and whether the connection is using a file descriptor 56 }, []string{LabelConnectionDirection, LabelConnectionUseFD}) 57 58 l.blockConnectionCount = promauto.NewCounterVec(prometheus.CounterOpts{ 59 Namespace: namespaceNetwork, 60 Subsystem: subsystemLibp2p, 61 Name: l.prefix + "resource_manager_block_connection_total", 62 Help: "total number of connections blocked by the libp2p resource manager", 63 64 // labels are: "inbound", "outbound" and whether the connection is using a file descriptor 65 }, []string{LabelConnectionDirection, LabelConnectionUseFD}) 66 67 l.allowStreamCount = promauto.NewCounterVec(prometheus.CounterOpts{ 68 Namespace: namespaceNetwork, 69 Subsystem: subsystemLibp2p, 70 Name: l.prefix + "resource_manager_allow_stream_total", 71 Help: "total number of streams allowed by the libp2p resource manager", 72 }, []string{LabelConnectionDirection}) 73 74 l.blockStreamCount = promauto.NewCounterVec(prometheus.CounterOpts{ 75 Namespace: namespaceNetwork, 76 Subsystem: subsystemLibp2p, 77 Name: l.prefix + "resource_manager_block_stream_total", 78 Help: "total number of streams blocked by the libp2p resource manager", 79 }, []string{LabelConnectionDirection}) 80 81 l.allowPeerCount = promauto.NewCounter(prometheus.CounterOpts{ 82 Namespace: namespaceNetwork, 83 Subsystem: subsystemLibp2p, 84 Name: l.prefix + "resource_manager_allow_peer_total", 85 Help: "total number of remote peers allowed by the libp2p resource manager to attach to their relevant incoming/outgoing streams", 86 }) 87 88 // Note: this is a low level metric than blockProtocolPeerCount. 89 // This metric is incremented when a peer is blocked by the libp2p resource manager on attaching as one end of a stream (on any protocol). 90 l.blockPeerCount = promauto.NewCounter(prometheus.CounterOpts{ 91 Namespace: namespaceNetwork, 92 Subsystem: subsystemLibp2p, 93 Name: l.prefix + "resource_manager_block_peer_total", 94 Help: "total number of remote peers blocked by the libp2p resource manager from attaching to their relevant incoming/outgoing streams", 95 }) 96 97 l.allowProtocolCount = promauto.NewCounter(prometheus.CounterOpts{ 98 Namespace: namespaceNetwork, 99 Subsystem: subsystemLibp2p, 100 Name: l.prefix + "resource_manager_allow_protocol_total", 101 Help: "total number of protocols allowed by the libp2p resource manager to attach to their relevant incoming/outgoing streams", 102 }) 103 104 l.blockProtocolCount = promauto.NewCounter(prometheus.CounterOpts{ 105 Namespace: namespaceNetwork, 106 Subsystem: subsystemLibp2p, 107 Name: l.prefix + "resource_manager_block_protocol_total", 108 Help: "total number of protocols blocked by the libp2p resource manager from attaching to their relevant incoming/outgoing streams", 109 }) 110 111 // Note: this is a higher level metric than blockPeerCount and blockProtocolCount. 112 // This metric is incremented when a peer is already attached as one end of a stream but on a different reserved protocol. 113 l.blockProtocolPeerCount = promauto.NewCounter(prometheus.CounterOpts{ 114 Namespace: namespaceNetwork, 115 Subsystem: subsystemLibp2p, 116 Name: l.prefix + "resource_manager_block_protocol_peer_total", 117 Help: "total number of remote peers blocked by the libp2p resource manager from attaching to their relevant incoming/outgoing streams on a specific protocol", 118 }) 119 120 l.allowServiceCount = promauto.NewCounter(prometheus.CounterOpts{ 121 Namespace: namespaceNetwork, 122 Subsystem: subsystemLibp2p, 123 Name: l.prefix + "resource_manager_allow_service_total", 124 Help: "total number of remote services (e.g., ping, relay) allowed by the libp2p resource manager to attach to their relevant incoming/outgoing streams", 125 }) 126 127 l.blockServiceCount = promauto.NewCounter(prometheus.CounterOpts{ 128 Namespace: namespaceNetwork, 129 Subsystem: subsystemLibp2p, 130 Name: l.prefix + "resource_manager_block_service_total", 131 Help: "total number of remote services (e.g., ping, relay) blocked by the libp2p resource manager from attaching to their relevant incoming/outgoing streams", 132 }) 133 134 // Note: this is a higher level metric than blockServiceCount and blockPeerCount. 135 // This metric is incremented when a service is already attached as one end of a stream but on a different reserved protocol. 136 l.blockServicePeerCount = promauto.NewCounter(prometheus.CounterOpts{ 137 Namespace: namespaceNetwork, 138 Subsystem: subsystemLibp2p, 139 Name: l.prefix + "resource_manager_block_service_peer_total", 140 Help: "total number of remote services (e.g., ping, relay) blocked by the libp2p resource manager from attaching to their relevant incoming/outgoing streams on a specific peer", 141 }) 142 143 l.allowMemoryHistogram = promauto.NewHistogram(prometheus.HistogramOpts{ 144 Namespace: namespaceNetwork, 145 Subsystem: subsystemLibp2p, 146 Name: l.prefix + "resource_manager_allowed_memory_bytes", 147 Help: "size of memory allocation requests allowed by the libp2p resource manager", 148 Buckets: []float64{KiB, 10 * KiB, 100 * KiB, 500 * KiB, 1 * MiB, 10 * MiB, 100 * MiB, 500 * MiB, 1 * GiB}, 149 }) 150 151 l.blockMemoryHistogram = promauto.NewHistogram(prometheus.HistogramOpts{ 152 Namespace: namespaceNetwork, 153 Subsystem: subsystemLibp2p, 154 Name: l.prefix + "resource_manager_blocked_memory_bytes", 155 Help: "size of memory allocation requests blocked by the libp2p resource manager", 156 Buckets: []float64{KiB, 10 * KiB, 100 * KiB, 500 * KiB, 1 * MiB, 10 * MiB, 100 * MiB, 500 * MiB, 1 * GiB}, 157 }) 158 159 return l 160 } 161 162 func (l *LibP2PResourceManagerMetrics) AllowConn(dir network.Direction, usefd bool) { 163 l.allowConnectionCount.WithLabelValues(dir.String(), strconv.FormatBool(usefd)).Inc() 164 l.logger.Trace().Str("direction", dir.String()).Bool("use_file_descriptor", usefd).Msg("allowing connection") 165 } 166 167 func (l *LibP2PResourceManagerMetrics) BlockConn(dir network.Direction, usefd bool) { 168 l.blockConnectionCount.WithLabelValues(dir.String(), strconv.FormatBool(usefd)).Inc() 169 l.logger.Debug().Bool(logging.KeySuspicious, true).Str("direction", dir.String()).Bool("using_file_descriptor", usefd).Msg("blocking connection") 170 } 171 172 func (l *LibP2PResourceManagerMetrics) AllowStream(p peer.ID, dir network.Direction) { 173 l.allowStreamCount.WithLabelValues(dir.String()).Inc() 174 l.logger.Trace().Str("peer", p.String()).Str("direction", dir.String()).Msg("allowing stream") 175 } 176 177 func (l *LibP2PResourceManagerMetrics) BlockStream(p peer.ID, dir network.Direction) { 178 l.blockStreamCount.WithLabelValues(dir.String()).Inc() 179 l.logger.Debug().Bool(logging.KeySuspicious, true).Str("peer", p.String()).Str("direction", dir.String()).Msg("blocking stream") 180 } 181 182 func (l *LibP2PResourceManagerMetrics) AllowPeer(p peer.ID) { 183 l.allowPeerCount.Inc() 184 l.logger.Trace().Str("peer", p.String()).Msg("allowing peer") 185 } 186 187 func (l *LibP2PResourceManagerMetrics) BlockPeer(p peer.ID) { 188 l.blockPeerCount.Inc() 189 l.logger.Debug().Bool(logging.KeySuspicious, true).Str("peer", p.String()).Msg("blocking peer") 190 } 191 192 func (l *LibP2PResourceManagerMetrics) AllowProtocol(proto protocol.ID) { 193 l.allowProtocolCount.Inc() 194 l.logger.Trace().Str("protocol", string(proto)).Msg("allowing protocol") 195 } 196 197 func (l *LibP2PResourceManagerMetrics) BlockProtocol(proto protocol.ID) { 198 l.blockProtocolCount.Inc() 199 l.logger.Debug().Bool(logging.KeySuspicious, true).Str("protocol", string(proto)).Msg("blocking protocol") 200 } 201 202 func (l *LibP2PResourceManagerMetrics) BlockProtocolPeer(proto protocol.ID, p peer.ID) { 203 l.blockProtocolPeerCount.Inc() 204 l.logger.Debug().Bool(logging.KeySuspicious, true).Str("protocol", string(proto)).Str("peer", p.String()).Msg("blocking protocol for peer") 205 } 206 207 func (l *LibP2PResourceManagerMetrics) AllowService(svc string) { 208 l.allowServiceCount.Inc() 209 l.logger.Trace().Str("service", svc).Msg("allowing service") 210 } 211 212 func (l *LibP2PResourceManagerMetrics) BlockService(svc string) { 213 l.blockServiceCount.Inc() 214 l.logger.Debug().Bool(logging.KeySuspicious, true).Str("service", svc).Msg("blocking service") 215 } 216 217 func (l *LibP2PResourceManagerMetrics) BlockServicePeer(svc string, p peer.ID) { 218 l.blockServicePeerCount.Inc() 219 l.logger.Debug().Bool(logging.KeySuspicious, true).Str("service", svc).Str("peer", p.String()).Msg("blocking service for peer") 220 } 221 222 func (l *LibP2PResourceManagerMetrics) AllowMemory(size int) { 223 l.allowMemoryHistogram.Observe(float64(size)) 224 l.logger.Trace().Int("size", size).Msg("allowing memory") 225 } 226 227 func (l *LibP2PResourceManagerMetrics) BlockMemory(size int) { 228 l.blockMemoryHistogram.Observe(float64(size)) 229 l.logger.Debug().Bool(logging.KeySuspicious, true).Int("size", size).Msg("blocking memory") 230 }