// Package metrics provides metrics collection and reporting interfaces for libp2p.
package metrics import ( ) // BandwidthCounter tracks incoming and outgoing data transferred by the local peer. // Metrics are available for total bandwidth across all peers / protocols, as well // as segmented by remote peer ID and protocol ID. type BandwidthCounter struct { totalIn flow.Meter totalOut flow.Meter protocolIn flow.MeterRegistry protocolOut flow.MeterRegistry peerIn flow.MeterRegistry peerOut flow.MeterRegistry } // NewBandwidthCounter creates a new BandwidthCounter. func () *BandwidthCounter { return new(BandwidthCounter) } // LogSentMessage records the size of an outgoing message // without associating the bandwidth to a specific peer or protocol. func ( *BandwidthCounter) ( int64) { .totalOut.Mark(uint64()) } // LogRecvMessage records the size of an incoming message // without associating the bandwidth to a specific peer or protocol. func ( *BandwidthCounter) ( int64) { .totalIn.Mark(uint64()) } // LogSentMessageStream records the size of an outgoing message over a single logical stream. // Bandwidth is associated with the given protocol.ID and peer.ID. func ( *BandwidthCounter) ( int64, protocol.ID, peer.ID) { .protocolOut.Get(string()).Mark(uint64()) .peerOut.Get(string()).Mark(uint64()) } // LogRecvMessageStream records the size of an incoming message over a single logical stream. // Bandwidth is associated with the given protocol.ID and peer.ID. func ( *BandwidthCounter) ( int64, protocol.ID, peer.ID) { .protocolIn.Get(string()).Mark(uint64()) .peerIn.Get(string()).Mark(uint64()) } // GetBandwidthForPeer returns a Stats struct with bandwidth metrics associated with the given peer.ID. // The metrics returned include all traffic sent / received for the peer, regardless of protocol. func ( *BandwidthCounter) ( peer.ID) ( Stats) { := .peerIn.Get(string()).Snapshot() := .peerOut.Get(string()).Snapshot() return Stats{ TotalIn: int64(.Total), TotalOut: int64(.Total), RateIn: .Rate, RateOut: .Rate, } } // GetBandwidthForProtocol returns a Stats struct with bandwidth metrics associated with the given protocol.ID. // The metrics returned include all traffic sent / received for the protocol, regardless of which peers were // involved. func ( *BandwidthCounter) ( protocol.ID) ( Stats) { := .protocolIn.Get(string()).Snapshot() := .protocolOut.Get(string()).Snapshot() return Stats{ TotalIn: int64(.Total), TotalOut: int64(.Total), RateIn: .Rate, RateOut: .Rate, } } // GetBandwidthTotals returns a Stats struct with bandwidth metrics for all data sent / received by the // local peer, regardless of protocol or remote peer IDs. func ( *BandwidthCounter) () ( Stats) { := .totalIn.Snapshot() := .totalOut.Snapshot() return Stats{ TotalIn: int64(.Total), TotalOut: int64(.Total), RateIn: .Rate, RateOut: .Rate, } } // GetBandwidthByPeer returns a map of all remembered peers and the bandwidth // metrics with respect to each. This method may be very expensive. func ( *BandwidthCounter) () map[peer.ID]Stats { := make(map[peer.ID]Stats) .peerIn.ForEach(func( string, *flow.Meter) { := peer.ID() := .Snapshot() := [] .TotalIn = int64(.Total) .RateIn = .Rate [] = }) .peerOut.ForEach(func( string, *flow.Meter) { := peer.ID() := .Snapshot() := [] .TotalOut = int64(.Total) .RateOut = .Rate [] = }) return } // GetBandwidthByProtocol returns a map of all remembered protocols and // the bandwidth metrics with respect to each. This method may be moderately // expensive. func ( *BandwidthCounter) () map[protocol.ID]Stats { := make(map[protocol.ID]Stats) .protocolIn.ForEach(func( string, *flow.Meter) { := protocol.ID() := .Snapshot() := [] .TotalIn = int64(.Total) .RateIn = .Rate [] = }) .protocolOut.ForEach(func( string, *flow.Meter) { := protocol.ID() := .Snapshot() := [] .TotalOut = int64(.Total) .RateOut = .Rate [] = }) return } // Reset clears all stats. func ( *BandwidthCounter) () { .totalIn.Reset() .totalOut.Reset() .protocolIn.Clear() .protocolOut.Clear() .peerIn.Clear() .peerOut.Clear() } // TrimIdle trims all timers idle since the given time. func ( *BandwidthCounter) ( time.Time) { .peerIn.TrimIdle() .peerOut.TrimIdle() .protocolIn.TrimIdle() .protocolOut.TrimIdle() }