@cryptotaxi247 / kubo / commits / 778a41895

refactor(rcmgr): use default libp2p rcmgr metrics (#9947)

Co-authored-by: Marcin Rataj <lidel@lidel.org>

Marten Seemann committed Nov 28, 2024 at 03:50 UTC 778a4189528f252bd3e5fb1257d53ff1119d2926
4 files changed +10 -260
core/node/libp2p/rcmgr.go
+4 -5
@@ -7,6 +7,10 @@ import (
7 "os"
8 "path/filepath"
9
10 + "github.com/ipfs/kubo/config"
11 + "github.com/ipfs/kubo/core/node/helpers"
12 + "github.com/ipfs/kubo/repo"
13 +
14 "github.com/benbjohnson/clock"
15 logging "github.com/ipfs/go-log/v2"
16 "github.com/libp2p/go-libp2p"
@@ -16,10 +20,6 @@ import (
20 rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
21 "github.com/multiformats/go-multiaddr"
22 "go.uber.org/fx"
19 -
20 - "github.com/ipfs/kubo/config"
21 - "github.com/ipfs/kubo/core/node/helpers"
22 - "github.com/ipfs/kubo/repo"
23 )
24
25 var rcmgrLogger = logging.Logger("rcmgr")
@@ -70,7 +70,6 @@ filled in with autocomputed defaults.`)
70 }
71
72 ropts := []rcmgr.Option{
73 - rcmgr.WithMetrics(createRcmgrMetrics()),
73 rcmgr.WithTraceReporter(str),
74 rcmgr.WithLimitPerSubnet(
75 nil,
core/node/libp2p/rcmgr_metrics.go deleted
-251
@@ -1,251 +0,0 @@
1 -package libp2p
2 -
3 -import (
4 - "errors"
5 - "strconv"
6 -
7 - "github.com/libp2p/go-libp2p/core/network"
8 - "github.com/libp2p/go-libp2p/core/peer"
9 - "github.com/libp2p/go-libp2p/core/protocol"
10 - rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
11 -
12 - "github.com/prometheus/client_golang/prometheus"
13 -)
14 -
15 -func mustRegister(c prometheus.Collector) {
16 - err := prometheus.Register(c)
17 - are := prometheus.AlreadyRegisteredError{}
18 - if errors.As(err, &are) {
19 - return
20 - }
21 - if err != nil {
22 - panic(err)
23 - }
24 -}
25 -
26 -func createRcmgrMetrics() rcmgr.MetricsReporter {
27 - const (
28 - direction = "direction"
29 - usesFD = "usesFD"
30 - protocol = "protocol"
31 - service = "service"
32 - )
33 -
34 - connAllowed := prometheus.NewCounterVec(
35 - prometheus.CounterOpts{
36 - Name: "libp2p_rcmgr_conns_allowed_total",
37 - Help: "allowed connections",
38 - },
39 - []string{direction, usesFD},
40 - )
41 - mustRegister(connAllowed)
42 -
43 - connBlocked := prometheus.NewCounterVec(
44 - prometheus.CounterOpts{
45 - Name: "libp2p_rcmgr_conns_blocked_total",
46 - Help: "blocked connections",
47 - },
48 - []string{direction, usesFD},
49 - )
50 - mustRegister(connBlocked)
51 -
52 - streamAllowed := prometheus.NewCounterVec(
53 - prometheus.CounterOpts{
54 - Name: "libp2p_rcmgr_streams_allowed_total",
55 - Help: "allowed streams",
56 - },
57 - []string{direction},
58 - )
59 - mustRegister(streamAllowed)
60 -
61 - streamBlocked := prometheus.NewCounterVec(
62 - prometheus.CounterOpts{
63 - Name: "libp2p_rcmgr_streams_blocked_total",
64 - Help: "blocked streams",
65 - },
66 - []string{direction},
67 - )
68 - mustRegister(streamBlocked)
69 -
70 - peerAllowed := prometheus.NewCounter(prometheus.CounterOpts{
71 - Name: "libp2p_rcmgr_peers_allowed_total",
72 - Help: "allowed peers",
73 - })
74 - mustRegister(peerAllowed)
75 -
76 - peerBlocked := prometheus.NewCounter(prometheus.CounterOpts{
77 - Name: "libp2p_rcmgr_peer_blocked_total",
78 - Help: "blocked peers",
79 - })
80 - mustRegister(peerBlocked)
81 -
82 - protocolAllowed := prometheus.NewCounterVec(
83 - prometheus.CounterOpts{
84 - Name: "libp2p_rcmgr_protocols_allowed_total",
85 - Help: "allowed streams attached to a protocol",
86 - },
87 - []string{protocol},
88 - )
89 - mustRegister(protocolAllowed)
90 -
91 - protocolBlocked := prometheus.NewCounterVec(
92 - prometheus.CounterOpts{
93 - Name: "libp2p_rcmgr_protocols_blocked_total",
94 - Help: "blocked streams attached to a protocol",
95 - },
96 - []string{protocol},
97 - )
98 - mustRegister(protocolBlocked)
99 -
100 - protocolPeerBlocked := prometheus.NewCounterVec(
101 - prometheus.CounterOpts{
102 - Name: "libp2p_rcmgr_protocols_for_peer_blocked_total",
103 - Help: "blocked streams attached to a protocol for a specific peer",
104 - },
105 - []string{protocol},
106 - )
107 - mustRegister(protocolPeerBlocked)
108 -
109 - serviceAllowed := prometheus.NewCounterVec(
110 - prometheus.CounterOpts{
111 - Name: "libp2p_rcmgr_services_allowed_total",
112 - Help: "allowed streams attached to a service",
113 - },
114 - []string{service},
115 - )
116 - mustRegister(serviceAllowed)
117 -
118 - serviceBlocked := prometheus.NewCounterVec(
119 - prometheus.CounterOpts{
120 - Name: "libp2p_rcmgr_services_blocked_total",
121 - Help: "blocked streams attached to a service",
122 - },
123 - []string{service},
124 - )
125 - mustRegister(serviceBlocked)
126 -
127 - servicePeerBlocked := prometheus.NewCounterVec(
128 - prometheus.CounterOpts{
129 - Name: "libp2p_rcmgr_service_for_peer_blocked_total",
130 - Help: "blocked streams attached to a service for a specific peer",
131 - },
132 - []string{service},
133 - )
134 - mustRegister(servicePeerBlocked)
135 -
136 - memoryAllowed := prometheus.NewCounter(prometheus.CounterOpts{
137 - Name: "libp2p_rcmgr_memory_allocations_allowed_total",
138 - Help: "allowed memory allocations",
139 - })
140 - mustRegister(memoryAllowed)
141 -
142 - memoryBlocked := prometheus.NewCounter(prometheus.CounterOpts{
143 - Name: "libp2p_rcmgr_memory_allocations_blocked_total",
144 - Help: "blocked memory allocations",
145 - })
146 - mustRegister(memoryBlocked)
147 -
148 - return rcmgrMetrics{
149 - connAllowed,
150 - connBlocked,
151 - streamAllowed,
152 - streamBlocked,
153 - peerAllowed,
154 - peerBlocked,
155 - protocolAllowed,
156 - protocolBlocked,
157 - protocolPeerBlocked,
158 - serviceAllowed,
159 - serviceBlocked,
160 - servicePeerBlocked,
161 - memoryAllowed,
162 - memoryBlocked,
163 - }
164 -}
165 -
166 -// Failsafe to ensure interface from go-libp2p-resource-manager is implemented
167 -var _ rcmgr.MetricsReporter = rcmgrMetrics{}
168 -
169 -type rcmgrMetrics struct {
170 - connAllowed *prometheus.CounterVec
171 - connBlocked *prometheus.CounterVec
172 - streamAllowed *prometheus.CounterVec
173 - streamBlocked *prometheus.CounterVec
174 - peerAllowed prometheus.Counter
175 - peerBlocked prometheus.Counter
176 - protocolAllowed *prometheus.CounterVec
177 - protocolBlocked *prometheus.CounterVec
178 - protocolPeerBlocked *prometheus.CounterVec
179 - serviceAllowed *prometheus.CounterVec
180 - serviceBlocked *prometheus.CounterVec
181 - servicePeerBlocked *prometheus.CounterVec
182 - memoryAllowed prometheus.Counter
183 - memoryBlocked prometheus.Counter
184 -}
185 -
186 -func getDirection(d network.Direction) string {
187 - switch d {
188 - default:
189 - return ""
190 - case network.DirInbound:
191 - return "inbound"
192 - case network.DirOutbound:
193 - return "outbound"
194 - }
195 -}
196 -
197 -func (r rcmgrMetrics) AllowConn(dir network.Direction, usefd bool) {
198 - r.connAllowed.WithLabelValues(getDirection(dir), strconv.FormatBool(usefd)).Inc()
199 -}
200 -
201 -func (r rcmgrMetrics) BlockConn(dir network.Direction, usefd bool) {
202 - r.connBlocked.WithLabelValues(getDirection(dir), strconv.FormatBool(usefd)).Inc()
203 -}
204 -
205 -func (r rcmgrMetrics) AllowStream(_ peer.ID, dir network.Direction) {
206 - r.streamAllowed.WithLabelValues(getDirection(dir)).Inc()
207 -}
208 -
209 -func (r rcmgrMetrics) BlockStream(_ peer.ID, dir network.Direction) {
210 - r.streamBlocked.WithLabelValues(getDirection(dir)).Inc()
211 -}
212 -
213 -func (r rcmgrMetrics) AllowPeer(_ peer.ID) {
214 - r.peerAllowed.Inc()
215 -}
216 -
217 -func (r rcmgrMetrics) BlockPeer(_ peer.ID) {
218 - r.peerBlocked.Inc()
219 -}
220 -
221 -func (r rcmgrMetrics) AllowProtocol(proto protocol.ID) {
222 - r.protocolAllowed.WithLabelValues(string(proto)).Inc()
223 -}
224 -
225 -func (r rcmgrMetrics) BlockProtocol(proto protocol.ID) {
226 - r.protocolBlocked.WithLabelValues(string(proto)).Inc()
227 -}
228 -
229 -func (r rcmgrMetrics) BlockProtocolPeer(proto protocol.ID, _ peer.ID) {
230 - r.protocolPeerBlocked.WithLabelValues(string(proto)).Inc()
231 -}
232 -
233 -func (r rcmgrMetrics) AllowService(svc string) {
234 - r.serviceAllowed.WithLabelValues(svc).Inc()
235 -}
236 -
237 -func (r rcmgrMetrics) BlockService(svc string) {
238 - r.serviceBlocked.WithLabelValues(svc).Inc()
239 -}
240 -
241 -func (r rcmgrMetrics) BlockServicePeer(svc string, _ peer.ID) {
242 - r.servicePeerBlocked.WithLabelValues(svc).Inc()
243 -}
244 -
245 -func (r rcmgrMetrics) AllowMemory(_ int) {
246 - r.memoryAllowed.Inc()
247 -}
248 -
249 -func (r rcmgrMetrics) BlockMemory(_ int) {
250 - r.memoryBlocked.Inc()
251 -}
docs/changelogs/v0.33.md
+6
@@ -18,6 +18,12 @@
18
19 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.
20
21 +#### Using default `libp2p_rcmgr` metrics
22 +
23 +Bespoke rcmgr metrics [were removed](https://github.com/ipfs/kubo/pull/9947), Kubo now exposes only the default `libp2p_rcmgr` metrics from go-libp2p.
24 +This makes it easier to compare Kubo with custom implementations based on go-libp2p.
25 +If you depended on removed ones, please fill an issue to add them to the upstream [go-libp2p](https://github.com/libp2p/go-libp2p).
26 +
27 #### 📦️ Dependency updates
28
29 - update `boxo` to [v0.24.TODO](https://github.com/ipfs/boxo/releases/tag/v0.24.TODO)
test/sharness/t0119-prometheus-data/prometheus_metrics_added_by_enabling_rcmgr
-4
@@ -1,4 +0,0 @@
1 -libp2p_rcmgr_memory_allocations_allowed_total
2 -libp2p_rcmgr_memory_allocations_blocked_total
3 -libp2p_rcmgr_peer_blocked_total
4 -libp2p_rcmgr_peers_allowed_total