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 flag to include file's dir in logs #74

Merged
merged 1 commit into from
Jul 9, 2019

Conversation

vincepri
Copy link
Member

@vincepri vincepri commented Jul 2, 2019

Signed-off-by: Vince Prignano vincepri@vmware.com

What this PR does / why we need it:
This PR adds a new flag to klog called add_dir_header. This flag, when enabled, includes the file's directory in the header.

This flag is meant to fix cases (like in Kubebuilder) where the generated files are called the same across different controllers/folders. This would reduce the need of renaming files, e.g. machine/controller.go -> machine/machine_controller.go.

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 #

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:


@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 2, 2019
@dims
Copy link
Member

dims commented Jul 2, 2019

/assign @DirectXMan12 @liggitt

klog.go Outdated
if slash >= 0 {
file = file[slash+1:]
dir := path.Base(path.Dir(file))
file = path.Base(file)
Copy link
Member

Choose a reason for hiding this comment

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

path.Base(path.Dir(...)) is significantly more expensive than the current code. until we have performance tests in place that tell us whether we are regressing (as several recent functional additions did), I don't think we should merge this

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fine. If we can find it useful we can keep it until we have a better solution in place.

klog.go Outdated
dir := path.Base(path.Dir(file))
file = path.Base(file)
if l.addDirHeader {
file = path.Join(dir, file)
Copy link
Member

Choose a reason for hiding this comment

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

alternatively, use a second strings.LastIndex call to back up a second segment if the addDirHeader option was true, and skip calling path.Base/path.Dir entirely

Copy link
Member

Choose a reason for hiding this comment

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

@liggitt what happens on windows? :)

Copy link
Member

Choose a reason for hiding this comment

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

what happens today? / is hard-coded

Copy link
Member

Choose a reason for hiding this comment

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

also, this is using path.Base(path.Dir(...)), not filepath...

Copy link
Member

Choose a reason for hiding this comment

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

ah good point.

Copy link
Member Author

Choose a reason for hiding this comment

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

Using a second LastIndex also sounds good. For windows compatibility question, we should probably open another issue

@dims
Copy link
Member

dims commented Jul 8, 2019

/uncc

@liggitt
Copy link
Member

liggitt commented Jul 9, 2019

can you add a benchmark that verifies this path does not regress in allocations or CPU when the add dir header option is false?

@vincepri
Copy link
Member Author

vincepri commented Jul 9, 2019

BenchmarkHeader branch=master

$ go test -benchmem -run=^$ k8s.io/klog -bench "^(BenchmarkHeader)$" -v -count=10
goos: darwin
goarch: amd64
pkg: k8s.io/klog
BenchmarkHeader-32    	 2000000	       804 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       802 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       801 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       802 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       803 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       803 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       808 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       815 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       808 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       810 ns/op	     184 B/op	       2 allocs/op
PASS
ok  	k8s.io/klog	24.282s

BenchmarkHeader branch=directory-header

$ go test -benchmem -run=^$ k8s.io/klog -bench "^(BenchmarkHeader)$" -v -count=10
goos: darwin
goarch: amd64
pkg: k8s.io/klog
BenchmarkHeader-32    	 2000000	       808 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       807 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       807 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       810 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       807 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       807 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       809 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       805 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       806 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeader-32    	 2000000	       807 ns/op	     184 B/op	       2 allocs/op
PASS
ok  	k8s.io/klog	24.345s

BenchmarkHeaderWithDir branch=directory-header

$ go test -benchmem -run=^$ k8s.io/klog -bench "^(BenchmarkHeaderWithDir)$" -v -count=10
goos: darwin
goarch: amd64
pkg: k8s.io/klog
BenchmarkHeaderWithDir-32    	 2000000	       815 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       814 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       812 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       813 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       815 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       814 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       815 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       814 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       814 ns/op	     184 B/op	       2 allocs/op
BenchmarkHeaderWithDir-32    	 2000000	       813 ns/op	     184 B/op	       2 allocs/op
PASS
ok  	k8s.io/klog	24.556s

Signed-off-by: Vince Prignano <vincepri@vmware.com>
@liggitt
Copy link
Member

liggitt commented Jul 9, 2019

thanks
/lgtm
/approve

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

dims commented Jul 9, 2019

@vincepri do you mind doc-ing the go test -benchmem magic. pretty please? :)

@dims
Copy link
Member

dims commented Jul 9, 2019

/approve

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dims, liggitt, vincepri

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 Jul 9, 2019
@k8s-ci-robot k8s-ci-robot merged commit aeddeec into kubernetes:master Jul 9, 2019
@@ -432,6 +433,7 @@ func InitFlags(flagset *flag.FlagSet) {
flagset.BoolVar(&logging.toStderr, "logtostderr", logging.toStderr, "log to standard error instead of files")
flagset.BoolVar(&logging.alsoToStderr, "alsologtostderr", logging.alsoToStderr, "log to standard error as well as files")
flagset.Var(&logging.verbosity, "v", "number for the log level verbosity")
flagset.BoolVar(&logging.skipHeaders, "add_dir_header", logging.addDirHeader, "If true, adds the file directory to the header")
Copy link

Choose a reason for hiding this comment

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

Shouldn't this be &logging.addDirHeader?

Copy link
Member

Choose a reason for hiding this comment

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

yes, it should. cc @dims

Copy link
Member

Choose a reason for hiding this comment

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

@jhahn21 Could you please file a PR to fix it. good catch. thanks!

Copy link

Choose a reason for hiding this comment

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

@dims Sure, I will.

Copy link

Choose a reason for hiding this comment

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

@dims Here is the PR: #101

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/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants