Skip to content

Commit

Permalink
add early muxer selection to swarm metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Feb 21, 2023
1 parent d9004d2 commit 1235885
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 26 deletions.
2 changes: 2 additions & 0 deletions core/network/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type ConnectionState struct {
Security protocol.ID
// the transport used on this connection. For example: tcp
Transport string
// indicates whether StreamMultiplexer was selected using inlined muxer negotiation
EarlyMuxerSelection bool
}

// ConnSecurity is the interface that one can mix into a connection interface to
Expand Down
135 changes: 134 additions & 1 deletion dashboards/swarm/swarm.json
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,139 @@
"title": "Connection Duration",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
}
]
},
"unit": "s"
},
"overrides": [
{
"matcher": {
"id": "byName",
"options": "early muxer"
},
"properties": [
{
"id": "color",
"value": {
"fixedColor": "green",
"mode": "fixed"
}
}
]
},
{
"matcher": {
"id": "byName",
"options": "regular"
},
"properties": [
{
"id": "color",
"value": {
"fixedColor": "yellow",
"mode": "fixed"
}
}
]
}
]
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"y": 25
},
"id": 35,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "sum(rate(libp2p_swarm_handshake_latency_seconds_sum{transport=\"tcp\",early_muxer=\"true\"}[$__rate_interval])) / sum(rate(libp2p_swarm_handshake_latency_seconds_count{transport=\"tcp\",early_muxer=\"true\"}[$__rate_interval]))",
"hide": false,
"legendFormat": "early muxer",
"range": true,
"refId": "A"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "sum(rate(libp2p_swarm_handshake_latency_seconds_sum{transport=\"tcp\",early_muxer=\"false\"}[$__rate_interval])) / sum(rate(libp2p_swarm_handshake_latency_seconds_count{transport=\"tcp\",early_muxer=\"false\"}[$__rate_interval]))",
"hide": false,
"legendFormat": "regular",
"range": true,
"refId": "B"
}
],
"title": "Handshake Latency (Early Muxer Selection)",
"type": "timeseries"
},
{
"collapsed": false,
"gridPos": {
Expand Down Expand Up @@ -2485,6 +2618,6 @@
"timezone": "",
"title": "libp2p Swarm",
"uid": "a15PyhO4z",
"version": 12,
"version": 2,
"weekStart": ""
}
14 changes: 10 additions & 4 deletions p2p/net/swarm/swarm_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
Name: "connections_opened_total",
Help: "Connections Opened",
},
[]string{"dir", "transport", "security", "muxer", "ip_version"},
[]string{"dir", "transport", "security", "muxer", "early_muxer", "ip_version"},
)
keyTypes = prometheus.NewCounterVec(
prometheus.CounterOpts{
Expand All @@ -42,7 +42,7 @@ var (
Name: "connections_closed_total",
Help: "Connections Closed",
},
[]string{"dir", "transport", "security", "muxer", "ip_version"},
[]string{"dir", "transport", "security", "muxer", "early_muxer", "ip_version"},
)
dialError = prometheus.NewCounterVec(
prometheus.CounterOpts{
Expand All @@ -59,7 +59,7 @@ var (
Help: "Duration of a Connection",
Buckets: prometheus.ExponentialBuckets(1.0/16, 2, 25), // up to 24 days
},
[]string{"dir", "transport", "security", "muxer", "ip_version"},
[]string{"dir", "transport", "security", "muxer", "early_muxer", "ip_version"},
)
connHandshakeLatency = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Expand All @@ -68,7 +68,7 @@ var (
Help: "Duration of the libp2p Handshake",
Buckets: prometheus.ExponentialBuckets(0.001, 1.3, 35),
},
[]string{"transport", "security", "muxer", "ip_version"},
[]string{"transport", "security", "muxer", "early_muxer", "ip_version"},
)
)

Expand Down Expand Up @@ -105,6 +105,12 @@ func appendConnectionState(tags []string, cs network.ConnectionState) []string {
// For example, QUIC doesn't set security nor muxer.
tags = append(tags, string(cs.Security))
tags = append(tags, string(cs.StreamMultiplexer))

earlyMuxer := "false"
if cs.EarlyMuxerSelection {
earlyMuxer = "true"
}
tags = append(tags, earlyMuxer)
return tags
}

