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

Logging: expose log level through log wrappers #10604

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
27 changes: 27 additions & 0 deletions pkg/util/log/slogadapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log/slog"
"testing"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
dslog "github.com/grafana/dskit/log"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -90,3 +91,29 @@ func TestSlogFromGoKit(t *testing.T) {
}
})
}

func BenchmarkSlogFromGoKit(b *testing.B) {
// Set up a logger with a stack of wrappers, similar to what tsdb gets.
var lvl dslog.Level
lvl.Set("info")
var logger log.Logger
logger = noopLogger{}
logger = newFilter(logger, lvl)
logger = WithUserID("userID", logger)

sl := SlogFromGoKit(logger)
b.Run("log", func(b *testing.B) {
for i := 0; i < b.N; i++ {
sl.Info("msg", "foo", "bar", "more", "data")
}
})
b.Run("debuglog", func(b *testing.B) {
for i := 0; i < b.N; i++ {
sl.Debug("msg", "foo", "bar", "more", "data")
}
})
}

type noopLogger struct{}

func (noopLogger) Log(...interface{}) error { return nil }
26 changes: 22 additions & 4 deletions pkg/util/log/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,42 @@ import (
"github.com/grafana/dskit/tracing"
)

// levelWrapper implements leveledLogger and log.Logger.
type levelWrapper struct {
lvl logLevel
log.Logger // Calls to Log() will fall through to here.
}

func wrap(original, logger log.Logger) log.Logger {
lvl := debugLevel // default if unknown
if x, ok := original.(leveledLogger); ok {
lvl = x.level()
}
return levelWrapper{lvl: lvl, Logger: logger}
}

func (w levelWrapper) level() logLevel {
return w.lvl
}

// WithUserID returns a Logger that has information about the current user in
// its details.
func WithUserID(userID string, l log.Logger) log.Logger {
// See note in WithContext.
return log.With(l, "user", userID)
return wrap(l, log.With(l, "user", userID))
}

// WithUserIDs returns a Logger that has information about the current user or
// users (separated by "|") in its details.
func WithUserIDs(userIDs []string, l log.Logger) log.Logger {
return log.With(l, "user", tenant.JoinTenantIDs(userIDs))
return wrap(l, log.With(l, "user", tenant.JoinTenantIDs(userIDs)))
}

// WithTraceID returns a Logger that has information about the traceID in
// its details.
func WithTraceID(traceID string, l log.Logger) log.Logger {
// See note in WithContext.
return log.With(l, "traceID", traceID)
return wrap(l, log.With(l, "traceID", traceID))
}

// WithContext returns a Logger that has information about the current user or users
Expand Down Expand Up @@ -60,5 +78,5 @@ func WithContext(ctx context.Context, l log.Logger) log.Logger {
// WithSourceIPs returns a Logger that has information about the source IPs in
// its details.
func WithSourceIPs(sourceIPs string, l log.Logger) log.Logger {
return log.With(l, "sourceIPs", sourceIPs)
return wrap(l, log.With(l, "sourceIPs", sourceIPs))
}
Loading