From af3aac6b459791fabd8ebc306cdff47c609fbf53 Mon Sep 17 00:00:00 2001 From: Igor Drozdov Date: Wed, 15 Sep 2021 20:46:00 +0300 Subject: Add context fields to logging It adds correlation ids wherever possible --- cmd/gitlab-sshd/main.go | 2 +- internal/command/shared/customaction/customaction.go | 2 +- internal/handler/exec.go | 2 +- internal/sshd/connection.go | 2 +- internal/sshd/server_config.go | 4 ++-- internal/sshd/sshd.go | 14 +++++++------- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cmd/gitlab-sshd/main.go b/cmd/gitlab-sshd/main.go index 5bbf221..165c7a5 100644 --- a/cmd/gitlab-sshd/main.go +++ b/cmd/gitlab-sshd/main.go @@ -97,7 +97,7 @@ func main() { sig := <-done signal.Reset(syscall.SIGINT, syscall.SIGTERM) - log.WithFields(log.Fields{"shutdown_timeout_s": cfg.Server.GracePeriodSeconds, "signal": sig.String()}).Info("Shutdown initiated") + log.WithContextFields(ctx, log.Fields{"shutdown_timeout_s": cfg.Server.GracePeriodSeconds, "signal": sig.String()}).Info("Shutdown initiated") server.Shutdown() diff --git a/internal/command/shared/customaction/customaction.go b/internal/command/shared/customaction/customaction.go index 34086fb..73d2ce4 100644 --- a/internal/command/shared/customaction/customaction.go +++ b/internal/command/shared/customaction/customaction.go @@ -64,7 +64,7 @@ func (c *Command) processApiEndpoints(ctx context.Context, response *accessverif "endpoint": endpoint, } - log.WithFields(fields).Info("Performing custom action") + log.WithContextFields(ctx, fields).Info("Performing custom action") response, err := c.performRequest(ctx, client, endpoint, request) if err != nil { diff --git a/internal/handler/exec.go b/internal/handler/exec.go index 27031b1..eb5211e 100644 --- a/internal/handler/exec.go +++ b/internal/handler/exec.go @@ -110,7 +110,7 @@ func getConn(ctx context.Context, gc *GitalyCommand) (*grpc.ClientConn, error) { if serviceName == "" { serviceName = "gitlab-shell-unknown" - log.WithFields(log.Fields{"service_name": serviceName}).Warn("No gRPC service name specified, defaulting to gitlab-shell-unknown") + log.WithContextFields(ctx, log.Fields{"service_name": serviceName}).Warn("No gRPC service name specified, defaulting to gitlab-shell-unknown") } serviceName = fmt.Sprintf("%s-%s", serviceName, gc.ServiceName) diff --git a/internal/sshd/connection.go b/internal/sshd/connection.go index 0e0da93..1d91a6c 100644 --- a/internal/sshd/connection.go +++ b/internal/sshd/connection.go @@ -54,7 +54,7 @@ func (c *connection) handle(ctx context.Context, chans <-chan ssh.NewChannel, ha // Prevent a panic in a single session from taking out the whole server defer func() { if err := recover(); err != nil { - log.WithFields(log.Fields{"recovered_error": err}).Warnf("panic handling session from %s", c.remoteAddr) + log.WithContextFields(ctx, log.Fields{"recovered_error": err, "address": c.remoteAddr}).Warn("panic handling session") } }() diff --git a/internal/sshd/server_config.go b/internal/sshd/server_config.go index 7306944..68210f8 100644 --- a/internal/sshd/server_config.go +++ b/internal/sshd/server_config.go @@ -32,12 +32,12 @@ func newServerConfig(cfg *config.Config) (*serverConfig, error) { for _, filename := range cfg.Server.HostKeyFiles { keyRaw, err := os.ReadFile(filename) if err != nil { - log.WithError(err).Warnf("Failed to read host key %v", filename) + log.WithError(err).WithFields(log.Fields{"filename": filename}).Warn("Failed to read host key") continue } key, err := ssh.ParsePrivateKey(keyRaw) if err != nil { - log.WithError(err).Warnf("Failed to parse host key %v", filename) + log.WithError(err).WithFields(log.Fields{"filename": filename}).Warn("Failed to parse host key") continue } diff --git a/internal/sshd/sshd.go b/internal/sshd/sshd.go index ff9e765..92a9c2b 100644 --- a/internal/sshd/sshd.go +++ b/internal/sshd/sshd.go @@ -47,7 +47,7 @@ func NewServer(cfg *config.Config) (*Server, error) { } func (s *Server) ListenAndServe(ctx context.Context) error { - if err := s.listen(); err != nil { + if err := s.listen(ctx); err != nil { return err } defer s.listener.Close() @@ -85,7 +85,7 @@ func (s *Server) MonitoringServeMux() *http.ServeMux { return mux } -func (s *Server) listen() error { +func (s *Server) listen(ctx context.Context) error { sshListener, err := net.Listen("tcp", s.Config.Server.Listen) if err != nil { return fmt.Errorf("failed to listen for connection: %w", err) @@ -97,10 +97,10 @@ func (s *Server) listen() error { ReadHeaderTimeout: ProxyHeaderTimeout, } - log.Info("Proxy protocol is enabled") + log.ContextLogger(ctx).Info("Proxy protocol is enabled") } - log.WithFields(log.Fields{"tcp_address": sshListener.Addr().String()}).Info("Listening for SSH connections") + log.WithContextFields(ctx, log.Fields{"tcp_address": sshListener.Addr().String()}).Info("Listening for SSH connections") s.listener = sshListener @@ -117,7 +117,7 @@ func (s *Server) serve(ctx context.Context) { break } - log.WithError(err).Warn("Failed to accept connection") + log.ContextLogger(ctx).WithError(err).Warn("Failed to accept connection") continue } @@ -152,7 +152,7 @@ func (s *Server) handleConn(ctx context.Context, nconn net.Conn) { // Prevent a panic in a single connection from taking out the whole server defer func() { if err := recover(); err != nil { - log.WithFields(log.Fields{"recovered_error": err}).Warnf("panic handling session from %s", remoteAddr) + log.WithContextFields(ctx, log.Fields{"recovered_error": err, "address": remoteAddr}).Warn("panic handling session") } }() @@ -161,7 +161,7 @@ func (s *Server) handleConn(ctx context.Context, nconn net.Conn) { sconn, chans, reqs, err := ssh.NewServerConn(nconn, s.serverConfig.get(ctx)) if err != nil { - log.WithError(err).Info("Failed to initialize SSH connection") + log.ContextLogger(ctx).WithError(err).Info("Failed to initialize SSH connection") return } -- cgit v1.2.1