Skip to content

Commit

Permalink
Tweaks to various sloglint overrides (#51522) (#51546)
Browse files Browse the repository at this point in the history
This aims to either alter logs to avoid the linter override, or at
minimum prevent formatting of messages if the log level is not
enabled.
  • Loading branch information
rosstimothy authored Jan 30, 2025
1 parent f6bec11 commit 8d2b615
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
7 changes: 5 additions & 2 deletions lib/integrations/awsoidc/eks_enroll_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,11 @@ func getHelmActionConfig(ctx context.Context, clientGetter genericclioptions.RES
// > func(format string, v ...interface{})
// slog.Log does not support it, so it must be added
debugLogWithFormat := func(format string, v ...interface{}) {
formatString := fmt.Sprintf(format, v...)
log.DebugContext(ctx, formatString) //nolint:sloglint // message should be a constant but in this case we are creating it at runtime.
if !log.Handler().Enabled(ctx, slog.LevelDebug) {
return
}
//nolint:sloglint // message should be a constant but in this case we are creating it at runtime.
log.DebugContext(ctx, fmt.Sprintf(format, v...))
}
if err := actionConfig.Init(clientGetter, agentNamespace, "secret", debugLogWithFormat); err != nil {
return nil, trace.Wrap(err)
Expand Down
12 changes: 12 additions & 0 deletions lib/service/pyroscope.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package service

import (
"context"
"fmt"
"log/slog"
"os"
Expand All @@ -33,16 +34,27 @@ type pyroscopeLogger struct {
}

func (l pyroscopeLogger) Infof(format string, args ...interface{}) {
if !l.l.Handler().Enabled(context.Background(), slog.LevelInfo) {
return
}
//nolint:sloglint // msg cannot be constant
l.l.Info(fmt.Sprintf(format, args...))
}

func (l pyroscopeLogger) Debugf(format string, args ...interface{}) {
if !l.l.Handler().Enabled(context.Background(), slog.LevelDebug) {
return
}

//nolint:sloglint // msg cannot be constant
l.l.Debug(fmt.Sprintf(format, args...))
}

func (l pyroscopeLogger) Errorf(format string, args ...interface{}) {
if !l.l.Handler().Enabled(context.Background(), slog.LevelError) {
return
}

//nolint:sloglint // msg cannot be constant
l.l.Error(fmt.Sprintf(format, args...))
}
Expand Down
4 changes: 4 additions & 0 deletions lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3292,6 +3292,10 @@ type promHTTPLogAdapter struct {

// Println implements the promhttp.Logger interface.
func (l promHTTPLogAdapter) Println(v ...interface{}) {
if !l.Handler().Enabled(l.ctx, slog.LevelError) {
return
}

//nolint:sloglint // msg cannot be constant
l.ErrorContext(l.ctx, fmt.Sprint(v...))
}
Expand Down
48 changes: 45 additions & 3 deletions tool/tbot/spiffe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,64 @@ package main
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/gravitational/trace"
"github.com/spiffe/go-spiffe/v2/svid/jwtsvid"
"github.com/spiffe/go-spiffe/v2/workloadapi"

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

// TODO(tross/noah): Remove once go-spiff has a slog<->workloadapi.Logger adapter.
// https://github.com/spiffe/go-spiffe/issues/281
type logger struct {
l *slog.Logger
}

func (l logger) Debugf(format string, args ...interface{}) {
if !l.l.Handler().Enabled(context.Background(), slog.LevelDebug) {
return
}

//nolint:sloglint // msg cannot be constant
l.l.DebugContext(context.Background(), fmt.Sprintf(format, args...))
}

func (l logger) Infof(format string, args ...interface{}) {
if !l.l.Handler().Enabled(context.Background(), slog.LevelInfo) {
return
}

//nolint:sloglint // msg cannot be constant
l.l.InfoContext(context.Background(), fmt.Sprintf(format, args...))
}

func (l logger) Warnf(format string, args ...interface{}) {
if !l.l.Handler().Enabled(context.Background(), slog.LevelWarn) {
return
}

//nolint:sloglint // msg cannot be constant
l.l.WarnContext(context.Background(), fmt.Sprintf(format, args...))
}

func (l logger) Errorf(format string, args ...interface{}) {
if !l.l.Handler().Enabled(context.Background(), slog.LevelError) {
return
}

//nolint:sloglint // msg cannot be constant
l.l.ErrorContext(context.Background(), fmt.Sprintf(format, args...))
}

func onSPIFFEInspect(ctx context.Context, path string) error {
log.InfoContext(ctx, "Inspecting SPIFFE Workload API Endpoint", "path", path)

source, err := workloadapi.New(
ctx,
// TODO(noah): Upstream PR to add slog<->workloadapi.Logger adapter.
workloadapi.WithLogger(utils.NewLogger()),
// https://github.com/spiffe/go-spiffe/issues/281
workloadapi.WithLogger(logger{l: slog.Default()}),
workloadapi.WithAddr(path),
)
if err != nil {
Expand Down

0 comments on commit 8d2b615

Please sign in to comment.