-
Notifications
You must be signed in to change notification settings - Fork 824
Handle the error in SpanLogger.Log if log level is Error (#3016) #3995
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -77,6 +77,31 @@ func FromContextWithFallback(ctx context.Context, fallback log.Logger) *SpanLogg | |
// also puts the on the spans. | ||
func (s *SpanLogger) Log(kvps ...interface{}) error { | ||
s.Logger.Log(kvps...) | ||
|
||
var logAsError = false | ||
errorIndex := -1 | ||
for i := 0; i < len(kvps)-1; i += 2 { | ||
// Find out whether to log as error | ||
if kvps[i] == level.Key() { | ||
logAsError = kvps[i+1] == level.ErrorValue() | ||
if !logAsError { | ||
break | ||
} | ||
ext.Error.Set(s.Span, true) | ||
} else if errorIndex == -1 { | ||
// Check if this is the error we want to log | ||
if _, ok := kvps[i+1].(error); ok && (kvps[i] == "err" || kvps[i] == "error") { | ||
errorIndex = i | ||
} | ||
} | ||
if logAsError && errorIndex != -1 { | ||
s.Span.LogFields(otlog.Error(kvps[i+1].(error))) | ||
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. I think you need to use |
||
// Remove the already logged error | ||
kvps = append(kvps[:i], kvps[i+2:]...) | ||
break | ||
} | ||
} | ||
|
||
fields, err := otlog.InterleavedKVToFields(kvps...) | ||
if err != nil { | ||
return err | ||
|
This file contains hidden or 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
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.
Is it doable adding some valuable unit tests on the new logic you've added? Otherwise we'll need to manually test it before merging, cause the logic is a bit convoluted and there may be subtle bugs slipping it.
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.
Because I could not see a way to retrieve the field of SpanLogger, it's unlikely to test by comparing the value of the fields. If we refactor the new logic into a private function, I can write unit tests for it. However unit testing a private function doesn't sound like a good idea.
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.
@pracucci Do you have any further suggestion based on my previous comment? Thanks!
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.
How about using https://github.com/opentracing/opentracing-go/blob/master/mocktracer/mocktracer.go ?
(We unit-test private functions all the time, but I agree it's better if you can avoid)