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 tool to check use of unstructured logs #99090

Merged
merged 1 commit into from Mar 6, 2021

Conversation

adisky
Copy link
Contributor

@adisky adisky commented Feb 15, 2021

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

What type of PR is this?

/kind feature

What this PR does / why we need it:

It adds script hack/verify-structured-logging which reports use of unstructured logs. This script will be used to prevent regression after packages have been migrated to use structured logging.

Which issue(s) this PR fixes:

Ref #98975

Special notes for your reviewer:

This PR only adds the static check tool, scripts needs to be created to use this tool. Currently it only reports the use of unstructured logging functions. It could be improved for other checks like use of format specifiers.

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP]: https://github.com/kubernetes/enhancements/tree/master/keps/sig-instrumentation/1602-structured-logging

/sig instrumentation

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/feature Categorizes issue or PR as related to a new feature. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Feb 15, 2021
@k8s-ci-robot k8s-ci-robot added area/test sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels Feb 15, 2021
@adisky adisky changed the title Add tool to check use of unstructured logs [WIP] Add tool to check use of unstructured logs Feb 15, 2021
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 15, 2021
@serathius
Copy link
Contributor

Overall looks good, can you add tests like for metric stability analysis ?https://github.com/kubernetes/kubernetes/blob/master/test/instrumentation/main_test.go

@adisky
Copy link
Contributor Author

adisky commented Feb 15, 2021

Overall looks good, can you add tests like for metric stability analysis ?https://github.com/kubernetes/kubernetes/blob/master/test/instrumentation/main_test.go

sure, will update with tests.

@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 Feb 15, 2021
@ehashman
Copy link
Member

/triage accepted
/priority important-soon
/assign

@k8s-ci-robot k8s-ci-robot added triage/accepted Indicates an issue or PR is ready to be actively worked on. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Feb 16, 2021
@adisky
Copy link
Contributor Author

adisky commented Feb 17, 2021

It is not working currently for Verbose style logs klog.V(2).Infof(), Working on it and tests.

Copy link
Member

@ehashman ehashman left a comment

Choose a reason for hiding this comment

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

Looking great thus far! I'm unfamiliar with the tool so I'm not yet sure how we'll handle the nested V(N) cases but looking forward to your solution.

My test run:

ehashman@red-dot:~/src/k8s/test/instrumentation/logcheck$ go run main.go analyzer.go ~/src/k8s/pkg/kubelet/lifecycle/
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/handlers.go:67:4: unstructured logging function "Infof" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/handlers.go:74:4: unstructured logging function "Infof" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/handlers.go:80:3: unstructured logging function "Errorf" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/handlers.go:113:4: unstructured logging function "Errorf" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/predicate.go:65:3: unstructured logging function "Errorf" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/predicate.go:79:3: unstructured logging function "Warningf" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/predicate.go:101:3: unstructured logging function "Warningf" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/predicate.go:113:4: unstructured logging function "Warningf" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/predicate.go:126:4: unstructured logging function "Warningf" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/predicate.go:139:4: unstructured logging function "Infof" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/predicate.go:143:4: unstructured logging function "Infof" should not be used
/home/ehashman/src/k8s/pkg/kubelet/lifecycle/predicate.go:147:4: unstructured logging function "Warningf" should not be used
exit status 3

test/instrumentation/logcheck/README.md Outdated Show resolved Hide resolved
@ehashman
Copy link
Member

/cc @BenTheElder

if you have the chance to provide some early feedback on this static analysis we're writing to prevent structured logging regressions

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

ehashman commented Mar 4, 2021

FWIW as well: I think we should have plenty of support to merge after code freeze if this misses it somehow, because this only alters tests not the release binaries, and test freeze is not until march 24th https://www.kubernetes.dev/resources/release/#tldr

.. but I think we can get this in before code freeze anyhow sweat_smile

The hope is to get this merged and included in CI before code freeze because it will validate that we've fully migrated pkg/kubelet and cmd/kubelet to structured logging, which is an acceptance criteria for structured logging graduation to beta.

Copy link
Member

@ehashman ehashman left a comment

Choose a reason for hiding this comment

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

I think at this point the last thing remaining is to wire this into verify.sh?

hack/.structured_logging Show resolved Hide resolved
@adisky
Copy link
Contributor Author

adisky commented Mar 4, 2021

I think at this point the last thing remaining is to wire this into verify.sh?

I added a verify script, is something else i am missing?
https://github.com/kubernetes/kubernetes/blob/5922c90e354e14fd38b4542ea087a7bf4606b8db/hack/verify-structured-logging.sh

@ehashman
Copy link
Member

ehashman commented Mar 4, 2021

