Skip to content

Commit

Permalink
metrics: add handshake duration metrics (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 authored Nov 12, 2022
1 parent 1b3f1e1 commit 1514443
Show file tree
Hide file tree
Showing 8 changed files with 1,781 additions and 264 deletions.
2 changes: 1 addition & 1 deletion internal/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (

SmuxGCDuration = 30 * time.Second
SmuxMaxAliveDuration = 1 * time.Hour
SmuxMaxStreamCnt = 50
SmuxMaxStreamCnt = 5

Listen_RAW = "raw"
Listen_WS = "ws"
Expand Down
62 changes: 32 additions & 30 deletions internal/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,11 @@ func (r *Relay) RunLocalTCPServer() error {
web.CurConnectionCount.WithLabelValues(remote.Label, web.METRIC_CONN_TCP).Inc()
defer web.CurConnectionCount.WithLabelValues(remote.Label, web.METRIC_CONN_TCP).Dec()
defer c.Close()
t1 := time.Now()
if err := r.TP.HandleTCPConn(c, remote); err != nil {
r.L.Errorf("HandleTCPConn meet error from:%s to:%s err:%s", c.RemoteAddr(), remote.Address, err)
}
web.HandShakeDuration.WithLabelValues(remote.Label).Observe(float64(time.Since(t1).Milliseconds()))
}(c)
}
}
Expand Down Expand Up @@ -192,6 +194,35 @@ func (r *Relay) RunLocalUDPServer() error {
}
}

func (r *Relay) RunLocalMTCPServer() error {
mTCPServer := transporter.NewMTCPServer(r.L.Named("MTCPServer"), r.LocalTCPAddr)
r.closeTcpF = func() error {
return mTCPServer.Close()
}

go func() {
r.L.Infof("Start MTCP relay server %s", r.Name)
mTCPServer.ListenAndServe()
}()

tp := r.TP.(*transporter.Raw)
for {
conn, e := mTCPServer.Accept()
if e != nil {
return e
}
go func(c net.Conn) {
remote := tp.GetRemote()
web.CurConnectionCount.WithLabelValues(remote.Label, web.METRIC_CONN_TCP).Inc()
defer web.CurConnectionCount.WithLabelValues(remote.Label, web.METRIC_CONN_TCP).Dec()
defer c.Close()
if err := tp.HandleTCPConn(c, remote); err != nil {
r.L.Errorf("HandleTCPConn meet error from:%s to:%s err:%s", c.RemoteAddr(), remote.Address, err)
}
}(conn)
}
}

func (r *Relay) RunLocalWSServer() error {
tp := r.TP.(*transporter.Raw)
mux := mux.NewRouter()
Expand Down Expand Up @@ -234,7 +265,7 @@ func (r *Relay) RunLocalWSSServer() error {
r.closeTcpF = func() error {
return lis.Close()
}
r.L.Infof("Start WSS relay %s", r.Name)
r.L.Infof("Start WSS relay Server %s", r.Name)
return server.Serve(tls.NewListener(lis, server.TLSConfig))
}

Expand Down Expand Up @@ -277,32 +308,3 @@ func (r *Relay) RunLocalMWSSServer() error {
go tp.HandleMWssRequest(conn)
}
}

func (r *Relay) RunLocalMTCPServer() error {
mTCPServer := transporter.NewMTCPServer(r.L.Named("MTCPServer"), r.LocalTCPAddr)
r.closeTcpF = func() error {
return mTCPServer.Close()
}

go func() {
r.L.Infof("Start MTCP relay server %s", r.Name)
mTCPServer.ListenAndServe()
}()

tp := r.TP.(*transporter.Raw)
for {
conn, e := mTCPServer.Accept()
if e != nil {
return e
}
go func(c net.Conn) {
remote := tp.GetRemote()
web.CurConnectionCount.WithLabelValues(remote.Label, web.METRIC_CONN_TCP).Inc()
defer web.CurConnectionCount.WithLabelValues(remote.Label, web.METRIC_CONN_TCP).Dec()
defer c.Close()
if err := tp.HandleTCPConn(c, remote); err != nil {
r.L.Errorf("HandleTCPConn meet error from:%s to:%s err:%s", c.RemoteAddr(), remote.Address, err)
}
}(conn)
}
}
6 changes: 2 additions & 4 deletions internal/transporter/mtcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ type MTCPClient struct {
}

func NewMTCPClient(l *zap.SugaredLogger) *MTCPClient {
return &MTCPClient{
l: l.Named("MTCPClient"),
}
return &MTCPClient{l: l}
}

func (c *MTCPClient) InitNewSession(ctx context.Context, addr string) (*smux.Session, error) {
Expand All @@ -138,6 +136,6 @@ func (c *MTCPClient) InitNewSession(ctx context.Context, addr string) (*smux.Ses
if err != nil {
return nil, err
}
c.l.Infof("Init new session to: %s", rc.RemoteAddr())
c.l.Infof("init new session to: %s", rc.RemoteAddr())
return session, nil
}
2 changes: 1 addition & 1 deletion internal/transporter/mwss.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,6 @@ func (c *MWSSClient) InitNewSession(ctx context.Context, addr string) (*smux.Ses
if err != nil {
return nil, err
}
c.l.Infof("Init new session to: %s", rc.RemoteAddr())
c.l.Infof("init new session to: %s", rc.RemoteAddr())
return session, nil
}
3 changes: 3 additions & 0 deletions internal/transporter/smux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/Ehco1996/ehco/internal/constant"
"github.com/Ehco1996/ehco/internal/web"
"github.com/xtaci/smux"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -90,6 +91,7 @@ func (tr *smuxTransporter) Dial(ctx context.Context, addr string) (conn net.Conn
}

// create new one
t1 := time.Now()
if session == nil {
session, err = tr.initSessionF(ctx, addr)
if err != nil {
Expand All @@ -105,5 +107,6 @@ func (tr *smuxTransporter) Dial(ctx context.Context, addr string) (conn net.Conn
session.Close()
return nil, err
}
web.HandShakeDuration.WithLabelValues(addr).Observe(float64(time.Since(t1).Milliseconds()))
return stream, nil
}
1 change: 1 addition & 0 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func registerMetrics(cfg *config.Config) {
prometheus.MustRegister(EhcoAlive)
prometheus.MustRegister(CurConnectionCount)
prometheus.MustRegister(NetWorkTransmitBytes)
prometheus.MustRegister(HandShakeDuration)

EhcoAlive.Set(EhcoAliveStateInit)

Expand Down
8 changes: 8 additions & 0 deletions internal/web/traffic_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ var (
Help: "传输流量总量bytes",
ConstLabels: ConstLabels,
}, []string{METRIC_LABEL_REMOTE, METRIC_LABEL_CONN_TYPE})

HandShakeDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Subsystem: METRIC_SUBSYSTEM_TRAFFIC,
Namespace: METRIC_NS,
Name: "handshake_duration",
Help: "握手时间ms",
ConstLabels: ConstLabels,
}, []string{METRIC_LABEL_REMOTE})
)
Loading

0 comments on commit 1514443

Please sign in to comment.