Skip to content

Commit

Permalink
Convert lib/teleagent to use slog (#50303)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy authored Dec 17, 2024
1 parent b8ffebd commit 04a20a7
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/teleagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@
package teleagent

import (
"context"
"errors"
"io"
"log/slog"
"net"
"os"
"os/user"
"path/filepath"
"time"

"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/agent"

"github.com/gravitational/teleport/lib/utils"
logutils "github.com/gravitational/teleport/lib/utils/log"
)

// Agent extends the agent.ExtendedAgent interface.
Expand Down Expand Up @@ -103,6 +105,8 @@ func (a *AgentServer) Serve() error {
if a.listener == nil {
return trace.BadParameter("Serve needs a Listen call first")
}

ctx := context.Background()
var tempDelay time.Duration // how long to sleep on accept failure
for {
conn, err := a.listener.Accept()
Expand All @@ -115,7 +119,7 @@ func (a *AgentServer) Serve() error {
return nil
}
if !neterr.Timeout() {
log.WithError(err).Error("Got non-timeout error.")
slog.ErrorContext(ctx, "Got non-timeout error", "error", err)
return trace.Wrap(err)
}
if tempDelay == 0 {
Expand All @@ -126,7 +130,7 @@ func (a *AgentServer) Serve() error {
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
log.WithError(err).Errorf("Got timeout error (will sleep %v).", tempDelay)
slog.ErrorContext(ctx, "Got timeout error - backing off", "delay_time", tempDelay, "error", err)
time.Sleep(tempDelay)
continue
}
Expand All @@ -135,19 +139,16 @@ func (a *AgentServer) Serve() error {
// get an agent instance for serving this conn
instance, err := a.getAgent()
if err != nil {
log.WithError(err).Error("Failed to get agent.")
slog.ErrorContext(ctx, "Failed to get agent", "error", err)
return trace.Wrap(err)
}

// serve agent protocol against conn in a
// separate goroutine.
go func() {
defer instance.Close()
//nolint:staticcheck // SA4023. ServeAgent always returns a non-nil error. This is fine.
if err := agent.ServeAgent(instance, conn); err != nil {
if !errors.Is(err, io.EOF) {
log.Error(err)
}
if err := agent.ServeAgent(instance, conn); err != nil && !errors.Is(err, io.EOF) {
slog.ErrorContext(ctx, "Serving agent terminated unexpectedly", "error", err)
}
}()
}
Expand All @@ -167,7 +168,9 @@ func (a *AgentServer) ListenAndServe(addr utils.NetAddr) error {
func (a *AgentServer) Close() error {
var errors []error
if a.listener != nil {
log.Debugf("AgentServer(%v) is closing", a.listener.Addr())
slog.DebugContext(context.Background(), "AgentServer is closing",
"listen_addr", logutils.StringerAttr(a.listener.Addr()),
)
if err := a.listener.Close(); err != nil {
errors = append(errors, trace.ConvertSystemError(err))
}
Expand Down

0 comments on commit 04a20a7

Please sign in to comment.