Skip to content

Commit

Permalink
Add connection information to multiplexer logs so it's easier to inve…
Browse files Browse the repository at this point in the history
…stigate (#32712)
  • Loading branch information
AntonAM authored Sep 28, 2023
1 parent 4ff4eb3 commit 4def17e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
15 changes: 12 additions & 3 deletions lib/multiplexer/multiplexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ func (m *Mux) detectAndForward(conn net.Conn) {
connWrapper, err := m.detect(conn)
if err != nil {
if trace.Unwrap(err) != io.EOF {
m.logLimiter.Log(m.Entry, log.WarnLevel, trace.DebugReport(err))
m.logLimiter.Log(m.Entry.WithFields(log.Fields{
"src_addr": conn.RemoteAddr(),
"dst_addr": conn.LocalAddr(),
}), log.WarnLevel, trace.DebugReport(err))
}
conn.Close()
return
Expand All @@ -302,9 +305,15 @@ func (m *Mux) detectAndForward(conn net.Conn) {
listener := m.protocolListener(connWrapper.protocol)
if listener == nil {
if connWrapper.protocol == ProtoHTTP {
m.Debug("Detected an HTTP request. If this is for a health check, use an HTTPS request instead.")
m.WithFields(log.Fields{
"src_addr": connWrapper.RemoteAddr(),
"dst_addr": connWrapper.LocalAddr(),
}).Debug("Detected an HTTP request. If this is for a health check, use an HTTPS request instead.")
}
m.Debugf("Closing %[1]s connection: %[1]s listener is disabled.", connWrapper.protocol)
m.WithFields(log.Fields{
"src_addr": connWrapper.RemoteAddr(),
"dst_addr": connWrapper.LocalAddr(),
}).Debugf("Closing %[1]s connection: %[1]s listener is disabled.", connWrapper.protocol)
connWrapper.Close()
return
}
Expand Down
15 changes: 12 additions & 3 deletions lib/multiplexer/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ func (l *TLSListener) Serve() error {
tlsConn, ok := conn.(*tls.Conn)
if !ok {
conn.Close()
log.Errorf("Expected tls.Conn, got %T, internal usage error.", conn)
l.log.WithFields(log.Fields{
"src_addr": conn.RemoteAddr(),
"dst_addr": conn.LocalAddr(),
}).Errorf("Expected tls.Conn, got %T, internal usage error.", conn)
continue
}
go l.detectAndForward(tlsConn)
Expand Down Expand Up @@ -141,7 +144,10 @@ func (l *TLSListener) detectAndForward(conn *tls.Conn) {
start := l.cfg.Clock.Now()
if err := conn.Handshake(); err != nil {
if trace.Unwrap(err) != io.EOF {
l.log.WithError(err).Warning("Handshake failed.")
l.log.WithFields(log.Fields{
"src_addr": conn.RemoteAddr(),
"dst_addr": conn.LocalAddr(),
}).WithError(err).Warning("Handshake failed.")
}
conn.Close()
return
Expand All @@ -167,7 +173,10 @@ func (l *TLSListener) detectAndForward(conn *tls.Conn) {
l.httpListener.HandleConnection(l.context, conn)
default:
conn.Close()
l.log.WithError(err).Errorf("unsupported protocol: %v", conn.ConnectionState().NegotiatedProtocol)
l.log.WithFields(log.Fields{
"src_addr": conn.RemoteAddr(),
"dst_addr": conn.LocalAddr(),
}).WithError(err).Errorf("unsupported protocol: %v", conn.ConnectionState().NegotiatedProtocol)
}
}

Expand Down
15 changes: 12 additions & 3 deletions lib/multiplexer/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ func (l *WebListener) Serve() error {

tlsConn, ok := conn.(*tls.Conn)
if !ok {
l.log.Errorf("Expected *tls.Conn, got %T.", conn)
l.log.WithFields(logrus.Fields{
"src_addr": conn.RemoteAddr(),
"dst_addr": conn.LocalAddr(),
}).Errorf("Expected *tls.Conn, got %T.", conn)
conn.Close()
continue
}
Expand All @@ -132,7 +135,10 @@ func (l *WebListener) detectAndForward(conn *tls.Conn) {

if err := conn.Handshake(); err != nil {
if trace.Unwrap(err) != io.EOF {
l.log.WithError(err).Warn("Handshake failed.")
l.log.WithFields(logrus.Fields{
"src_addr": conn.RemoteAddr(),
"dst_addr": conn.LocalAddr(),
}).WithError(err).Warn("Handshake failed.")
}
conn.Close()
return
Expand All @@ -151,7 +157,10 @@ func (l *WebListener) detectAndForward(conn *tls.Conn) {
// tls listener.
isDatabaseConnection, err := dbcommon.IsDatabaseConnection(conn.ConnectionState())
if err != nil {
l.log.WithError(err).Debug("Failed to check if connection is database connection.")
l.log.WithFields(logrus.Fields{
"src_addr": conn.RemoteAddr(),
"dst_addr": conn.LocalAddr(),
}).WithError(err).Debug("Failed to check if connection is database connection.")
}
if isDatabaseConnection {
l.dbListener.HandleConnection(l.context, conn)
Expand Down

0 comments on commit 4def17e

Please sign in to comment.