-
Notifications
You must be signed in to change notification settings - Fork 211
/
gossipsub.go
190 lines (166 loc) · 5.45 KB
/
gossipsub.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package metrics
import (
"sync"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/prometheus/client_golang/prometheus"
"github.com/spacemeshos/go-spacemesh/metrics"
)
var (
totalPeers = metrics.NewGauge("total_peers", subsystem, "Total number of peers", nil)
peersPerProtocol = metrics.NewGauge(
"peers_per_protocol",
subsystem,
"Number of peers per protocol",
[]string{"protocol"},
)
)
var (
// ProcessedMessagesDuration in nanoseconds to process a message. Labeled by protocol and result.
ProcessedMessagesDuration = metrics.NewHistogramWithBuckets(
"processed_messages_duration",
subsystem,
"Duration in nanoseconds to process a message",
[]string{"protocol", "result"},
prometheus.ExponentialBuckets(1_000_000, 4, 10),
)
deliveredMessagesBytes = metrics.NewCounter(
"delivered_messages_bytes",
subsystem,
"Total amount of delivered payloads (doesn't count gossipsub metadata)",
[]string{"protocol"},
)
receivedMessagesBytes = metrics.NewCounter(
"received_messages_bytes",
subsystem,
"Total amount of received payloads (doesn't count gossipsub metadata)",
[]string{"protocol"},
)
deliveredMessagesCount = metrics.NewCounter(
"delivered_messages_count",
subsystem,
"Total number of delivered messages",
[]string{"protocol"},
)
receivedMessagesCount = metrics.NewCounter(
"received_messages_count",
subsystem,
"Total amount of received messages",
[]string{"protocol"},
)
queueFullCount = metrics.NewCounter(
"queue_full",
subsystem,
"counter of dropped messages because queue was full",
[]string{"protocol"},
)
throttledCount = metrics.NewCounter(
"throttled",
subsystem,
"counter of dropped messages because of throttling",
[]string{"protocol"},
)
rejectedCount = metrics.NewCounter(
"rejected",
subsystem,
"counter of dropped messages for any other reason",
[]string{"protocol"},
)
)
// GossipCollector pubsub.RawTracer implementation
// total number of peers
// number of peers per each gossip protocol.
type GossipCollector struct {
peers struct {
sync.Mutex
m map[peer.ID]protocol.ID
}
}
// NewGoSIPCollector creates a new GossipCollector.
func NewGoSIPCollector() *GossipCollector {
return &GossipCollector{
peers: struct {
sync.Mutex
m map[peer.ID]protocol.ID
}{
m: make(map[peer.ID]protocol.ID),
},
}
}
// AddPeer is invoked when a new peer is added.
func (g *GossipCollector) AddPeer(id peer.ID, proto protocol.ID) {
g.peers.Lock()
g.peers.m[id] = proto
g.peers.Unlock()
peersPerProtocol.WithLabelValues(string(proto)).Inc()
totalPeers.WithLabelValues().Inc()
}
// RemovePeer is invoked when a peer is removed.
func (g *GossipCollector) RemovePeer(id peer.ID) {
g.peers.Lock()
proto := g.peers.m[id]
delete(g.peers.m, id)
g.peers.Unlock()
peersPerProtocol.WithLabelValues(string(proto)).Dec()
totalPeers.WithLabelValues().Dec()
}
// Join is invoked when a new topic is joined.
func (g *GossipCollector) Join(string) {}
// Leave is invoked when a topic is abandoned.
func (g *GossipCollector) Leave(string) {}
// Graft is invoked when a new peer is grafted on the mesh (gossipsub).
func (g *GossipCollector) Graft(peer.ID, string) {}
// Prune is invoked when a peer is pruned from the message (gossipsub).
func (g *GossipCollector) Prune(peer.ID, string) {}
// ValidateMessage is invoked when a message first enters the validation pipeline.
func (g *GossipCollector) ValidateMessage(msg *pubsub.Message) {
if msg.Topic == nil {
return
}
receivedMessagesBytes.WithLabelValues(*msg.Topic).Add(float64(len(msg.Data)))
receivedMessagesCount.WithLabelValues(*msg.Topic).Inc()
}
// DeliverMessage is invoked when a message is delivered.
func (g *GossipCollector) DeliverMessage(msg *pubsub.Message) {
if msg.Topic == nil {
return
}
deliveredMessagesBytes.WithLabelValues(*msg.Topic).Add(float64(len(msg.Data)))
deliveredMessagesCount.WithLabelValues(*msg.Topic).Inc()
}
// RejectMessage is invoked when a message is Rejected or Ignored.
// The reason argument can be one of the named strings Reject*.
func (g *GossipCollector) RejectMessage(msg *pubsub.Message, reason string) {
topic := ""
if msg.Topic != nil {
topic = *msg.Topic
}
switch reason {
case pubsub.RejectValidationThrottled:
throttledCount.WithLabelValues(topic).Inc()
case pubsub.RejectValidationQueueFull:
queueFullCount.WithLabelValues(topic).Inc()
default:
rejectedCount.WithLabelValues(topic).Inc()
}
}
// DuplicateMessage is invoked when a duplicate message is dropped.
func (g *GossipCollector) DuplicateMessage(msg *pubsub.Message) {
if msg.Topic == nil {
return
}
receivedMessagesBytes.WithLabelValues(*msg.Topic).Add(float64(len(msg.Data)))
receivedMessagesCount.WithLabelValues(*msg.Topic).Inc()
}
// ThrottlePeer is invoked when a peer is throttled by the peer gater.
func (g *GossipCollector) ThrottlePeer(peer.ID) {}
// RecvRPC is invoked when an incoming RPC is received.
func (g *GossipCollector) RecvRPC(*pubsub.RPC) {}
// SendRPC is invoked when a RPC is sent.
func (g *GossipCollector) SendRPC(*pubsub.RPC, peer.ID) {}
// DropRPC is invoked when an outbound RPC is dropped, typically because of a queue full.
func (g *GossipCollector) DropRPC(*pubsub.RPC, peer.ID) {}
// UndeliverableMessage is invoked when the consumer of Subscribe is not reading messages fast enough and
// the pressure release mechanism trigger, dropping messages.
func (g *GossipCollector) UndeliverableMessage(*pubsub.Message) {}