Expand Down
4 changes: 2 additions & 2 deletions p2p/net/swarm/swarm_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestMetricsNoAllocNoCover(t *testing.T) {
mt := NewMetricsTracer()

connections := []network.ConnectionState{
{StreamMultiplexer: "yamux", Security: "tls", Transport: "tcp"},
{StreamMultiplexer: "yamux", Security: "noise", Transport: "tcp"},
{StreamMultiplexer: "yamux", Security: "tls", Transport: "tcp", EarlyMuxerSelection: true},
{StreamMultiplexer: "yamux", Security: "noise", Transport: "tcp", EarlyMuxerSelection: false},
{StreamMultiplexer: "", Security: "", Transport: "quic"},
{StreamMultiplexer: "mplex", Security: "noise", Transport: "tcp"},
}
Expand Down
12 changes: 7 additions & 5 deletions p2p/net/upgrader/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ type transportConn struct {
scope network.ConnManagementScope
stat network.ConnStats

muxer protocol.ID
security protocol.ID
muxer protocol.ID
security protocol.ID
earlyMuxerSelection bool
}

var _ transport.CapableConn = &transportConn{}
Expand Down Expand Up @@ -56,8 +57,9 @@ func (t *transportConn) Close() error {

func (t *transportConn) ConnState() network.ConnectionState {
return network.ConnectionState{
StreamMultiplexer: t.muxer,
Security: t.security,
Transport: "tcp",
StreamMultiplexer: t.muxer,
Security: t.security,
Transport: "tcp",
EarlyMuxerSelection: t.earlyMuxerSelection,
}
}
17 changes: 9 additions & 8 deletions p2p/net/upgrader/upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,15 @@ func (u *upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
}

tc := &transportConn{
MuxedConn: smconn,
ConnMultiaddrs: maconn,
ConnSecurity: sconn,
transport: t,
stat: stat,
scope: connScope,
muxer: muxer,
security: security,
MuxedConn: smconn,
ConnMultiaddrs: maconn,
ConnSecurity: sconn,
transport: t,
stat: stat,
scope: connScope,
muxer: muxer,
security: security,
earlyMuxerSelection: sconn.ConnState().EarlyMuxerSelection,
}
return tc, nil
}
Expand Down
1 change: 1 addition & 0 deletions p2p/security/noise/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (s *secureSession) Close() error {
func SessionWithConnState(s *secureSession, muxer protocol.ID) *secureSession {
if s != nil {
s.connectionState.StreamMultiplexer = muxer
s.connectionState.EarlyMuxerSelection = muxer != ""
}
return s
}
2 changes: 2 additions & 0 deletions p2p/security/noise/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,9 @@ func TestHandshakeWithTransportEarlyData(t *testing.T) {
defer respConn.Close()

require.Equal(t, expectedProto, initConn.connectionState.StreamMultiplexer)
require.Equal(t, expectedProto != "", initConn.connectionState.EarlyMuxerSelection)
require.Equal(t, expectedProto, respConn.connectionState.StreamMultiplexer)
require.Equal(t, expectedProto != "", respConn.connectionState.EarlyMuxerSelection)

initData := []byte("Test data for noise transport")
_, err := initConn.Write(initData)
Expand Down
15 changes: 9 additions & 6 deletions p2p/security/tls/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,15 @@ func (t *Transport) setupConn(tlsConn *tls.Conn, remotePubKey ci.PubKey) (sec.Se
}

return &conn{
Conn: tlsConn,
localPeer: t.localPeer,
privKey: t.privKey,
remotePeer: remotePeerID,
remotePubKey: remotePubKey,
connectionState: network.ConnectionState{StreamMultiplexer: protocol.ID(nextProto)},
Conn: tlsConn,
localPeer: t.localPeer,
privKey: t.privKey,
remotePeer: remotePeerID,
remotePubKey: remotePubKey,
connectionState: network.ConnectionState{
StreamMultiplexer: protocol.ID(nextProto),
EarlyMuxerSelection: nextProto != "",
},
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions p2p/security/tls/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func TestHandshakeWithNextProtoSucceeds(t *testing.T) {
require.True(t, clientConn.RemotePublicKey().Equals(serverKey.GetPublic()), "server public key mismatch")
require.True(t, serverConn.RemotePublicKey().Equals(clientKey.GetPublic()), "client public key mismatch")
require.Equal(t, clientConn.ConnState().StreamMultiplexer, expectedMuxer)
require.Equal(t, clientConn.ConnState().EarlyMuxerSelection, expectedMuxer != "")
// exchange some data
_, err = serverConn.Write([]byte("foobar"))
require.NoError(t, err)
Expand Down

0 comments on commit 1235885

Please sign in to comment.