Skip to content

Commit c2e77cf

Browse files
authored
Add canonical peer status logging with sampling (#269)
* Add canonical peer status log * Use %q formatting * Use underscore for consistency
1 parent 4c4f229 commit c2e77cf

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

core/canonicallog/canonicallog.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package canonicallog
22

33
import (
4+
"fmt"
5+
"math/rand"
46
"net"
7+
"strings"
58

69
logging "github.com/ipfs/go-log/v2"
710
"github.com/libp2p/go-libp2p-core/peer"
@@ -15,7 +18,7 @@ var log = logging.WithSkip(logging.Logger("canonical-log"), 1)
1518
// Protocols should use this to identify a misbehaving peer to allow the end
1619
// user to easily identify these nodes across protocols and libp2p.
1720
func LogMisbehavingPeer(p peer.ID, peerAddr multiaddr.Multiaddr, component string, err error, msg string) {
18-
log.Warnf("CANONICAL_MISBEHAVING_PEER: peer=%s addr=%s component=%s err=%v msg=%s", p, peerAddr.String(), component, err, msg)
21+
log.Warnf("CANONICAL_MISBEHAVING_PEER: peer=%s addr=%s component=%s err=%q msg=%q", p, peerAddr.String(), component, err, msg)
1922
}
2023

2124
// LogMisbehavingPeerNetAddr is the canonical way to log a misbehaving peer.
@@ -24,9 +27,30 @@ func LogMisbehavingPeer(p peer.ID, peerAddr multiaddr.Multiaddr, component strin
2427
func LogMisbehavingPeerNetAddr(p peer.ID, peerAddr net.Addr, component string, originalErr error, msg string) {
2528
ma, err := manet.FromNetAddr(peerAddr)
2629
if err != nil {
27-
log.Warnf("CANONICAL_MISBEHAVING_PEER: peer=%s netAddr=%s component=%s err=%v msg=%s", p, peerAddr.String(), component, originalErr, msg)
30+
log.Warnf("CANONICAL_MISBEHAVING_PEER: peer=%s net_addr=%s component=%s err=%q msg=%q", p, peerAddr.String(), component, originalErr, msg)
2831
return
2932
}
3033

31-
log.Warnf("CANONICAL_MISBEHAVING_PEER: peer=%s addr=%s component=%s err=%v msg=%s", p, ma, component, originalErr, msg)
34+
LogMisbehavingPeer(p, ma, component, originalErr, msg)
35+
}
36+
37+
// LogPeerStatus logs any useful information about a peer. It takes in a sample
38+
// rate and will only log one in every sampleRate messages (randomly). This is
39+
// useful in surfacing events that are normal in isolation, but may be abnormal
40+
// in large quantities. For example, a successful connection from an IP address
41+
// is normal. 10,000 connections from that same IP address is not normal. libp2p
42+
// itself does nothing besides emitting this log. Hook this up to another tool
43+
// like fail2ban to action on the log.
44+
func LogPeerStatus(sampleRate int, p peer.ID, peerAddr multiaddr.Multiaddr, keyVals ...string) {
45+
if rand.Intn(sampleRate) == 0 {
46+
keyValsStr := strings.Builder{}
47+
for i, kOrV := range keyVals {
48+
if i%2 == 0 {
49+
fmt.Fprintf(&keyValsStr, " %v=", kOrV)
50+
} else {
51+
fmt.Fprintf(&keyValsStr, "%q", kOrV)
52+
}
53+
}
54+
log.Infof("CANONICAL_PEER_STATUS: peer=%s addr=%s sample_rate=%v%s", p, peerAddr.String(), sampleRate, keyValsStr.String())
55+
}
3256
}

core/canonicallog/canonicallog_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ import (
55
"net"
66
"testing"
77

8+
logging "github.com/ipfs/go-log/v2"
89
"github.com/libp2p/go-libp2p-core/test"
910
"github.com/multiformats/go-multiaddr"
1011
)
1112

1213
func TestLogs(t *testing.T) {
14+
err := logging.SetLogLevel("canonical-log", "info")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
1319
LogMisbehavingPeer(test.RandPeerIDFatal(t), multiaddr.StringCast("/ip4/1.2.3.4"), "somecomponent", fmt.Errorf("something"), "hi")
1420

1521
netAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 80}
16-
LogMisbehavingPeerNetAddr(test.RandPeerIDFatal(t), netAddr, "somecomponent", fmt.Errorf("something"), "hi")
22+
LogMisbehavingPeerNetAddr(test.RandPeerIDFatal(t), netAddr, "somecomponent", fmt.Errorf("something"), "hello \"world\"")
23+
24+
LogPeerStatus(1, test.RandPeerIDFatal(t), multiaddr.StringCast("/ip4/1.2.3.4"), "extra", "info")
1725
}

0 commit comments

Comments
 (0)