forked from uber-go/zap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce allocations for disabled/sampled logs (uber-go#45)
* Reduce allocations for disabled/sampled logs Even when a log level is disabled or heavily sampled, calling `logger.Error(msg, someField, someOtherField)` will make at least one allocation to hold the fields. If we're using the more expensive field types, we may allocate even more, only to discard all that work. This PR introduces the `logger.Check` API, which allows particularly performance-sensitive applications to avoid these allocations by using a slightly wordier call pattern. Using `Check` makes it virtually free to pass fields to a sampled logger: ``` BenchmarkZapDisabledLevelsAddingFields-4 3000000 439 ns/op 704 B/op 2 allocs/op BenchmarkZapDisabledLevelsCheckAddingFields-4 200000000 5.74 ns/op 0 B/op 0 allocs/op BenchmarkZapSampleWithoutFields-4 20000000 60.9 ns/op 0 B/op 0 allocs/op BenchmarkZapSampleCheckWithoutFields-4 20000000 61.4 ns/op 0 B/op 0 allocs/op BenchmarkZapSampleAddingFields-4 2000000 575 ns/op 704 B/op 2 allocs/op BenchmarkZapSampleCheckAddingFields-4 20000000 65.2 ns/op 0 B/op 0 allocs/op ``` * Improve CheckedMessage ergonomics * Don't log on CheckedMessage re-use
- Loading branch information
1 parent
4f8e68a
commit 1392e24
Showing
9 changed files
with
265 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) 2016 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package zap | ||
|
||
import "sync/atomic" | ||
|
||
// A CheckedMessage is the result of a call to Logger.Check, which allows | ||
// especially performance-sensitive applications to avoid allocations for disabled | ||
// or heavily sampled log levels. | ||
type CheckedMessage struct { | ||
logger Logger | ||
used uint32 | ||
lvl Level | ||
msg string | ||
} | ||
|
||
// NewCheckedMessage constructs a CheckedMessage. It's only intended for use by | ||
// wrapper libraries, and shouldn't be necessary in application code. | ||
func NewCheckedMessage(logger Logger, lvl Level, msg string) *CheckedMessage { | ||
return &CheckedMessage{ | ||
logger: logger, | ||
lvl: lvl, | ||
msg: msg, | ||
} | ||
} | ||
|
||
// Write logs the pre-checked message with the supplied fields. It should only | ||
// be used once; if a CheckedMessage is re-used, it also logs an error message | ||
// with the underlying logger's DFatal method. | ||
func (m *CheckedMessage) Write(fields ...Field) { | ||
if n := atomic.AddUint32(&m.used, 1); n > 1 { | ||
if n == 2 { | ||
// Log an error on the first re-use. After that, skip the I/O and | ||
// allocations and just return. | ||
m.logger.DFatal("Shouldn't re-use a CheckedMessage.", String("original", m.msg)) | ||
} | ||
return | ||
} | ||
m.logger.Log(m.lvl, m.msg, fields...) | ||
} | ||
|
||
// OK checks whether it's safe to call Write. | ||
func (m *CheckedMessage) OK() bool { | ||
return m != nil | ||
} |
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,58 @@ | ||
// Copyright (c) 2016 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package zap | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestJSONLoggerCheck(t *testing.T) { | ||
withJSONLogger(t, opts(Info), func(jl *jsonLogger, output func() []string) { | ||
assert.False(t, jl.Check(Debug, "Debug.").OK(), "Expected CheckedMessage to be not OK at disabled levels.") | ||
|
||
cm := jl.Check(Info, "Info.") | ||
require.True(t, cm.OK(), "Expected CheckedMessage to be OK at enabled levels.") | ||
cm.Write(Int("magic", 42)) | ||
assert.Equal( | ||
t, | ||
`{"msg":"Info.","level":"info","ts":0,"fields":{"magic":42}}`, | ||
output()[0], | ||
"Unexpected output after writing a CheckedMessage.", | ||
) | ||
}) | ||
} | ||
|
||
func TestCheckedMessageIsSingleUse(t *testing.T) { | ||
expected := []string{ | ||
`{"msg":"Single-use.","level":"info","ts":0,"fields":{}}`, | ||
`{"msg":"Shouldn't re-use a CheckedMessage.","level":"error","ts":0,"fields":{"original":"Single-use."}}`, | ||
} | ||
withJSONLogger(t, nil, func(jl *jsonLogger, output func() []string) { | ||
cm := jl.Check(Info, "Single-use.") | ||
cm.Write() // ok | ||
cm.Write() // first re-use logs error | ||
cm.Write() // second re-use is silently ignored | ||
assert.Equal(t, expected, output(), "Expected re-using a CheckedMessage to log an error.") | ||
}) | ||
} |
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
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