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
Conversation
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 also plug this new tool into the CI? Currently, the CI is not running the tests in this PR
hack/tools/logcheck/main_test.go
Outdated
|
||
import ( | ||
"testing" | ||
"fmt" |
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.
fmt is not used here, so it can be removed, right?
hack/tools/logcheck/README.md
Outdated
|
||
**Installation:**`go install k8s.io/klog/hack/tools/logcheck` | ||
**Usage:** `$logcheck.go <package_name>` | ||
`e.g $logcheck $KUBE_ROOT/pkg/kubelet/lifecycle/` |
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.
nit: $KUBE_ROOT no longer makes sense in the klog repo 😄
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.
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
.
This PR can add testing this module here: klog/.github/workflows/test.yml Line 17 in 06bfcd3
Add something like |
@nikhita @BenTheElder Thanks for the quick review :). Addressed all except the code comments that i am working on. |
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 really like this, thanks!
Some nits/suggestions, nothing serious 😅
Signed-off-by: Aditi Sharma <adi.sky17@gmail.com>
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.
/approve
/lgtm
thanks for adding this @adisky !
[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 |
"Infof", "Info", "Infoln", "InfoDepth", | ||
"Warning", "Warningf", "Warningln", "WarningDepth", | ||
"Error", "Errorf", "Errorln", "ErrorDepth", | ||
"Fatal", "Fatalf", "Fatalln", "FatalDepth", |
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.
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
:)
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.
Right it will be replaced by ErrorS
, here is the guideline
https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#replacing-fatal-calls
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.
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
} | ||
|
||
// extracting package name | ||
pName, ok := selExpr.X.(*ast.Ident) |
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 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?
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 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.
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: