metrics: Prometheus endpoint on its own --metrics listener; the pod exposes it inside the cluster only

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-19 10:56:43 +01:00
co-authored by Claude Sonnet 5.5
parent b922808a51
commit c653ae4206
13 changed files with 306 additions and 3 deletions
+18
View File
@@ -4,6 +4,7 @@ import (
"context"
"log/slog"
"sync"
"sync/atomic"
"time"
pubsub "github.com/libp2p/go-libp2p-pubsub"
@@ -38,6 +39,12 @@ type peerGuard struct {
disconnect func(peer.ID)
self peer.ID
// Totals since the start, for the metrics.
dropped atomic.Uint64 // messages dropped for exceeding the rate
invalid atomic.Uint64 // messages rejected as invalid
bans atomic.Uint64 // peers banned
syncRefused atomic.Uint64 // sync requests refused
mu sync.Mutex
peers map[peer.ID]*peerCount
streams map[peer.ID]int // sync streams open per peer
@@ -107,6 +114,7 @@ func (g *peerGuard) admit(p peer.ID) bool {
if c.total <= maxPerWindow {
return true
}
g.dropped.Add(1)
if !c.noted {
c.noted = true
g.log.Warn("peer sends too much; dropping its messages until the minute is over",
@@ -117,6 +125,7 @@ func (g *peerGuard) admit(p peer.ID) bool {
// bad counts an invalid message from p and bans it past the limit.
func (g *peerGuard) bad(p peer.ID) {
g.invalid.Add(1)
g.mu.Lock()
c := g.count(p)
c.bad++
@@ -126,6 +135,7 @@ func (g *peerGuard) bad(p peer.ID) {
return
}
g.bl.Add(p)
g.bans.Add(1)
g.log.Warn("banned a peer that kept sending invalid messages", "peer", p,
"invalid_in_a_minute", maxBadPerWindow, "for", banFor)
if g.disconnect != nil {
@@ -146,6 +156,14 @@ const (
// peers asking too often and peers with too many streams open; otherwise it
// returns the function to call when the stream is done.
func (g *peerGuard) beginSync(p peer.ID) (end func(), ok bool) {
end, ok = g.beginSyncUncounted(p)
if !ok {
g.syncRefused.Add(1)
}
return end, ok
}
func (g *peerGuard) beginSyncUncounted(p peer.ID) (end func(), ok bool) {
if g.banned(p) {
return nil, false
}
+10 -1
View File
@@ -29,6 +29,10 @@ type ipGate struct {
counts map[string]int
banned atomic.Pointer[func(peer.ID) bool]
// Totals since the start, for the metrics.
refusedFull atomic.Uint64 // connections refused: the source address was full
refusedBanned atomic.Uint64 // connections refused: the peer is banned
}
func newIPGate(max int) *ipGate { return &ipGate{max: max, counts: map[string]int{}} }
@@ -72,11 +76,16 @@ func (g *ipGate) full(a ma.Multiaddr) bool {
func (g *ipGate) InterceptPeerDial(peer.ID) bool { return true }
func (g *ipGate) InterceptAddrDial(peer.ID, ma.Multiaddr) bool { return true }
func (g *ipGate) InterceptAccept(cm network.ConnMultiaddrs) bool {
return !g.full(cm.RemoteMultiaddr())
if g.full(cm.RemoteMultiaddr()) {
g.refusedFull.Add(1)
return false
}
return true
}
func (g *ipGate) InterceptSecured(dir network.Direction, p peer.ID, _ network.ConnMultiaddrs) bool {
if dir == network.DirInbound {
if f := g.banned.Load(); f != nil && (*f)(p) {
g.refusedBanned.Add(1)
return false
}
}
+3
View File
@@ -138,6 +138,8 @@ type Node struct {
log *slog.Logger
// guard rate-limits and bans the peers that relay to us.
guard *peerGuard
// gate limits inbound connections per address and refuses banned peers.
gate *ipGate
// gossipIn counts listings that arrived by gossip since the last summary.
gossipIn tally
// recoverAsked counts the peers asked for our own listings (see recoverOwn).
@@ -280,6 +282,7 @@ func New(ctx context.Context, cfg Config) (*Node, error) {
regions: map[string]*regionRun{},
syncing: map[peer.ID]bool{},
boot: boot,
gate: gate,
}
if n.clock == nil {
n.clock = time.Now
+33
View File
@@ -0,0 +1,33 @@
package p2p
// Stats is a snapshot of what the node counts about the network, for the
// metrics endpoint. The totals count from the start of the process.
type Stats struct {
Peers int // connected peers
RegionPeers map[string]int // peers known to follow each region we follow
MessagesDropped uint64 // gossip dropped for exceeding a peer's rate
MessagesInvalid uint64 // gossip rejected as invalid
PeersBanned uint64 // peers banned for sending invalid messages
SyncRefused uint64 // sync requests refused (rate, streams, banned)
ConnectionsFull uint64 // inbound connections refused: source address at its limit
ConnectionsBanned uint64 // inbound connections refused: peer banned
}
// Stats returns the current counters.
func (n *Node) Stats() Stats {
s := Stats{
Peers: n.PeerCount(),
RegionPeers: map[string]int{},
MessagesDropped: n.guard.dropped.Load(),
MessagesInvalid: n.guard.invalid.Load(),
PeersBanned: n.guard.bans.Load(),
SyncRefused: n.guard.syncRefused.Load(),
ConnectionsFull: n.gate.refusedFull.Load(),
ConnectionsBanned: n.gate.refusedBanned.Load(),
}
for _, r := range n.Regions() {
s.RegionPeers[r] = len(n.RegionPeers(r))
}
return s
}
+44
View File
@@ -0,0 +1,44 @@
package p2p
import (
"testing"
"github.com/libp2p/go-libp2p/core/network"
)
func TestStatsCountWhatTheGuardAndTheGateDo(t *testing.T) {
a := startNode(t, nil, "u1x")
b := startNode(t, nil, "u1x")
connect(t, a, b)
eventually(t, "a sees b", func() bool { return a.Stats().Peers == 1 && a.Stats().RegionPeers["u1x"] == 1 })
// Invalid messages and a ban.
for i := 0; i < maxBadPerWindow; i++ {
a.guard.bad(b.host.ID())
}
// A flood: messages over the rate are dropped.
for i := 0; i < maxPerWindow+5; i++ {
a.guard.admit(b.host.ID())
}
// A banned peer's sync request, and a connection from a full address.
a.guard.beginSync(b.host.ID())
a.gate.mu.Lock()
a.gate.counts["203.0.113.7"] = DefaultConnsPerIP
a.gate.mu.Unlock()
a.gate.InterceptAccept(fakeConnAddrs{mustAddr(t, "/ip4/203.0.113.7/tcp/1000")})
a.gate.InterceptSecured(network.DirInbound, b.host.ID(), nil)
s := a.Stats()
if s.MessagesInvalid != maxBadPerWindow || s.PeersBanned != 1 {
t.Errorf("invalid %d, banned %d", s.MessagesInvalid, s.PeersBanned)
}
if s.MessagesDropped != 5 {
t.Errorf("dropped %d, want 5", s.MessagesDropped)
}
if s.SyncRefused != 1 {
t.Errorf("sync refused %d, want 1", s.SyncRefused)
}
if s.ConnectionsFull != 1 || s.ConnectionsBanned != 1 {
t.Errorf("connections refused: full %d, banned %d", s.ConnectionsFull, s.ConnectionsBanned)
}
}