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

Convert lib/teleagent to use slog #50303

Merged
merged 1 commit into from
Dec 17, 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
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
Loading