-
Notifications
You must be signed in to change notification settings - Fork 377
/
conn_logger.go
110 lines (94 loc) · 2.8 KB
/
conn_logger.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
package ipfsutil
import (
"context"
"time"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/zap"
)
var ignoredTags = map[string]bool{
"kbucket": true,
"relay": true,
"relay-hop-stream": true,
}
type connLogger struct {
host host.Host
logger *zap.Logger
}
func EnableConnLogger(ctx context.Context, logger *zap.Logger, h host.Host) {
notifee := &connLogger{
host: h,
logger: logger.Named("conn_logger"),
}
h.Network().Notify(notifee)
go func() {
<-ctx.Done()
h.Network().StopNotify(notifee)
}()
}
func (cl *connLogger) getPeerTags(p peer.ID) []string {
if tagInfo := cl.host.ConnManager().GetTagInfo(p); tagInfo != nil {
var tags []string
for tag := range tagInfo.Tags {
if !ignoredTags[tag] {
tags = append(tags, tag)
}
}
if len(tags) > 0 {
return tags
}
}
return nil
}
func (cl *connLogger) Listen(n network.Network, m ma.Multiaddr) {
cl.logger.Debug("Listener opened", zap.String("Multiaddr", m.String()))
}
func (cl *connLogger) ListenClose(n network.Network, m ma.Multiaddr) {
cl.logger.Debug("Listener closed", zap.String("Multiaddr", m.String()))
}
func (cl *connLogger) Connected(net network.Network, c network.Conn) {
// Wait 10 ms until the peer has been tagged by orbit-db
go func() {
<-time.After(10 * time.Millisecond)
if tags := cl.getPeerTags(c.RemotePeer()); tags != nil {
cl.logger.Info("Connected",
zap.String("peer", c.RemotePeer().Pretty()),
zap.String("to", c.LocalMultiaddr().String()),
zap.String("from", c.RemoteMultiaddr().String()),
zap.Strings("tags", tags),
)
}
}()
}
func (cl *connLogger) Disconnected(n network.Network, c network.Conn) {
if tags := cl.getPeerTags(c.RemotePeer()); tags != nil {
cl.logger.Info("Disconnected",
zap.String("peer", c.RemotePeer().Pretty()),
zap.String("to", c.LocalMultiaddr().String()),
zap.String("from", c.RemoteMultiaddr().String()),
zap.Strings("tags", tags),
)
}
}
func (cl *connLogger) OpenedStream(n network.Network, s network.Stream) {
if tags := cl.getPeerTags(s.Conn().RemotePeer()); tags != nil {
cl.logger.Debug("Stream opened",
zap.String("peer", s.Conn().RemotePeer().Pretty()),
zap.String("to", s.Conn().LocalMultiaddr().String()),
zap.String("from", s.Conn().RemoteMultiaddr().String()),
zap.Strings("tags", tags),
)
}
}
func (cl *connLogger) ClosedStream(n network.Network, s network.Stream) {
if tags := cl.getPeerTags(s.Conn().RemotePeer()); tags != nil {
cl.logger.Debug("Stream closed",
zap.String("peer", s.Conn().RemotePeer().Pretty()),
zap.String("to", s.Conn().LocalMultiaddr().String()),
zap.String("from", s.Conn().RemoteMultiaddr().String()),
zap.Strings("tags", tags),
)
}
}