Skip to content

Commit

Permalink
feat: expose rcmgr metrics when enabled (#8785)
Browse files Browse the repository at this point in the history
* add metrics for the resource manager
* export protocol and service name in Prometheus metrics
* fix: expose rcmgr metrics only when enabled

Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
marten-seemann and lidel authored Apr 5, 2022
1 parent bfc52ab commit 41db58d
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 5 deletions.
1 change: 0 additions & 1 deletion core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func LibP2P(bcfg *BuildCfg, cfg *config.Config) fx.Option {

// Services (resource management)
fx.Provide(libp2p.ResourceManager(cfg.Swarm.ResourceMgr)),

fx.Provide(libp2p.AddrFilters(cfg.Swarm.AddrFilters)),
fx.Provide(libp2p.AddrsFactory(cfg.Addresses.Announce, cfg.Addresses.AppendAnnounce, cfg.Addresses.NoAnnounce)),
fx.Provide(libp2p.SmuxTransport(cfg.Swarm.Transports)),
Expand Down
234 changes: 233 additions & 1 deletion core/node/libp2p/rcmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

config "github.com/ipfs/go-ipfs/config"
"github.com/ipfs/go-ipfs/repo"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
rcmgr "github.com/libp2p/go-libp2p-resource-manager"

"github.com/prometheus/client_golang/prometheus"
"go.uber.org/fx"
)

Expand Down Expand Up @@ -71,7 +75,8 @@ func ResourceManager(cfg config.ResourceMgr) func(fx.Lifecycle, repo.Repo) (netw

libp2p.SetDefaultServiceLimits(limiter)

var ropts []rcmgr.Option
ropts := []rcmgr.Option{rcmgr.WithMetrics(createRcmgrMetrics())}

if os.Getenv("LIBP2P_DEBUG_RCMGR") != "" {
traceFilePath := filepath.Join(repoPath, NetLimitTraceFilename)
ropts = append(ropts, rcmgr.WithTrace(traceFilePath))
Expand Down Expand Up @@ -373,3 +378,230 @@ func NetSetLimit(mgr network.ResourceManager, scope string, limit config.Resourc
return fmt.Errorf("invalid scope %q", scope)
}
}

func createRcmgrMetrics() rcmgr.MetricsReporter {
const (
direction = "direction"
usesFD = "usesFD"
protocol = "protocol"
service = "service"
)

connAllowed := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_conns_allowed_total",
Help: "allowed connections",
},
[]string{direction, usesFD},
)
prometheus.MustRegister(connAllowed)

connBlocked := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_conns_blocked_total",
Help: "blocked connections",
},
[]string{direction, usesFD},
)
prometheus.MustRegister(connBlocked)

streamAllowed := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_streams_allowed_total",
Help: "allowed streams",
},
[]string{direction},
)
prometheus.MustRegister(streamAllowed)

streamBlocked := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_streams_blocked_total",
Help: "blocked streams",
},
[]string{direction},
)
prometheus.MustRegister(streamBlocked)

peerAllowed := prometheus.NewCounter(prometheus.CounterOpts{
Name: "libp2p_rcmgr_peers_allowed_total",
Help: "allowed peers",
})
prometheus.MustRegister(peerAllowed)

peerBlocked := prometheus.NewCounter(prometheus.CounterOpts{
Name: "libp2p_rcmgr_peer_blocked_total",
Help: "blocked peers",
})
prometheus.MustRegister(peerBlocked)

protocolAllowed := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_protocols_allowed_total",
Help: "allowed streams attached to a protocol",
},
[]string{protocol},
)
prometheus.MustRegister(protocolAllowed)

protocolBlocked := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_protocols_blocked_total",
Help: "blocked streams attached to a protocol",
},
[]string{protocol},
)
prometheus.MustRegister(protocolBlocked)

protocolPeerBlocked := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_protocols_for_peer_blocked_total",
Help: "blocked streams attached to a protocol for a specific peer",
},
[]string{protocol},
)
prometheus.MustRegister(protocolPeerBlocked)

serviceAllowed := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_services_allowed_total",
Help: "allowed streams attached to a service",
},
[]string{service},
)
prometheus.MustRegister(serviceAllowed)

serviceBlocked := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_services_blocked_total",
Help: "blocked streams attached to a service",
},
[]string{service},
)
prometheus.MustRegister(serviceBlocked)

servicePeerBlocked := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "libp2p_rcmgr_service_for_peer_blocked_total",
Help: "blocked streams attached to a service for a specific peer",
},
[]string{service},
)
prometheus.MustRegister(servicePeerBlocked)

