55package libp2p
66
77import (
8+ "strings"
9+
810 "github.com/ethersphere/bee/v2/pkg/bzz"
911 m "github.com/ethersphere/bee/v2/pkg/metrics"
1012 ma "github.com/multiformats/go-multiaddr"
@@ -14,7 +16,19 @@ import (
1416
1517const (
1618 connectionTransportLabelName = "transport"
17- connectionTransportHelp = "The 'transport' label is one of: tcp, ws, wss, quic-v1, quic, unknown."
19+ connectionPublicLabelName = "public"
20+ )
21+
22+ var (
23+ transportLabelValues = []string {
24+ bzz .TransportTCP .String (),
25+ bzz .TransportWS .String (),
26+ bzz .TransportWSS .String (),
27+ bzz .TransportQUICV1 .String (),
28+ bzz .TransportQUIC .String (),
29+ bzz .TransportUnknown .String (),
30+ }
31+ publicLabelValues = []string {"true" , "false" }
1832)
1933
2034type metrics struct {
@@ -23,8 +37,6 @@ type metrics struct {
2337 // using reflection
2438 CreatedConnectionCount * prometheus.CounterVec
2539 HandledConnectionCount * prometheus.CounterVec
26- PublicAddressConnections * prometheus.CounterVec
27- PrivateAddressConnections * prometheus.CounterVec
2840 CreatedStreamCount prometheus.Counter
2941 ClosedStreamCount prometheus.Counter
3042 StreamResetCount prometheus.Counter
@@ -41,44 +53,41 @@ type metrics struct {
4153
4254func newMetrics () metrics {
4355 subsystem := "libp2p"
56+ transportHelp := "The 'transport' label is one of: " + strings .Join (transportLabelValues , ", " )
57+ publicHelp := "The 'public' label is one of: " + strings .Join (publicLabelValues , ", " ) + " (true = public remote multiaddr)."
58+
59+ createdConnectionCount := prometheus .NewCounterVec (
60+ prometheus.CounterOpts {
61+ Namespace : m .Namespace ,
62+ Subsystem : subsystem ,
63+ Name : "created_connection_count" ,
64+ Help : "Number of initiated outgoing libp2p connections. " + transportHelp ,
65+ },
66+ []string {connectionTransportLabelName },
67+ )
68+
69+ handledConnectionCount := prometheus .NewCounterVec (
70+ prometheus.CounterOpts {
71+ Namespace : m .Namespace ,
72+ Subsystem : subsystem ,
73+ Name : "handled_connection_count" ,
74+ Help : "Number of handled incoming libp2p connections. " + transportHelp + " " + publicHelp ,
75+ },
76+ []string {connectionTransportLabelName , connectionPublicLabelName },
77+ )
78+
79+ // Ensure all expected label value combinations exist as 0-valued series,
80+ // so Grafana shows a flat line instead of "No Data".
81+ for _ , transport := range transportLabelValues {
82+ createdConnectionCount .WithLabelValues (transport ).Add (0 )
83+ for _ , public := range publicLabelValues {
84+ handledConnectionCount .WithLabelValues (transport , public ).Add (0 )
85+ }
86+ }
4487
4588 return metrics {
46- CreatedConnectionCount : prometheus .NewCounterVec (
47- prometheus.CounterOpts {
48- Namespace : m .Namespace ,
49- Subsystem : subsystem ,
50- Name : "created_connection_count" ,
51- Help : "Number of initiated outgoing libp2p connections. " + connectionTransportHelp ,
52- },
53- []string {connectionTransportLabelName },
54- ),
55- HandledConnectionCount : prometheus .NewCounterVec (
56- prometheus.CounterOpts {
57- Namespace : m .Namespace ,
58- Subsystem : subsystem ,
59- Name : "handled_connection_count" ,
60- Help : "Number of handled incoming libp2p connections. " + connectionTransportHelp ,
61- },
62- []string {connectionTransportLabelName },
63- ),
64- PublicAddressConnections : prometheus .NewCounterVec (
65- prometheus.CounterOpts {
66- Namespace : m .Namespace ,
67- Subsystem : subsystem ,
68- Name : "public_address_connections_total" ,
69- Help : "Number of libp2p connections whose remote multiaddr is a public address. " + connectionTransportHelp ,
70- },
71- []string {connectionTransportLabelName },
72- ),
73- PrivateAddressConnections : prometheus .NewCounterVec (
74- prometheus.CounterOpts {
75- Namespace : m .Namespace ,
76- Subsystem : subsystem ,
77- Name : "private_address_connections_total" ,
78- Help : "Number of libp2p connections whose remote multiaddr is a private address. " + connectionTransportHelp ,
79- },
80- []string {connectionTransportLabelName },
81- ),
89+ CreatedConnectionCount : createdConnectionCount ,
90+ HandledConnectionCount : handledConnectionCount ,
8291 CreatedStreamCount : prometheus .NewCounter (prometheus.CounterOpts {
8392 Namespace : m .Namespace ,
8493 Subsystem : subsystem ,
@@ -159,35 +168,15 @@ func (m metrics) incCreatedConnection(addr ma.Multiaddr) {
159168}
160169
161170func (m metrics ) observeHandledConnection (addr ma.Multiaddr ) {
162- transport := connectionTransportLabel (addr )
163- m .HandledConnectionCount .WithLabelValues (transport ).Inc ()
171+ public := "false"
164172 if manet .IsPublicAddr (addr ) {
165- m .PublicAddressConnections .WithLabelValues (transport ).Inc ()
166- return
173+ public = "true"
167174 }
168- m .PrivateAddressConnections .WithLabelValues (transport ).Inc ()
175+ m .HandledConnectionCount .WithLabelValues (connectionTransportLabel ( addr ), public ).Inc ()
169176}
170177
171- // connectionTransportLabel returns the Prometheus transport label for a connection
172- // multiaddr. Live WSS connections are often encoded with the deprecated /wss
173- // component rather than the /tls/.../ws form used in advertised AutoTLS addresses.
174178func connectionTransportLabel (addr ma.Multiaddr ) string {
175- if addr == nil {
176- return bzz .TransportUnknown .String ()
177- }
178- if _ , err := addr .ValueForProtocol (ma .P_WSS ); err == nil {
179- return bzz .TransportWSS .String ()
180- }
181- if t := bzz .ClassifyTransport (addr ); t != bzz .TransportUnknown {
182- return t .String ()
183- }
184- if _ , err := addr .ValueForProtocol (ma .P_QUIC_V1 ); err == nil {
185- return "quic-v1"
186- }
187- if _ , err := addr .ValueForProtocol (ma .P_QUIC ); err == nil {
188- return "quic"
189- }
190- return bzz .TransportUnknown .String ()
179+ return bzz .ClassifyTransport (addr ).String ()
191180}
192181
193182func (s * Service ) Metrics () []prometheus.Collector {
0 commit comments