Yes, in order for this to run as part of make verify I believe https://github.com/kubernetes/kubernetes/blob/master/hack/make-rules/verify.sh needs to be updated.

@adisky
Copy link
Contributor Author

adisky commented Mar 4, 2021

@BenTheElder
Copy link
Member

Verify locates all hack/verify*.sh and runs them except for specific exclusions. Shouldn't need to change anything there.

@BenTheElder
Copy link
Member

we indeed ran it in 13s, #99090 (comment) is my main remaining concern (it looks like this script can never exit failure currently?)


# Install logcheck
pushd "${KUBE_ROOT}/hack/tools" >/dev/null
GO111MODULE=on go install k8s.io/klog/hack/tools/logcheck
Copy link
Member

Choose a reason for hiding this comment

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

this may also need

export GOBIN="${KUBE_OUTPUT_BINPATH}"
PATH="${GOBIN}:${PATH}"

we don't want to install to the global GOBIN

Copy link
Member

Choose a reason for hiding this comment

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

also needs a call to kube::golang::setup_env earlier I think (which will also validate the required go version if running this script by itself).

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the setup_env was required for me to run this locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using kube::golang::setup_env not working for me locally, added kube::golang::verify_go_version, also I have checked other linting scripts they are not calling setup_env.
https://github.com/kubernetes/kubernetes/blob/master/hack/verify-staticcheck.sh

Copy link
Member

Choose a reason for hiding this comment

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

kube::golang::setup_env is pretty standard, this is a bug in verify-staticcheck if anything https://cs.k8s.io/?q=kube%3A%3Agolang%3A%3Asetup_env&i=nope&files=&excludeFiles=&repos=

Signed-off-by: Aditi Sharma <adi.sky17@gmail.com>
@adisky
Copy link
Contributor Author

adisky commented Mar 5, 2021

/test pull-kubernetes-unit

@ehashman
Copy link
Member

ehashman commented Mar 5, 2021

/retest

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.

/lgtm
/approve
This is sufficient to iterate on, thank you.
/hold
One remaining check to consider: normally before merging a new lint like this we want to see that it will in fact fail on something, so we typically push a temporary commit to show this (e.g. adding a bad change in a covered file), then revert the change (giving CI runs reviewers can see).

done < <(cat "${migrated_packages_file}")


# TODO: Improve concurrancy here
Copy link
Member

Choose a reason for hiding this comment

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

nit: seems spelling lint missed this, s/concurrency/concurrancy/ (not going to block on this during code freeze of all times!)

Copy link
Member

Choose a reason for hiding this comment

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

Also hard data: this is taking 13s in CI which is fast enough for now (though nearly as slow as gofmt parsing the entire repo). The overall job takes 35m, but every bit counts (most of these tools are not easily made any faster, and we have removed some checks out of the default make verify to avoid excessive time creep).
Will investigate in follow-ups.

source "${KUBE_ROOT}/hack/lib/init.sh"
source "${KUBE_ROOT}/hack/lib/util.sh"

kube::golang::verify_go_version
Copy link
Member

Choose a reason for hiding this comment

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

because this operates on the source tree kube::golang::setup_env is needed to work for developers that do not have Kubernetes checked out to a GOPATH. we can do that in a follow-up though.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 5, 2021
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Mar 5, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 5, 2021
# TODO: Improve concurrancy here
ret=0
for package in "${migrated_packages[@]}"; do
logcheck "$KUBE_ROOT/$package" || ret=$?
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this is the best way to do this (because it will involve building twice) but I was able to fix kubernetes/klog#215 with the following patch:

Suggested change
logcheck "$KUBE_ROOT/$package" || ret=$?
GOOS=linux logcheck "$KUBE_ROOT/$package" || ret=$?
GOOS=windows logcheck "$KUBE_ROOT/$package" || ret=$?

@ehashman
Copy link
Member

ehashman commented Mar 6, 2021

/hold cancel

I've confirmed locally this fails against files not in the list. I'm going to follow up with a PR to update migrated files and update the guidance for structured logging PRs to add to this file.

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Mar 6, 2021
@BenTheElder
Copy link
Member

/retest
🤨

@k8s-ci-robot k8s-ci-robot merged commit f55a4ce into kubernetes:master Mar 6, 2021
Structured Logging Migration for Kubelet, 1.21 automation moved this from Waiting on Author to Done Mar 6, 2021
@k8s-ci-robot k8s-ci-robot added this to the v1.21 milestone Mar 6, 2021
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. area/dependency Issues or PRs related to dependency changes area/test cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. lgtm "Looks good to me", indicates that a PR is ready to be merged. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note-none Denotes a PR that doesn't merit a release note. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

7 participants