-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a ZapMapLogger for logging without duplicate entries #94
Merged
Merged
Changes from 22 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
636d04a
Add ZapMap logger which stores params in a map to avoid duplicate fields
smoorpal 55943ea
add an assertion to json tests that the trimmed logEntry is equal to …
smoorpal 74a9ff8
add benchmarks to service logs
smoorpal 7945d96
add benchmark changes
smoorpal 8df1900
restructure to avoid inconsistencies in testing
smoorpal 59dbf36
use truncate to avoidn regularly allocating the buffer
smoorpal a37c0a7
use pointers for the map implementation
smoorpal 25bd69c
use Reset() instead of Truncate(0)
smoorpal eb47c08
revert go.mod to avoid pulling in additional vendor files and ready PR.
smoorpal cb23af3
deprecated comments for zerolog
smoorpal 98d4be3
remove previous implementation of parameters and make Zap Map impleme…
smoorpal 31ce52d
fix check in test
smoorpal cb2129f
manually track output log levels and always use zerolog.Log in order …
smoorpal d5e91ac
fix regex to work with forks
smoorpal d182bf4
do not test for duplicate safe params since this is effectively progr…
smoorpal 3ea8f20
remove comments that have been resolved
smoorpal 3359016
format
smoorpal 0e52c46
fix check
smoorpal 566a68e
add changelog
smoorpal 23cf081
remove deprecated
smoorpal 1676f0e
remove unnecessary additions to tests
smoorpal 7c9eaa6
remove erroneous
smoorpal 6ec6cb5
use a more specific regex
smoorpal c0b2cb9
check that later safe params overwrite original safe params with context
smoorpal 765c123
expand the contect_test to test for overwritten safe params from the …
smoorpal d9ede8e
implement string map and anymap values in zerolog since parameters ar…
smoorpal 96badca
remove specialized benchmark
smoorpal 7e50fa6
better context in error
smoorpal fb50648
use map[string]struct{} instead of int
smoorpal 705d29f
golint
smoorpal ab8ea66
add comments explaining specific implementation of AnyMapValues and S…
smoorpal ccf8a1a
update comment
smoorpal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: modify wlog-zap and wlog-zerolog to avoid printing duplicate fields in the base object. Duplicate fields results in ambiguous behavior when unmarshalling and can break certain json unmarshaler implementations | ||
links: | ||
- https://github.com/palantir/witchcraft-go-logging/pull/94 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/palantir/pkg/objmatcher" | ||
|
@@ -310,7 +311,61 @@ something/something:123`, | |
}), | ||
}), | ||
}, | ||
{ | ||
Name: "duplicate origin", | ||
Message: "this is a test", | ||
LogParams: []svc1log.Param{ | ||
svc1log.Origin("origin.0"), | ||
svc1log.Origin("origin.1"), | ||
svc1log.UID("user-1"), | ||
svc1log.SID("session-1"), | ||
svc1log.TraceID("X-Y-Z"), | ||
svc1log.SafeParams(map[string]interface{}{ | ||
"key": "value", | ||
"int": 10, | ||
}), | ||
svc1log.UnsafeParams(map[string]interface{}{ | ||
"Password": "HelloWorld!", | ||
}), | ||
svc1log.Tags(map[string]string{ | ||
"key1": "value1", | ||
"key2": "value2", | ||
}), | ||
}, | ||
JSONMatcher: objmatcher.MapMatcher(map[string]objmatcher.Matcher{ | ||
"origin": objmatcher.NewEqualsMatcher("origin.1"), | ||
"level": objmatcher.NewEqualsMatcher("INFO"), | ||
"time": objmatcher.NewRegExpMatcher(".+"), | ||
"type": objmatcher.NewEqualsMatcher("service.1"), | ||
"message": objmatcher.NewEqualsMatcher("this is a test"), | ||
"params": objmatcher.MapMatcher(map[string]objmatcher.Matcher{ | ||
"key": objmatcher.NewEqualsMatcher("value"), | ||
"int": objmatcher.NewEqualsMatcher(json.Number("10")), | ||
}), | ||
"uid": objmatcher.NewEqualsMatcher("user-1"), | ||
"sid": objmatcher.NewEqualsMatcher("session-1"), | ||
"traceId": objmatcher.NewEqualsMatcher("X-Y-Z"), | ||
"unsafeParams": objmatcher.MapMatcher(map[string]objmatcher.Matcher{ | ||
"Password": objmatcher.NewEqualsMatcher("HelloWorld!"), | ||
}), | ||
"tags": objmatcher.MapMatcher(map[string]objmatcher.Matcher{ | ||
"key1": objmatcher.NewEqualsMatcher("value1"), | ||
"key2": objmatcher.NewEqualsMatcher("value2"), | ||
}), | ||
}), | ||
}, | ||
} | ||
} | ||
|
||
func BenchmarkCases() []TestCase { | ||
testCases := TestCases() | ||
benchmarkCases := make([]TestCase, 0, len(testCases)) | ||
for _, testCase := range testCases { | ||
if testCase.Name != "duplicate origin" && testCase.Name != "parameter that is set manually overrides base value" { | ||
benchmarkCases = append(benchmarkCases, testCase) | ||
} | ||
} | ||
return benchmarkCases | ||
} | ||
|
||
func JSONTestSuite(t *testing.T, loggerProvider func(w io.Writer, level wlog.LogLevel, origin string) svc1log.Logger) { | ||
|
@@ -362,6 +417,9 @@ func jsonOutputTests(t *testing.T, loggerProvider func(w io.Writer, level wlog.L | |
logEntry := buf.Bytes() | ||
err := safejson.Unmarshal(logEntry, &gotServiceLog) | ||
require.NoError(t, err, "Case %d: %s\nService log line is not a valid map: %v", i, tc.Name, string(logEntry)) | ||
logEntryRewrite, err := safejson.Marshal(gotServiceLog) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This set of assertions parses the printed service log and rewrites it. If the bytes are not the same length then duplicate fields must have been printed. |
||
require.NoError(t, err, "Not able to marshal log line") | ||
assert.Equal(t, len(strings.TrimRight(string(logEntry), "\n")), len(string(logEntryRewrite)), "log line is not stable. Differing length on remarshal") | ||
|
||
assert.NoError(t, tc.JSONMatcher.Matches(gotServiceLog), "Case %d: %s", i, tc.Name) | ||
}) | ||
|
@@ -447,6 +505,25 @@ func extraParamsDoNotAppearTest(t *testing.T, loggerProvider func(w io.Writer, l | |
}) | ||
} | ||
|
||
func JSONBenchmarkSuite(b *testing.B, loggerProvider func(w io.Writer, level wlog.LogLevel, origin string) svc1log.Logger) { | ||
jsonOutputBenchmarks(b, loggerProvider) | ||
} | ||
|
||
func jsonOutputBenchmarks(b *testing.B, loggerProvider func(w io.Writer, level wlog.LogLevel, origin string) svc1log.Logger) { | ||
buf := bytes.Buffer{} | ||
for _, tc := range BenchmarkCases() { | ||
b.Run(tc.Name, func(b *testing.B) { | ||
logger := loggerProvider(&buf, wlog.DebugLevel, tc.Origin) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
buf.Reset() | ||
logger.Info(tc.Message, tc.LogParams...) | ||
assert.Greater(b, buf.Len(), 0) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// panics when marshaled as JSON | ||
type jsonMarshalPanicType struct{} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the duplicate safe param test because we are considering this a user error. This is the policy decided for Java. If a user adds duplicate safe params then their line will eventually not be parsed or result in a performance hit when falling back. Zerolog is difficult to enforce this and probably not worth it since you have to reverse all parameters being applied to it to catch the few bad actors.