-
Notifications
You must be signed in to change notification settings - Fork 210
/
bandwidth.go
87 lines (72 loc) · 2.95 KB
/
bandwidth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package metrics
import (
"github.com/libp2p/go-libp2p/core/metrics"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
prometheusMetrics "github.com/spacemeshos/go-spacemesh/metrics"
)
const (
incoming = "incoming"
outgoing = "outgoing"
// subsystem shared by all metrics exposed by this package.
subsystem = "p2p"
)
var (
totalIn = prometheusMetrics.NewCounter("total_send", subsystem, "Total bytes sent", nil)
totalOut = prometheusMetrics.NewCounter("total_recv", subsystem, "Total bytes received", nil)
messagesPerProtocol = prometheusMetrics.NewCounter(
"messages_per_protocol",
subsystem,
"Number of messages sent per protocol",
[]string{"protocol", "direction"},
)
trafficPerProtocol = prometheusMetrics.NewCounter(
"traffic_per_protocol",
subsystem,
"Traffic sent per protocol",
[]string{"protocol", "direction"},
)
)
// BandwidthCollector implement metrics.Reporter
// that keeps track of the number of messages sent and received per protocol.
type BandwidthCollector struct{}
// NewBandwidthCollector creates a new BandwidthCollector.
func NewBandwidthCollector() *BandwidthCollector {
return &BandwidthCollector{}
}
// LogSentMessageStream logs the message node sent to the peer.
func (b *BandwidthCollector) LogSentMessageStream(size int64, proto protocol.ID, p peer.ID) {
totalOut.WithLabelValues().Add(float64(size))
trafficPerProtocol.WithLabelValues(string(proto), outgoing).Add(float64(size))
messagesPerProtocol.WithLabelValues(string(proto), outgoing).Inc()
}
// LogRecvMessageStream logs the message that node received from the peer.
func (b *BandwidthCollector) LogRecvMessageStream(size int64, proto protocol.ID, p peer.ID) {
totalIn.WithLabelValues().Add(float64(size))
trafficPerProtocol.WithLabelValues(string(proto), incoming).Add(float64(size))
messagesPerProtocol.WithLabelValues(string(proto), incoming).Inc()
}
// LogSentMessage logs the message sent to the peer.
func (b *BandwidthCollector) LogSentMessage(int64) {}
// LogRecvMessage logs the message received from the peer.
func (b *BandwidthCollector) LogRecvMessage(int64) {}
// GetBandwidthForPeer mock returns the bandwidth for a given peer.
func (b *BandwidthCollector) GetBandwidthForPeer(peer.ID) metrics.Stats {
return metrics.Stats{}
}
// GetBandwidthForProtocol mock returns the bandwidth for a given protocol.
func (b *BandwidthCollector) GetBandwidthForProtocol(protocol.ID) metrics.Stats {
return metrics.Stats{}
}
// GetBandwidthTotals returns mock the total bandwidth used by the node.
func (b *BandwidthCollector) GetBandwidthTotals() metrics.Stats {
return metrics.Stats{}
}
// GetBandwidthByPeer mock returns the bandwidth for a given peer.
func (b *BandwidthCollector) GetBandwidthByPeer() map[peer.ID]metrics.Stats {
return nil
}
// GetBandwidthByProtocol mock returns the bandwidth for a given protocol.
func (b *BandwidthCollector) GetBandwidthByProtocol() map[protocol.ID]metrics.Stats {
return nil
}