Skip to content
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

Merged
merged 2 commits into from Mar 20, 2021

Conversation

mowangdk
Copy link

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

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 17, 2021
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 17, 2021
if len(keyValues)%2 != 0 {
pass.Report(analysis.Diagnostic{
Pos: fun.Pos(),
Message: fmt.Sprintf("Invalid Number of arguments for %s", funName),
Copy link

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.

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.

Copy link
Author

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~

if !isASCII {
pass.Report(analysis.Diagnostic{
Pos: fun.Pos(),
Message: fmt.Sprintf("Invalid value for key %v, it's must be ascii", lit.Value),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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),

Copy link

@serathius serathius Mar 19, 2021

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.

} else {
pass.Report(analysis.Diagnostic{
Pos: fun.Pos(),
Message: "Invalid value type for key",
Copy link

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?

Copy link
Author

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.

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`
Copy link

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

Copy link
Author

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~

@@ -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)) {
Copy link

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()
    }
}

@adisky
Copy link

adisky commented Mar 19, 2021

@mowangdk Thanks for working on this :) !!!. Tried it locally it is working fine
Some suggestions inline

if len(keyValues)%2 != 0 {
pass.Report(analysis.Diagnostic{
Pos: fun.Pos(),
Message: fmt.Sprintf("Invalid Number of arguments for %s", funName),

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.

if lit.Kind != token.STRING {
pass.Report(analysis.Diagnostic{
Pos: fun.Pos(),
Message: fmt.Sprintf("Invalid value type for key %v", lit.Value),

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.

Message: fmt.Sprintf("Invalid value for key %v, it's must be ascii", lit.Value),
})
}
} else {
Copy link

@serathius serathius Mar 19, 2021

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 {

} else {
pass.Report(analysis.Diagnostic{
Pos: fun.Pos(),
Message: "Invalid value type for key",

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?

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 20, 2021
Copy link
Author

@mowangdk mowangdk left a 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

} else {
pass.Report(analysis.Diagnostic{
Pos: fun.Pos(),
Message: "Invalid value type for key",
Copy link
Author

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.

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`
Copy link
Author

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~

if len(keyValues)%2 != 0 {
pass.Report(analysis.Diagnostic{
Pos: fun.Pos(),
Message: fmt.Sprintf("Invalid Number of arguments for %s", funName),
Copy link
Author

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~

@serathius
Copy link

/lgtm
Please mark the Outdated comments as resolved.
/assign @dims

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 20, 2021
@dims
Copy link
Member

dims commented Mar 20, 2021

/approve

thanks @serathius

@k8s-ci-robot
Copy link

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 20, 2021
@k8s-ci-robot k8s-ci-robot merged commit e158208 into kubernetes:master Mar 20, 2021
pohly pushed a commit to pohly/klog that referenced this pull request Jun 10, 2022
Add check for InfoS & ErrorS parameters
pohly pushed a commit to pohly/logtools that referenced this pull request Jun 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

logcheck: Add check for valid keys and number of args for ErrorS/InfoS
5 participants