Skip to content

Commit

Permalink
Log remote addr, not local addr
Browse files Browse the repository at this point in the history
The intention here was to log the remote address, logging the local
address is just the address the server is listening on.

Also caches it in the clientData so we don't have to keep converting it
to a string. It won't change.
  • Loading branch information
mattrobenolt committed Feb 12, 2024
1 parent 628bba8 commit 474d842
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,44 @@ func (h *handler) testCredentials(timeout time.Duration) error {
}

func (h *handler) NewConnection(c *mysql.Conn) {
data := &clientData{
start: time.Now(),
remoteAddr: c.RemoteAddr().String(),
}
h.connectionsMu.Lock()
h.connections[c] = &clientData{start: time.Now()}
h.connections[c] = data
h.connectionsMu.Unlock()

h.logger.LogAttrs(
context.Background(),
slog.LevelDebug,
"new connection",
slog.String("addr", c.GetRawConn().LocalAddr().String()),
slog.String("addr", data.remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
)
}

func (h *handler) ConnectionClosed(c *mysql.Conn) {
h.connectionsMu.Lock()
start := h.connections[c].start
remoteAddr := h.connections[c].remoteAddr
delete(h.connections, c)
h.connectionsMu.Unlock()

h.logger.LogAttrs(
context.Background(),
slog.LevelDebug,
"connection closed",
slog.String("addr", remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
slog.Duration("duration", time.Since(start)),
)
}

type clientData struct {
start time.Time
Session *psdbpb.Session
start time.Time
remoteAddr string
Session *psdbpb.Session
}

func (d *clientData) IsOLAP() bool {
Expand All @@ -123,7 +130,7 @@ func (h *handler) ComQuery(c *mysql.Conn, query string, callback func(*sqltypes.
context.Background(),
slog.LevelDebug,
"execute",
slog.String("addr", c.GetRawConn().LocalAddr().String()),
slog.String("addr", data.remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
slog.String("query", query),
slog.Bool("olap", data.IsOLAP()),
Expand Down Expand Up @@ -162,7 +169,7 @@ func (h *handler) ComPrepare(c *mysql.Conn, query string, bindVars map[string]*v
context.Background(),
slog.LevelDebug,
"prepare",
slog.String("addr", c.GetRawConn().LocalAddr().String()),
slog.String("addr", data.remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
slog.String("query", query),
)
Expand Down Expand Up @@ -194,7 +201,7 @@ func (h *handler) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, call
context.Background(),
slog.LevelDebug,
"stmt_execute",
slog.String("addr", c.GetRawConn().LocalAddr().String()),
slog.String("addr", data.remoteAddr),
slog.Int("mysql_id", int(c.ConnectionID)),
slog.String("query", prepare.PrepareStmt),
slog.Bool("olap", data.IsOLAP()),
Expand Down

0 comments on commit 474d842

Please sign in to comment.