From cd257504657da93dfb8c3fbd3b96470b464eb0ff Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Wed, 14 Jun 2023 12:17:20 +0300 Subject: [PATCH 1/4] rcmgr: use default libp2p rcmgr metrics --- core/node/libp2p/rcmgr.go | 13 +- core/node/libp2p/rcmgr_metrics.go | 251 ------------------------------ 2 files changed, 7 insertions(+), 257 deletions(-) delete mode 100644 core/node/libp2p/rcmgr_metrics.go diff --git a/core/node/libp2p/rcmgr.go b/core/node/libp2p/rcmgr.go index c11c7262c7d..d5b3db197d1 100644 --- a/core/node/libp2p/rcmgr.go +++ b/core/node/libp2p/rcmgr.go @@ -7,6 +7,10 @@ import ( "os" "path/filepath" + "github.com/ipfs/kubo/config" + "github.com/ipfs/kubo/core/node/helpers" + "github.com/ipfs/kubo/repo" + "github.com/benbjohnson/clock" logging "github.com/ipfs/go-log/v2" "github.com/libp2p/go-libp2p" @@ -15,11 +19,8 @@ import ( "github.com/libp2p/go-libp2p/core/protocol" rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager" "github.com/multiformats/go-multiaddr" + "github.com/prometheus/client_golang/prometheus" "go.uber.org/fx" - - "github.com/ipfs/kubo/config" - "github.com/ipfs/kubo/core/node/helpers" - "github.com/ipfs/kubo/repo" ) var rcmgrLogger = logging.Logger("rcmgr") @@ -73,8 +74,8 @@ filled in with autocomputed defaults.`) if err != nil { return nil, opts, err } - - ropts := []rcmgr.Option{rcmgr.WithMetrics(createRcmgrMetrics()), rcmgr.WithTraceReporter(str)} + rcmgrObs.MustRegisterWith(prometheus.DefaultRegisterer) + ropts := []rcmgr.Option{rcmgr.WithTraceReporter(str)} if len(cfg.ResourceMgr.Allowlist) > 0 { var mas []multiaddr.Multiaddr diff --git a/core/node/libp2p/rcmgr_metrics.go b/core/node/libp2p/rcmgr_metrics.go deleted file mode 100644 index f8b1a7daa3b..00000000000 --- a/core/node/libp2p/rcmgr_metrics.go +++ /dev/null @@ -1,251 +0,0 @@ -package libp2p - -import ( - "errors" - "strconv" - - "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/p2p/host/resource-manager" - - "github.com/prometheus/client_golang/prometheus" -) - -func mustRegister(c prometheus.Collector) { - err := prometheus.Register(c) - are := prometheus.AlreadyRegisteredError{} - if errors.As(err, &are) { - return - } - if err != nil { - panic(err) - } -} - -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}, - ) - mustRegister(connAllowed) - - connBlocked := prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "libp2p_rcmgr_conns_blocked_total", - Help: "blocked connections", - }, - []string{direction, usesFD}, - ) - mustRegister(connBlocked) - - streamAllowed := prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "libp2p_rcmgr_streams_allowed_total", - Help: "allowed streams", - }, - []string{direction}, - ) - mustRegister(streamAllowed) - - streamBlocked := prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "libp2p_rcmgr_streams_blocked_total", - Help: "blocked streams", - }, - []string{direction}, - ) - mustRegister(streamBlocked) - - peerAllowed := prometheus.NewCounter(prometheus.CounterOpts{ - Name: "libp2p_rcmgr_peers_allowed_total", - Help: "allowed peers", - }) - mustRegister(peerAllowed) - - peerBlocked := prometheus.NewCounter(prometheus.CounterOpts{ - Name: "libp2p_rcmgr_peer_blocked_total", - Help: "blocked peers", - }) - mustRegister(peerBlocked) - - protocolAllowed := prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "libp2p_rcmgr_protocols_allowed_total", - Help: "allowed streams attached to a protocol", - }, - []string{protocol}, - ) - mustRegister(protocolAllowed) - - protocolBlocked := prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "libp2p_rcmgr_protocols_blocked_total", - Help: "blocked streams attached to a protocol", - }, - []string{protocol}, - ) - 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}, - ) - mustRegister(protocolPeerBlocked) - - serviceAllowed := prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "libp2p_rcmgr_services_allowed_total", - Help: "allowed streams attached to a service", - }, - []string{service}, - ) - mustRegister(serviceAllowed) - - serviceBlocked := prometheus.NewCounterVec( - prometheus.CounterOpts{ - Name: "libp2p_rcmgr_services_blocked_total", - Help: "blocked streams attached to a service", - }, - []string{service}, - ) - 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}, - ) - mustRegister(servicePeerBlocked) - - memoryAllowed := prometheus.NewCounter(prometheus.CounterOpts{ - Name: "libp2p_rcmgr_memory_allocations_allowed_total", - Help: "allowed memory allocations", - }) - mustRegister(memoryAllowed) - - memoryBlocked := prometheus.NewCounter(prometheus.CounterOpts{ - Name: "libp2p_rcmgr_memory_allocations_blocked_total", - Help: "blocked memory allocations", - }) - 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() -} From bc33c8770ddd1c5e73097504c57b8184f098bedc Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 27 Nov 2024 20:28:26 +0100 Subject: [PATCH 2/4] docs: add changelog --- docs/changelogs/v0.33.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/changelogs/v0.33.md b/docs/changelogs/v0.33.md index 1230fca37ce..c6a43876156 100644 --- a/docs/changelogs/v0.33.md +++ b/docs/changelogs/v0.33.md @@ -18,6 +18,11 @@ This release includes some refactorings and improvements affecting Bitswap which should improve reliability. One of the changes affects blocks providing. Previously, the bitswap layer took care itself of announcing new blocks -added or received- with the configured provider (i.e. DHT). This bypassed the "Reprovider", that is, the system that manages precisely "providing" the blocks stored by Kubo. The Reprovider knows how to take advantage of the [AcceleratedDHTClient](https://github.com/ipfs/kubo/blob/master/docs/config.md#routingaccelerateddhtclient), is able to handle priorities, logs statistics and is able to resume on daemon reboot where it left off. From now on, Bitswap will not be doing any providing on-the-side and all announcements are managed by the reprovider. In some cases, when the reproviding queue is full with other elements, this may cause additional delays, but more likely this will result in improved block-providing behaviour overall. +#### Using default `libp2p_rcmgr` metrics + +Bespoke rcmgr metrics [were removed](https://github.com/ipfs/kubo/pull/9947), Kubo now exposes only the default `libp2p_rcmgr` metrics from go-libp2p. +This makes it easier to compare Kubo with custom implementations based on go-libp2p. + #### 📦️ Dependency updates - update `boxo` to [v0.24.TODO](https://github.com/ipfs/boxo/releases/tag/v0.24.TODO) From 53ee4aca449b3963c4cc2e760b0cdbbbf0559ecd Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 27 Nov 2024 20:32:21 +0100 Subject: [PATCH 3/4] test: update test/sharness/t0119-prometheus-data we no longer expect those extra ones --- docs/changelogs/v0.33.md | 11 ++++++++++- .../prometheus_metrics_added_by_enabling_rcmgr | 4 ---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/changelogs/v0.33.md b/docs/changelogs/v0.33.md index c6a43876156..e37359ff8ee 100644 --- a/docs/changelogs/v0.33.md +++ b/docs/changelogs/v0.33.md @@ -21,7 +21,16 @@ This release includes some refactorings and improvements affecting Bitswap which #### Using default `libp2p_rcmgr` metrics Bespoke rcmgr metrics [were removed](https://github.com/ipfs/kubo/pull/9947), Kubo now exposes only the default `libp2p_rcmgr` metrics from go-libp2p. -This makes it easier to compare Kubo with custom implementations based on go-libp2p. +This makes it easier to compare Kubo with custom implementations based on go-libp2p. Removed metrics: + +```diff +-libp2p_rcmgr_memory_allocations_allowed_total +-libp2p_rcmgr_memory_allocations_blocked_total +-libp2p_rcmgr_peer_blocked_total +-libp2p_rcmgr_peers_allowed_total +``` + +If you depended on removed ones, please fill an issue to add them to the upstream [go-libp2p](https://github.com/libp2p/go-libp2p). #### 📦️ Dependency updates diff --git a/test/sharness/t0119-prometheus-data/prometheus_metrics_added_by_enabling_rcmgr b/test/sharness/t0119-prometheus-data/prometheus_metrics_added_by_enabling_rcmgr index 382ab125602..e69de29bb2d 100644 --- a/test/sharness/t0119-prometheus-data/prometheus_metrics_added_by_enabling_rcmgr +++ b/test/sharness/t0119-prometheus-data/prometheus_metrics_added_by_enabling_rcmgr @@ -1,4 +0,0 @@ -libp2p_rcmgr_memory_allocations_allowed_total -libp2p_rcmgr_memory_allocations_blocked_total -libp2p_rcmgr_peer_blocked_total -libp2p_rcmgr_peers_allowed_total From 58efb84e850c86bd8bce003246e1225509e6f1cd Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Wed, 27 Nov 2024 20:50:04 +0100 Subject: [PATCH 4/4] docs: simplify changelog --- docs/changelogs/v0.33.md | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/docs/changelogs/v0.33.md b/docs/changelogs/v0.33.md index e37359ff8ee..cd0d0f7db21 100644 --- a/docs/changelogs/v0.33.md +++ b/docs/changelogs/v0.33.md @@ -21,15 +21,7 @@ This release includes some refactorings and improvements affecting Bitswap which #### Using default `libp2p_rcmgr` metrics Bespoke rcmgr metrics [were removed](https://github.com/ipfs/kubo/pull/9947), Kubo now exposes only the default `libp2p_rcmgr` metrics from go-libp2p. -This makes it easier to compare Kubo with custom implementations based on go-libp2p. Removed metrics: - -```diff --libp2p_rcmgr_memory_allocations_allowed_total --libp2p_rcmgr_memory_allocations_blocked_total --libp2p_rcmgr_peer_blocked_total --libp2p_rcmgr_peers_allowed_total -``` - +This makes it easier to compare Kubo with custom implementations based on go-libp2p. If you depended on removed ones, please fill an issue to add them to the upstream [go-libp2p](https://github.com/libp2p/go-libp2p). #### 📦️ Dependency updates