Skip to content

Commit

Permalink
Avoid expensive log.Valuer evaluation for disallowed levels (thanos-i…
Browse files Browse the repository at this point in the history
…o#6322)

Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
  • Loading branch information
damnever authored and GiedriusS committed May 17, 2023
1 parent df86e81 commit 3327e8f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/logging/logger.go
Expand Up @@ -46,11 +46,14 @@ func NewLogger(logLevel, logFormat, debugName string) log.Logger {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
}

// Sort the logger chain to avoid expensive log.Valuer evaluation for disallowed level.
// Ref: https://github.com/go-kit/log/issues/14#issuecomment-945038252
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.Caller(5))
logger = level.NewFilter(logger, lvl)

if debugName != "" {
logger = log.With(logger, "name", debugName)
}

return log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return logger
}
19 changes: 19 additions & 0 deletions pkg/logging/logger_test.go
@@ -0,0 +1,19 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package logging

import (
"testing"

"github.com/go-kit/log/level"
)

func BenchmarkDisallowedLogLevels(b *testing.B) {
logger := NewLogger("warn", "logfmt", "benchmark")

for i := 0; i < b.N; i++ {
level.Info(logger).Log("hello", "world", "number", i)
level.Debug(logger).Log("hello", "world", "number", i)
}
}

0 comments on commit 3327e8f

Please sign in to comment.