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 check for InfoS & ErrorS parameters #229
Conversation
hack/tools/logcheck/main.go
Outdated
if len(keyValues)%2 != 0 { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: fun.Pos(), | ||
Message: fmt.Sprintf("Invalid Number of arguments for %s", funName), |
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 think we should not continue from here, no. of args are invalid because something is missed(key/value) in the log message,
e.g.
klog.V(2).InfoS("Starting container in a pod", "containerID", containerID, pod) Invalid Number of arguments for InfoS
here key is missed, it has value at that position. So no meaning of key type check, it will always fail.
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 agree, if number of arguments doesn't match, we cannot be sure about what is missing. It could be value at the end of arguments, but also key in the middle of them.
This makes analysis that assumes that keys are positioned in certain way hard to understand. In example provided by @adisky we would say tath pod
should be a string, which doesn't make sense.
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.
get, will fixed on next push, Thanks for advice~
hack/tools/logcheck/main.go
Outdated
if !isASCII { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: fun.Pos(), | ||
Message: fmt.Sprintf("Invalid value for key %v, it's must be ascii", lit.Value), |
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.
Message: fmt.Sprintf("Invalid value for key %v, it's must be ascii", lit.Value), | |
Message: fmt.Sprintf("Invalid value for key %v, it must be ascii", lit.Value), |
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.
Same comment about the message. Let's give some background here.
Key positional arguments are expected to be lowerCamelCase alphanumeric strings. Please remove any non-Latin characters.
hack/tools/logcheck/main.go
Outdated
} else { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: fun.Pos(), | ||
Message: "Invalid value type for key", |
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.
can we print the key here?
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.
It will be a bit complicated, because it has to check arg type
switch arg.(type) {
case *ast.FuncLit:
....
case *ast.CompositeLit:
....
}
if you are ok with it, i will fix it.
hack/tools/logcheck/testdata/data.go
Outdated
klog.InfoS("Starting container in a pod", "containerID", "containerID", "pod") // want `Invalid Number of arguments for InfoS` | ||
klog.ErrorS(nil, "Starting container in a pod", "containerID", "containerID", "pod") // want `Invalid Number of arguments for ErrorS` | ||
klog.InfoS("Starting container in a pod", "测试", "containerID") // want `Invalid value for key` | ||
klog.ErrorS(nil, "Starting container in a pod", "测试", "containerID") // want `Invalid value for key` |
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 is the test passing here? here error message should be Invalid value for key, It must be ascii
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.
Seems like it match by prefix~
hack/tools/logcheck/main.go
Outdated
@@ -80,6 +82,13 @@ func checkForFunctionExpr(fun ast.Expr, pass *analysis.Pass) { | |||
// extracting package name | |||
pName, ok := selExpr.X.(*ast.Ident) | |||
|
|||
if ok && pName.Name == "klog" && !isUnstructured((fName)) { |
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.
Suggesting little reshuffling here.
if ok && pName.Name == "klog" {
if isUnstructured((fName)) {
-----
----
} else {
isKeysValid()
}
}
@mowangdk Thanks for working on this :) !!!. Tried it locally it is working fine |
hack/tools/logcheck/main.go
Outdated
if len(keyValues)%2 != 0 { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: fun.Pos(), | ||
Message: fmt.Sprintf("Invalid Number of arguments for %s", funName), |
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.
This static analysis will be used for static analysis of every K/K PR, which means that almost every contributor will at some point need to fix errors from it. This puts requirement that descriptions for errors should be clear enough that someone without any knowledge of Structured Logging should be able to understand it.
Instead of message nvalid Number of arguments for %s
, we should to with something more actionable:
For example:
Uneven number of positional arguments. Additional arguments to InfoS should always be Key Value pairs.
Please check if there is any key or value missing.
hack/tools/logcheck/main.go
Outdated
if lit.Kind != token.STRING { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: fun.Pos(), | ||
Message: fmt.Sprintf("Invalid value type for key %v", lit.Value), |
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.
Same commend about message as above. Could we go with something like:
Key positional arguments are expected to be inlined constant strings. Please replace variable provided with it's value.
hack/tools/logcheck/main.go
Outdated
Message: fmt.Sprintf("Invalid value for key %v, it's must be ascii", lit.Value), | ||
}) | ||
} | ||
} else { |
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.
Let's move error handling block above and remove the else
.
Can you reverse logic check on line 136
if lit, ok := arg.(*ast.BasicLit); ok {
to
if lit, ok := arg.(*ast.BasicLit); !ok {
hack/tools/logcheck/main.go
Outdated
} else { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: fun.Pos(), | ||
Message: "Invalid value type for key", |
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.
What kind of failures we expect here? Can we give some suggestions to users about fixing this?
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.
Add new commit to fix some suggestions. ptal~ thanks a lot
hack/tools/logcheck/main.go
Outdated
} else { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: fun.Pos(), | ||
Message: "Invalid value type for key", |
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.
It will be a bit complicated, because it has to check arg type
switch arg.(type) {
case *ast.FuncLit:
....
case *ast.CompositeLit:
....
}
if you are ok with it, i will fix it.
hack/tools/logcheck/testdata/data.go
Outdated
klog.InfoS("Starting container in a pod", "containerID", "containerID", "pod") // want `Invalid Number of arguments for InfoS` | ||
klog.ErrorS(nil, "Starting container in a pod", "containerID", "containerID", "pod") // want `Invalid Number of arguments for ErrorS` | ||
klog.InfoS("Starting container in a pod", "测试", "containerID") // want `Invalid value for key` | ||
klog.ErrorS(nil, "Starting container in a pod", "测试", "containerID") // want `Invalid value for key` |
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.
Seems like it match by prefix~
hack/tools/logcheck/main.go
Outdated
if len(keyValues)%2 != 0 { | ||
pass.Report(analysis.Diagnostic{ | ||
Pos: fun.Pos(), | ||
Message: fmt.Sprintf("Invalid Number of arguments for %s", funName), |
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.
get, will fixed on next push, Thanks for advice~
/lgtm |
/approve thanks @serathius |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: dims, mowangdk, serathius The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Add check for InfoS & ErrorS parameters
Add check for InfoS & ErrorS parameters
What this PR does / why we need it:
Add check for valid keys and number of args for ErrorS/InfoS
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)
format, will close the issue(s) when PR gets merged):Fixes #219
Special notes for your reviewer:
None
Release note:
None