From b2872189409f32b537c58103c295f84d515d8ddb Mon Sep 17 00:00:00 2001 From: Xiaochao Dong Date: Thu, 4 May 2023 15:27:51 +0800 Subject: [PATCH] Avoid expensive log.Valuer evaluation for disallowed levels (#6322) Signed-off-by: Xiaochao Dong (@damnever) --- CHANGELOG.md | 1 + pkg/logging/logger.go | 5 ++++- pkg/logging/logger_test.go | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 pkg/logging/logger_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index a08c755f21f..a95be6c8805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pkg/logging/logger.go b/pkg/logging/logger.go index b711607b479..cca8853da58 100644 --- a/pkg/logging/logger.go +++ b/pkg/logging/logger.go @@ -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 } diff --git a/pkg/logging/logger_test.go b/pkg/logging/logger_test.go new file mode 100644 index 00000000000..2e4eb3f73fa --- /dev/null +++ b/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) + } +}