Skip to content

Commit

Permalink
feat: better node metrics chart (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehco1996 authored Sep 2, 2024
1 parent 54e5279 commit 22b6586
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 397 deletions.
18 changes: 6 additions & 12 deletions internal/conn/relay_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ type relayConnImpl struct {
EndTime time.Time `json:"end_time,omitempty"`

// options set those fields
l *zap.SugaredLogger
remote *lb.Node
HandshakeDuration time.Duration
RelayLabel string `json:"relay_label"`
ConnType string `json:"conn_type"`
Options *conf.Options
l *zap.SugaredLogger
remote *lb.Node
RelayLabel string `json:"relay_label"`
ConnType string `json:"conn_type"`
Options *conf.Options
}

func WithRelayLabel(relayLabel string) RelayConnOption {
Expand All @@ -78,12 +77,6 @@ func WithRelayLabel(relayLabel string) RelayConnOption {
}
}

func WithHandshakeDuration(duration time.Duration) RelayConnOption {
return func(rci *relayConnImpl) {
rci.HandshakeDuration = duration
}
}

func WithConnType(connType string) RelayConnOption {
return func(rci *relayConnImpl) {
rci.ConnType = connType
Expand All @@ -93,6 +86,7 @@ func WithConnType(connType string) RelayConnOption {
func WithRemote(remote *lb.Node) RelayConnOption {
return func(rci *relayConnImpl) {
rci.remote = remote
rci.Stats.HandShakeLatency = remote.HandShakeDuration
}
}

Expand Down
8 changes: 0 additions & 8 deletions internal/transporter/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ func newBaseRelayServer(cfg *conf.Config, cmgr cmgr.Cmgr) (*BaseRelayServer, err
}

func (b *BaseRelayServer) RelayTCPConn(ctx context.Context, c net.Conn, remote *lb.Node) error {
if remote == nil {
remote = b.remotes.Next().Clone()
}
metrics.CurConnectionCount.WithLabelValues(remote.Label, metrics.METRIC_CONN_TYPE_TCP).Inc()
defer metrics.CurConnectionCount.WithLabelValues(remote.Label, metrics.METRIC_CONN_TYPE_TCP).Dec()

Expand All @@ -66,15 +63,11 @@ func (b *BaseRelayServer) RelayTCPConn(ctx context.Context, c net.Conn, remote *
return fmt.Errorf("handshake error: %w", err)
}
defer rc.Close()

b.l.Infof("RelayTCPConn from %s to %s", c.LocalAddr(), remote.Address)
return b.handleRelayConn(c, rc, remote, metrics.METRIC_CONN_TYPE_TCP)
}

func (b *BaseRelayServer) RelayUDPConn(ctx context.Context, c net.Conn, remote *lb.Node) error {
if remote == nil {
remote = b.remotes.Next().Clone()
}
metrics.CurConnectionCount.WithLabelValues(remote.Label, metrics.METRIC_CONN_TYPE_UDP).Inc()
defer metrics.CurConnectionCount.WithLabelValues(remote.Label, metrics.METRIC_CONN_TYPE_UDP).Dec()

Expand Down Expand Up @@ -144,7 +137,6 @@ func (b *BaseRelayServer) handleRelayConn(c, rc net.Conn, remote *lb.Node, connT
conn.WithConnType(connType),
conn.WithRelayLabel(b.cfg.Label),
conn.WithRelayOptions(b.cfg.Options),
conn.WithHandshakeDuration(remote.HandShakeDuration),
}
relayConn := conn.NewRelayConn(c, rc, opts...)
if b.cmgr != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/transporter/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *RawServer) ListenAndServe(ctx context.Context) error {
}
go func(c net.Conn) {
defer c.Close()
if err := s.RelayTCPConn(ctx, c, nil); err != nil {
if err := s.RelayTCPConn(ctx, c, s.remotes.Next()); err != nil {
s.l.Errorf("RelayTCPConn meet error: %s", err.Error())
}
}(c)
Expand All @@ -118,7 +118,7 @@ func (s *RawServer) listenUDP(ctx context.Context) error {
return err
}
go func() {
if err := s.RelayUDPConn(ctx, c, nil); err != nil {
if err := s.RelayUDPConn(ctx, c, s.remotes.Next()); err != nil {
s.l.Errorf("RelayUDPConn meet error: %s", err.Error())
}
}()
Expand Down
2 changes: 2 additions & 0 deletions internal/transporter/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ func (s *WsServer) handleRequest(w http.ResponseWriter, req *http.Request) {
var remote *lb.Node
if addr := req.URL.Query().Get(conf.WS_QUERY_REMOTE_ADDR); addr != "" {
remote = &lb.Node{Address: addr, Label: addr}
} else {
remote = s.remotes.Next()
}

if req.URL.Query().Get("type") == "udp" {
Expand Down
Loading

0 comments on commit 22b6586

Please sign in to comment.