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 Static linter for structured logging #213

Merged
merged 1 commit into from Mar 3, 2021

Conversation

adisky
Copy link

@adisky adisky commented Mar 3, 2021

Signed-off-by: Aditi Sharma adi.sky17@gmail.com

What this PR does / why we need it:
Adding Static linter to avoid regressions after migration to structured logging.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Ref Issue: kubernetes/kubernetes#98975
See Discussion: kubernetes/kubernetes#99090
Special notes for your reviewer:

Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

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 3, 2021
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 3, 2021
Copy link
Member

@nikhita nikhita left a comment

Choose a reason for hiding this comment

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

Can we also plug this new tool into the CI? Currently, the CI is not running the tests in this PR


import (
"testing"
"fmt"
Copy link
Member

Choose a reason for hiding this comment

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

fmt is not used here, so it can be removed, right?


**Installation:**`go install k8s.io/klog/hack/tools/logcheck`
**Usage:** `$logcheck.go <package_name>`
`e.g $logcheck $KUBE_ROOT/pkg/kubelet/lifecycle/`
Copy link
Member

Choose a reason for hiding this comment

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

nit: $KUBE_ROOT no longer makes sense in the klog repo 😄

Copy link
Member

Choose a reason for hiding this comment

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

maybe just ./pkg/kubelet/lifecycle ?

GOPATH is going away in go 1.17 anyhow, so users will have to start calling tools like this from within a module, where it will either be k8s.io/kubernetes/pkg/kubelet/lifecycle or ./pkg/kubelet/lifecycle.

@BenTheElder
Copy link
Member

This PR can add testing this module here:

- name: Test

Add something like (cd hack/tools && go test -v -race ./...) below line 20 I think

@adisky
Copy link
Author

adisky commented Mar 3, 2021

@nikhita @BenTheElder Thanks for the quick review :). Addressed all except the code comments that i am working on.

Copy link
Member

@BenTheElder BenTheElder left a comment

Choose a reason for hiding this comment

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

I really like this, thanks!
Some nits/suggestions, nothing serious 😅

hack/tools/logcheck/main_test.go Show resolved Hide resolved
hack/tools/logcheck/main.go Outdated Show resolved Hide resolved
hack/tools/logcheck/main.go Outdated Show resolved Hide resolved
Signed-off-by: Aditi Sharma <adi.sky17@gmail.com>
@BenTheElder
Copy link
Member

BenTheElder commented Mar 3, 2021

/assign @dims

klog/OWNERS

Line 12 in 06bfcd3

- dims

Copy link
Member

@dims dims left a comment

Choose a reason for hiding this comment

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

/approve
/lgtm

thanks for adding this @adisky !

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: adisky, dims

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 3, 2021
@k8s-ci-robot k8s-ci-robot merged commit 14dec33 into kubernetes:master Mar 3, 2021
"Infof", "Info", "Infoln", "InfoDepth",
"Warning", "Warningf", "Warningln", "WarningDepth",
"Error", "Errorf", "Errorln", "ErrorDepth",
"Fatal", "Fatalf", "Fatalln", "FatalDepth",
Copy link
Member

@nikhita nikhita Mar 4, 2021

Choose a reason for hiding this comment

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

There is no FatalS so what would be recommend to use as an alternative?

Edit: never mind, I guess the recommend approach is to use ErrorS :)

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Member

@neolit123 neolit123 Mar 4, 2021

Choose a reason for hiding this comment

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

Fatal* must be replaced with ErrorS + os.Exit() or this would be a change in behavior.
the migrate guide is inaccurate in this regard.

EDIT: actually NVM it does mention it:
https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#replacing-fatal-calls

This was referenced Mar 12, 2021
}

// extracting package name
pName, ok := selExpr.X.(*ast.Ident)
Copy link

Choose a reason for hiding this comment

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

This is taking a shortcut. Was that intentional because the alternative is too complex?

What we want to compare against is k8s.io/klog/v2, not just the name that the package was imported under. As it stands now, the following code will not be recognized as using klog.Info:

import glog "k8s.io/klog/v2"

func someFunc() {
  glog.Info("hello world"
}

It must be possible to lookup the selector, but after staring at the analysis.Pass for a while I still haven't figured out how. Any ideas?

Copy link

Choose a reason for hiding this comment

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

I found a solution. This here works and also covers other cases that weren't handled before, like if klogV := klog.V(1); klogV.Enabled() { klogV.Info("should not be used") }:

// isKlog checks whether an expression is klog.Verbose or the klog package itself.
func isKlog(expr ast.Expr, pass *analysis.Pass) bool {
	// For klog.V(1) and klogV := klog.V(1) we can decide based on the type.
	if typeAndValue, ok := pass.TypesInfo.Types[expr]; ok {
		switch t := typeAndValue.Type.(type) {
		case *types.Named:
			typeName := t.Obj()
			name := typeName.Name()
			pkg := typeName.Pkg()
			pkgPath := pkg.Path()
			if name == "Verbose" && pkgPath == "k8s.io/klog/v2" {
				return true
			}
		}
	}

	// In "klog.Info", "klog" is a package identifier. It doesn't need to
	// be "klog" because here we look up the actual package.
	if ident, ok := expr.(*ast.Ident); ok {
		if object, ok := pass.TypesInfo.Uses[ident]; ok {
			switch object := object.(type) {
			case *types.PkgName:
				pkg := object.Imported()
				if pkg.Path() == "k8s.io/klog/v2" {
					return true
				}
			}
		}
	}

	return false
}

I'm currently collecting some improvements of logcheck for contextual logging and will include this in that patch set.

IMHO it's not important enough to be fixed right away. Import renaming was done in some projects that migrated from glog, but seems less common nowadays.

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.

None yet

7 participants