Skip to content

Commit 01b9825

Browse files
authored
Merge pull request #227 from libp2p/feat/tracing
tracing support
2 parents 28a87b3 + 7065297 commit 01b9825

File tree

9 files changed

+7104
-8
lines changed

9 files changed

+7104
-8
lines changed

floodsub.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func NewFloodSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, err
3131
type FloodSubRouter struct {
3232
p *PubSub
3333
protocols []protocol.ID
34+
tracer *pubsubTracer
3435
}
3536

3637
func (fs *FloodSubRouter) Protocols() []protocol.ID {
@@ -39,11 +40,16 @@ func (fs *FloodSubRouter) Protocols() []protocol.ID {
3940

4041
func (fs *FloodSubRouter) Attach(p *PubSub) {
4142
fs.p = p
43+
fs.tracer = p.tracer
4244
}
4345

44-
func (fs *FloodSubRouter) AddPeer(peer.ID, protocol.ID) {}
46+
func (fs *FloodSubRouter) AddPeer(p peer.ID, proto protocol.ID) {
47+
fs.tracer.AddPeer(p, proto)
48+
}
4549

46-
func (fs *FloodSubRouter) RemovePeer(peer.ID) {}
50+
func (fs *FloodSubRouter) RemovePeer(p peer.ID) {
51+
fs.tracer.RemovePeer(p)
52+
}
4753

4854
func (fs *FloodSubRouter) EnoughPeers(topic string, suggested int) bool {
4955
// check all peers in the topic
@@ -91,13 +97,19 @@ func (fs *FloodSubRouter) Publish(from peer.ID, msg *pb.Message) {
9197

9298
select {
9399
case mch <- out:
100+
fs.tracer.SendRPC(out, pid)
94101
default:
95102
log.Infof("dropping message to peer %s: queue full", pid)
103+
fs.tracer.DropRPC(out, pid)
96104
// Drop it. The peer is too slow.
97105
}
98106
}
99107
}
100108

101-
func (fs *FloodSubRouter) Join(topic string) {}
109+
func (fs *FloodSubRouter) Join(topic string) {
110+
fs.tracer.Join(topic)
111+
}
102112

103-
func (fs *FloodSubRouter) Leave(topic string) {}
113+
func (fs *FloodSubRouter) Leave(topic string) {
114+
fs.tracer.Join(topic)
115+
}

gossipsub.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type GossipSubRouter struct {
6565
gossip map[peer.ID][]*pb.ControlIHave // pending gossip
6666
control map[peer.ID]*pb.ControlMessage // pending control messages
6767
mcache *MessageCache
68+
tracer *pubsubTracer
6869
}
6970

7071
func (gs *GossipSubRouter) Protocols() []protocol.ID {
@@ -73,16 +74,19 @@ func (gs *GossipSubRouter) Protocols() []protocol.ID {
7374

7475
func (gs *GossipSubRouter) Attach(p *PubSub) {
7576
gs.p = p
77+
gs.tracer = p.tracer
7678
go gs.heartbeatTimer()
7779
}
7880

7981
func (gs *GossipSubRouter) AddPeer(p peer.ID, proto protocol.ID) {
8082
log.Debugf("PEERUP: Add new peer %s using %s", p, proto)
83+
gs.tracer.AddPeer(p, proto)
8184
gs.peers[p] = proto
8285
}
8386

8487
func (gs *GossipSubRouter) RemovePeer(p peer.ID) {
8588
log.Debugf("PEERDOWN: Remove disconnected peer %s", p)
89+
gs.tracer.RemovePeer(p)
8690
delete(gs.peers, p)
8791
for _, peers := range gs.mesh {
8892
delete(peers, p)
@@ -208,6 +212,7 @@ func (gs *GossipSubRouter) handleGraft(p peer.ID, ctl *pb.ControlMessage) []*pb.
208212
prune = append(prune, topic)
209213
} else {
210214
log.Debugf("GRAFT: Add mesh link from %s in %s", p, topic)
215+
gs.tracer.Graft(p, topic)
211216
peers[p] = struct{}{}
212217
gs.tagPeer(p, topic)
213218
}
@@ -231,6 +236,7 @@ func (gs *GossipSubRouter) handlePrune(p peer.ID, ctl *pb.ControlMessage) {
231236
peers, ok := gs.mesh[topic]
232237
if ok {
233238
log.Debugf("PRUNE: Remove mesh link to %s in %s", p, topic)
239+
gs.tracer.Prune(p, topic)
234240
delete(peers, p)
235241
gs.untagPeer(p, topic)
236242
}
@@ -294,6 +300,7 @@ func (gs *GossipSubRouter) Join(topic string) {
294300
}
295301

296302
log.Debugf("JOIN %s", topic)
303+
gs.tracer.Join(topic)
297304

298305
gmap, ok = gs.fanout[topic]
299306
if ok {
@@ -319,6 +326,7 @@ func (gs *GossipSubRouter) Join(topic string) {
319326

320327
for p := range gmap {
321328
log.Debugf("JOIN: Add mesh link to %s in %s", p, topic)
329+
gs.tracer.Graft(p, topic)
322330
gs.sendGraft(p, topic)
323331
gs.tagPeer(p, topic)
324332
}
@@ -331,11 +339,13 @@ func (gs *GossipSubRouter) Leave(topic string) {
331339
}
332340

333341
log.Debugf("LEAVE %s", topic)
342+
gs.tracer.Leave(topic)
334343

335344
delete(gs.mesh, topic)
336345

337346
for p := range gmap {
338347
log.Debugf("LEAVE: Remove mesh link to %s in %s", p, topic)
348+
gs.tracer.Prune(p, topic)
339349
gs.sendPrune(p, topic)
340350
gs.untagPeer(p, topic)
341351
}
@@ -384,8 +394,10 @@ func (gs *GossipSubRouter) sendRPC(p peer.ID, out *RPC) {
384394

385395
select {
386396
case mch <- out:
397+
gs.tracer.SendRPC(out, p)
387398
default:
388399
log.Infof("dropping message to peer %s: queue full", p)
400+
gs.tracer.DropRPC(out, p)
389401
// push control messages that need to be retried
390402
ctl := out.GetControl()
391403
if ctl != nil {
@@ -443,6 +455,7 @@ func (gs *GossipSubRouter) heartbeat() {
443455

444456
for _, p := range plst {
445457
log.Debugf("HEARTBEAT: Add mesh link to %s in %s", p, topic)
458+
gs.tracer.Graft(p, topic)
446459
peers[p] = struct{}{}
447460
gs.tagPeer(p, topic)
448461
topics := tograft[p]
@@ -458,6 +471,7 @@ func (gs *GossipSubRouter) heartbeat() {
458471

459472
for _, p := range plst[:idontneed] {
460473
log.Debugf("HEARTBEAT: Remove mesh link to %s in %s", p, topic)
474+
gs.tracer.Prune(p, topic)
461475
delete(peers, p)
462476
gs.untagPeer(p, topic)
463477
topics := toprune[p]

0 commit comments

Comments
 (0)