Skip to content

Commit a3f0f80

Browse files
committed
profiler: collect peer latency and tunnel types
1 parent 1936840 commit a3f0f80

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

gateway/gateway.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/zllovesuki/t/multiplexer"
1313
"github.com/zllovesuki/t/multiplexer/alpn"
14+
"github.com/zllovesuki/t/profiler"
1415
"github.com/zllovesuki/t/server"
1516
"github.com/zllovesuki/t/shared"
1617

@@ -93,19 +94,27 @@ func (g *Gateway) handleConnection(ctx context.Context, conn *tls.Conn) {
9394
switch cs.NegotiatedProtocol {
9495
case alpn.Unknown.String(), alpn.HTTP.String():
9596
g.Logger.Debug("forward http connection")
97+
profiler.GatewayReqsType.WithLabelValues("http").Inc()
98+
9699
g.httpTunnelAcceptor.ch <- conn
97100
case alpn.Raw.String():
98101
g.Logger.Debug("forward raw connection")
102+
profiler.GatewayReqsType.WithLabelValues("raw").Inc()
103+
99104
_, err := g.Multiplexer.Forward(ctx, conn, g.link(cs.ServerName, cs.NegotiatedProtocol))
100105
if err != nil {
101106
g.Logger.Error("establish raw link error", zap.Error(err))
102107
conn.Close()
103108
}
104109
case alpn.Multiplexer.String():
105110
g.Logger.Error("received alpn proposal for multiplexer on gateway")
111+
profiler.GatewayReqsType.WithLabelValues("multiplexer").Inc()
112+
106113
conn.Close()
107114
default:
108115
g.Logger.Error("unknown alpn proposal", zap.String("proposal", cs.NegotiatedProtocol))
116+
profiler.GatewayReqsType.WithLabelValues("error").Inc()
117+
109118
conn.Close()
110119
}
111120
}

gateway/tunnel_http.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func (g *Gateway) httpHandler() http.Handler {
2525
},
2626
Transport: &http.Transport{
2727
DialContext: func(c context.Context, network, addr string) (net.Conn, error) {
28-
return g.Multiplexer.Direct(c, g.link(addr, alpn.HTTP.String()))
28+
conn, err := g.Multiplexer.Direct(c, g.link(addr, alpn.HTTP.String()))
29+
return conn, err
2930
},
3031
// TODO(zllovesuki): Make MaxConnsPerHost configurable
3132
MaxConnsPerHost: 15,

profiler/metrics.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,24 @@ var (
1010
Name: "gateway_requests_total",
1111
}, []string{"status", "type"})
1212

13+
GatewayReqsType = prometheus.NewCounterVec(prometheus.CounterOpts{
14+
Namespace: "t",
15+
Subsystem: "server",
16+
Help: "Count of all forwarded tunnel types",
17+
Name: "gateway_tunnel_type",
18+
}, []string{"type"})
19+
1320
ConnectionStats = prometheus.NewCounterVec(prometheus.CounterOpts{
1421
Namespace: "t",
1522
Subsystem: "server",
1623
Help: "Stats on multiplexer connections",
1724
Name: "multiplexer_stats",
1825
}, []string{"desc", "type"})
26+
27+
PeerLatencyHist = prometheus.NewHistogram(prometheus.HistogramOpts{
28+
Namespace: "t",
29+
Subsystem: "server",
30+
Help: "Histogram of peers latency via gossip",
31+
Name: "peer_rtt_ms",
32+
})
1933
)

profiler/profiler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ func StartProfiler(addr string) {
1212

1313
r := prometheus.NewRegistry()
1414
r.MustRegister(GatewayRequests)
15+
r.MustRegister(GatewayReqsType)
1516
r.MustRegister(ConnectionStats)
17+
r.MustRegister(PeerLatencyHist)
1618

1719
m.Handle("/", debug())
1820
m.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))

server/gossip.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/zllovesuki/t/acme"
1111
"github.com/zllovesuki/t/multiplexer"
1212
"github.com/zllovesuki/t/multiplexer/alpn"
13+
"github.com/zllovesuki/t/profiler"
1314
"github.com/zllovesuki/t/state"
1415

1516
"github.com/hashicorp/memberlist"
@@ -218,8 +219,10 @@ func (s *Server) NotifyPingComplete(other *memberlist.Node, rtt time.Duration, p
218219
s.logger.Error("unmarshal node meta from ping", zap.Error(err))
219220
return
220221
}
222+
221223
n := binary.BigEndian.Uint64(payload)
222-
s.logger.Info("ping", zap.Uint64("Peer", m.PeerID), zap.Duration("rtt", rtt), zap.Uint64("numPeers", n))
224+
s.logger.Debug("ping", zap.Uint64("Peer", m.PeerID), zap.Duration("rtt", rtt), zap.Uint64("numPeers", n))
225+
profiler.PeerLatencyHist.Observe(float64(rtt / time.Millisecond))
223226
}
224227

225228
// ======== Gossip Helpers ========

0 commit comments

Comments
 (0)