Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log remote addr, not local addr #19

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading