-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathpeerlog.go
189 lines (158 loc) · 4.42 KB
/
peerlog.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
package peerlog
import (
"fmt"
"sync/atomic"
"time"
core "github.com/ipfs/go-ipfs/core"
plugin "github.com/ipfs/go-ipfs/plugin"
logging "github.com/ipfs/go-log"
event "github.com/libp2p/go-libp2p-core/event"
network "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"go.uber.org/zap"
)
var log = logging.Logger("plugin/peerlog")
type eventType int
var (
// size of the event queue buffer
eventQueueSize = 64 * 1024
// number of events to drop when busy.
busyDropAmount = eventQueueSize / 8
)
const (
eventConnect eventType = iota
eventIdentify
)
type plEvent struct {
kind eventType
peer peer.ID
}
// Log all the PeerIDs we see
//
// Usage:
// GOLOG_FILE=~/peer.log IPFS_LOGGING_FMT=json ipfs daemon
// Output:
// {"level":"info","ts":"2020-02-10T13:54:26.639Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:51","msg":"connected","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt"}
// {"level":"info","ts":"2020-02-10T13:54:59.095Z","logger":"plugin/peerlog","caller":"peerlog/peerlog.go:56","msg":"identified","peer":"QmS2H72gdrekXJggGdE9SunXPntBqdkJdkXQJjuxcH8Cbt","agent":"go-ipfs/0.5.0/"}
//
type peerLogPlugin struct {
droppedCount uint64
events chan plEvent
}
var _ plugin.PluginDaemonInternal = (*peerLogPlugin)(nil)
// Plugins is exported list of plugins that will be loaded
var Plugins = []plugin.Plugin{
&peerLogPlugin{},
}
// Name returns the plugin's name, satisfying the plugin.Plugin interface.
func (*peerLogPlugin) Name() string {
return "peerlog"
}
// Version returns the plugin's version, satisfying the plugin.Plugin interface.
func (*peerLogPlugin) Version() string {
return "0.1.0"
}
// Init initializes plugin
func (pl *peerLogPlugin) Init(*plugin.Environment) error {
pl.events = make(chan plEvent, eventQueueSize)
return nil
}
func (pl *peerLogPlugin) collectEvents(node *core.IpfsNode) {
ctx := node.Context()
busyCounter := 0
dlog := log.Desugar()
for {
// Deal with dropped events.
dropped := atomic.SwapUint64(&pl.droppedCount, 0)
if dropped > 0 {
busyCounter++
// sleep a bit to give the system a chance to catch up with logging.
select {
case <-time.After(time.Duration(busyCounter) * time.Second):
case <-ctx.Done():
return
}
// drain 1/8th of the backlog backlog so we
// don't immediately run into this situation
// again.
loop:
for i := 0; i < busyDropAmount; i++ {
select {
case <-pl.events:
dropped++
default:
break loop
}
}
// Add in any events we've dropped in the mean-time.
dropped += atomic.SwapUint64(&pl.droppedCount, 0)
// Report that we've dropped events.
dlog.Error("dropped events", zap.Uint64("count", dropped))
} else {
busyCounter = 0
}
var e plEvent
select {
case <-ctx.Done():
return
case e = <-pl.events:
}
peerID := zap.String("peer", e.peer.Pretty())
switch e.kind {
case eventConnect:
dlog.Info("connected", peerID)
case eventIdentify:
agent, err := node.Peerstore.Get(e.peer, "AgentVersion")
switch err {
case nil:
case peerstore.ErrNotFound:
continue
default:
dlog.Error("failed to get agent version", zap.Error(err))
continue
}
agentS, ok := agent.(string)
if !ok {
continue
}
dlog.Info("identified", peerID, zap.String("agent", agentS))
}
}
}
func (pl *peerLogPlugin) emit(evt eventType, p peer.ID) {
select {
case pl.events <- plEvent{kind: evt, peer: p}:
default:
atomic.AddUint64(&pl.droppedCount, 1)
}
}
func (pl *peerLogPlugin) Start(node *core.IpfsNode) error {
// Ensure logs from this plugin get printed regardless of global IPFS_LOGGING value
if err := logging.SetLogLevel("plugin/peerlog", "info"); err != nil {
return fmt.Errorf("failed to set log level: %w", err)
}
sub, err := node.PeerHost.EventBus().Subscribe(new(event.EvtPeerIdentificationCompleted))
if err != nil {
return fmt.Errorf("failed to subscribe to identify notifications")
}
var notifee network.NotifyBundle
notifee.ConnectedF = func(net network.Network, conn network.Conn) {
pl.emit(eventConnect, conn.RemotePeer())
}
node.PeerHost.Network().Notify(¬ifee)
go func() {
defer sub.Close()
for e := range sub.Out() {
switch e := e.(type) {
case event.EvtPeerIdentificationCompleted:
pl.emit(eventIdentify, e.Peer)
}
}
}()
go pl.collectEvents(node)
return nil
}
func (*peerLogPlugin) Close() error {
return nil
}