memoryAllowed := prometheus.NewCounter(prometheus.CounterOpts{
Name: "libp2p_rcmgr_memory_allocations_allowed_total",
Help: "allowed memory allocations",
})
prometheus.MustRegister(memoryAllowed)

memoryBlocked := prometheus.NewCounter(prometheus.CounterOpts{
Name: "libp2p_rcmgr_memory_allocations_blocked_total",
Help: "blocked memory allocations",
})
prometheus.MustRegister(memoryBlocked)

return rcmgrMetrics{
connAllowed,
connBlocked,
streamAllowed,
streamBlocked,
peerAllowed,
peerBlocked,
protocolAllowed,
protocolBlocked,
protocolPeerBlocked,
serviceAllowed,
serviceBlocked,
servicePeerBlocked,
memoryAllowed,
memoryBlocked,
}
}

// Failsafe to ensure interface from go-libp2p-resource-manager is implemented
var _ rcmgr.MetricsReporter = rcmgrMetrics{}

type rcmgrMetrics struct {
connAllowed *prometheus.CounterVec
connBlocked *prometheus.CounterVec
streamAllowed *prometheus.CounterVec
streamBlocked *prometheus.CounterVec
peerAllowed prometheus.Counter
peerBlocked prometheus.Counter
protocolAllowed *prometheus.CounterVec
protocolBlocked *prometheus.CounterVec
protocolPeerBlocked *prometheus.CounterVec
serviceAllowed *prometheus.CounterVec
serviceBlocked *prometheus.CounterVec
servicePeerBlocked *prometheus.CounterVec
memoryAllowed prometheus.Counter
memoryBlocked prometheus.Counter
}

func getDirection(d network.Direction) string {
switch d {
default:
return ""
case network.DirInbound:
return "inbound"
case network.DirOutbound:
return "outbound"
}
}

func (r rcmgrMetrics) AllowConn(dir network.Direction, usefd bool) {
r.connAllowed.WithLabelValues(getDirection(dir), strconv.FormatBool(usefd)).Inc()
}

func (r rcmgrMetrics) BlockConn(dir network.Direction, usefd bool) {
r.connBlocked.WithLabelValues(getDirection(dir), strconv.FormatBool(usefd)).Inc()
}

func (r rcmgrMetrics) AllowStream(_ peer.ID, dir network.Direction) {
r.streamAllowed.WithLabelValues(getDirection(dir)).Inc()
}

func (r rcmgrMetrics) BlockStream(_ peer.ID, dir network.Direction) {
r.streamBlocked.WithLabelValues(getDirection(dir)).Inc()
}

func (r rcmgrMetrics) AllowPeer(_ peer.ID) {
r.peerAllowed.Inc()
}

func (r rcmgrMetrics) BlockPeer(_ peer.ID) {
r.peerBlocked.Inc()
}

func (r rcmgrMetrics) AllowProtocol(proto protocol.ID) {
r.protocolAllowed.WithLabelValues(string(proto)).Inc()
}

func (r rcmgrMetrics) BlockProtocol(proto protocol.ID) {
r.protocolBlocked.WithLabelValues(string(proto)).Inc()
}

func (r rcmgrMetrics) BlockProtocolPeer(proto protocol.ID, _ peer.ID) {
r.protocolPeerBlocked.WithLabelValues(string(proto)).Inc()
}

func (r rcmgrMetrics) AllowService(svc string) {
r.serviceAllowed.WithLabelValues(svc).Inc()
}

func (r rcmgrMetrics) BlockService(svc string) {
r.serviceBlocked.WithLabelValues(svc).Inc()
}

func (r rcmgrMetrics) BlockServicePeer(svc string, _ peer.ID) {
r.servicePeerBlocked.WithLabelValues(svc).Inc()
}

func (r rcmgrMetrics) AllowMemory(_ int) {
r.memoryAllowed.Inc()
}

func (r rcmgrMetrics) BlockMemory(_ int) {
r.memoryBlocked.Inc()
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ require (
github.com/whyrusleeping/go-sysinfo v0.0.0-20190219211824-4a357d4b90b1
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7
go.opencensus.io v0.23.0
go.uber.org/dig v1.14.0
go.uber.org/fx v1.16.0
go.uber.org/zap v1.21.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.27.0
go.opentelemetry.io/otel v1.2.0
go.opentelemetry.io/otel/exporters/jaeger v1.2.0
Expand All @@ -116,6 +113,9 @@ require (
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.2.0
go.opentelemetry.io/otel/sdk v1.2.0
go.opentelemetry.io/otel/trace v1.2.0
go.uber.org/dig v1.14.0
go.uber.org/fx v1.16.0
go.uber.org/zap v1.21.0
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20211025112917-711f33c9992c
Expand Down

0 comments on commit 41db58d

Please sign in to comment.