-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging: do filter last, for improved performance (#3875)
* logging: add a benchmark for debug logging Since this is normally filtered out, we want low overhead. * logging: do filter last, for improved performance Previously the code would find out what line it was on and format the time, before deciding that the whole log line should be filtered out. Moving the filter to the end avoids most of this work. Add a test to validate that the filtering still works.
- Loading branch information
Showing
3 changed files
with
72 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
package log_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
gokitlog "github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/weaveworks/common/server" | ||
|
||
"github.com/grafana/mimir/pkg/util/log" | ||
) | ||
|
||
// Check that debug lines are correctly filtered out. | ||
func ExampleInitLogger() { | ||
// Kludge a couple of things so we can do tests repeatably. | ||
saveStderr := os.Stderr | ||
os.Stderr = os.Stdout | ||
saveTimestamp := gokitlog.DefaultTimestampUTC | ||
gokitlog.DefaultTimestampUTC = gokitlog.TimestampFormat( | ||
func() time.Time { return time.Unix(0, 0) }, | ||
time.RFC3339Nano, | ||
) | ||
|
||
cfg := server.Config{} | ||
_ = cfg.LogLevel.Set("info") | ||
log.InitLogger(&cfg) | ||
level.Info(log.Logger).Log("test", "1") | ||
level.Debug(log.Logger).Log("test", "2 - should not print") | ||
cfg.Log.Infof("test 3") | ||
cfg.Log.Debugf("test 4 - should not print") | ||
// Output: | ||
// ts=1970-01-01T00:00:00Z caller=log_test.go:31 level=info test=1 | ||
// ts=1970-01-01T00:00:00Z caller=log_test.go:33 level=info msg="test 3" | ||
|
||
os.Stderr = saveStderr | ||
gokitlog.DefaultTimestampUTC = saveTimestamp | ||
} | ||
|
||
// Check the overhead of debug logging which gets filtered out. | ||
func BenchmarkDebugLog(b *testing.B) { | ||
cfg := server.Config{} | ||
_ = cfg.LogLevel.Set("info") | ||
log.InitLogger(&cfg) | ||
b.ResetTimer() | ||
dl := level.Debug(log.Logger) | ||
for i := 0; i < b.N; i++ { | ||
dl.Log("something", "happened") | ||
} | ||
} |