Skip to content

Commit

Permalink
Avoid expensive log.Valuer evaluation for disallowed levels (#6322)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaochao Dong (@damnever) <the.xcdong@gmail.com>
  • Loading branch information
damnever committed May 4, 2023
1 parent 1d08ebf commit 7ba2f2b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -42,6 +42,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#6071](https://github.com/thanos-io/thanos/pull/6071) Query Frontend: *breaking :warning:* Add experimental native histogram support for which we updated and aligned with the [Prometheus common](https://github.com/prometheus/common) model, which is used for caching so a cache reset required.
- [#6163](https://github.com/thanos-io/thanos/pull/6163) Receiver: changed max backoff from 30s to 5s for forwarding requests. Can be configured with `--receive-forward-max-backoff`.
- [#6327](https://github.com/thanos-io/thanos/pull/6327) *: *breaking :warning:* Use histograms instead of summaries for instrumented handlers.
- [#6322](https://github.com/thanos-io/thanos/pull/6322) Logging: Avoid expensive log.Valuer evaluation for disallowed levels.

### Removed

Expand Down
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 7ba2f2b

Please sign in to comment.