-
Notifications
You must be signed in to change notification settings - Fork 101
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
Support structured logging #202
Support structured logging #202
Conversation
Welcome @bells17! |
Hi @bells17. Thanks for your PR. I'm waiting for a kubernetes-csi member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
3f34d99
to
0a4004e
Compare
/ok-to-test |
cmd/livenessprobe/main.go
Outdated
fg := featuregate.NewFeatureGate() | ||
logsapi.AddFeatureGates(fg) | ||
c := logsapi.NewLoggingConfiguration() | ||
logsapi.AddGoFlags(c, flag.CommandLine) | ||
logs.InitLogs() | ||
flag.Set("logtostderr", "true") | ||
flag.Parse() | ||
if err := logsapi.ValidateAndApply(c, fg); err != nil { | ||
klog.ErrorS(err, "LoggingConfiguration is invalid") | ||
os.Exit(1) | ||
} | ||
if c.Format == logsapi.JSONLogFormat { | ||
logger, _ := json.NewJSONLogger(c.Verbosity, os.Stderr, nil, nil) | ||
klog.SetLogger(logger) | ||
defer klog.ClearLogger() | ||
} |
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 looks like quite a big chunk of code that could live in csi-lib-utils, so all sidecars can benefit from it. BTW, is there anything in component-base
that we could use to set up logging at a sidecar startup?
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 apologize for the oversight. On double-checking, I realized that it wasn't necessary to manually reset the log format, so I've fixed the code.
This looks like a fairly large chunk of code that could live in csi-lib-utils so that all sidecars can benefit from it.
Indeed, even after fixing the source code, we're still left with an implementation that spans about 20 lines. Considering that other CSI sidecars can also leverage this implementation, I believe it's a good idea to move it to csi-lib-utils.
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.
We discussed this PR in our sig-stroage triage meeting and we think that structured logging would be a great thing to have.
However, we would like to merge it after we release the CSI sidecars for Kubernetes 1.28 - it should be soon-ish.
Would you be interested in changing all the other sidecars? It would be great if all of them had the same logging cmdline options + capabilities in 1.29. You don't need to do all of them, we could find other volunteers. The cmdline changes are simple enough, but all the logs would need to be updated from klog.Infof
to klog.InfoS
.
cmd/livenessprobe/main.go
Outdated
// We run `klog.InitFlags` to configure the `logtostderr` option. | ||
// Due to a conflict with `logsapi.AddGoFlags`, we set only | ||
// the `logtostderr` option in `flag.CommandLine` using the `fs` variable | ||
// to avoid errors. | ||
// | ||
// For options set by klog and component-base logs, see the links below: | ||
// https://github.com/kubernetes/component-base/blob/v0.28.0-rc.0/logs/api/v1/options.go#L337-L355 | ||
// https://github.com/kubernetes/klog/blob/v2.100.1/klog.go#L400-L424 | ||
var fs flag.FlagSet | ||
klog.InitFlags(&fs) | ||
fs.VisitAll(func(f *flag.Flag) { | ||
switch f.Name { | ||
case "logtostderr": | ||
flag.CommandLine.Var(f.Value, f.Name, f.Usage) | ||
} | ||
}) |
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 checked that --logtostderr
is enabled by default. IMO it makes sense to remove flag.Set("logtostderr", "true")
and related code from this PR - it should do no harm.
We carried flag.Set("logtostderr", "true")
since the sidecar first PR and nobody remembers why.
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've removed the code related to --logtostderr
. Could you please review it again?
I'd be happy to take on the challenge!
Understood. As for |
2e273a4
to
927acd6
Compare
Based on the guidelines specified at the URL below, I've updated the klog logging functions to align with those recommendations. And I became aware of a tool called logcheck recently. So I've run logcheck command against the
|
@jsafrane, I have updated the klog functions based on the guidelines. |
/lgtm /hold |
@mauriciopoppe I apologize, but I thought it would be simpler to squash the commits, so I have done so and pushed. I kindly ask for your review again. |
No problem, a single commit lgtm /lgtm |
@jsafrane @mauriciopoppe Sorry, I missed a few spots that needed corrections, so I've added another commit. |
7aca59f
to
8951e32
Compare
1e9bcc4
to
779b5a0
Compare
@mauriciopoppe Thanks. I squashed the commits. |
/lgtm |
779b5a0
to
6a6bf23
Compare
@jsafrane @mauriciopoppe I've resolved the conflicts and pushed the changes. Could you please give it another look and approve if everything is fine? Here are the steps I followed to resolve the conflicts:
|
/lgtm Waiting for the hold lift to get merged |
/hold cancel |
What type of PR is this?
/kind feature
What this PR does / why we need it:
In this PR I have added JSON logging.
With this change, it is now possible to output the logs of the livenessprove container in JSON format.
Running the container with the
--logging-format=json
option will output the logs in JSON format.In addition, I've modified the log messages based on the following guideline:
https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#remove-string-formatting-from-log-message
I’ve update the klog functions in use according to the guidelines provided below, and I've confirmed that they pass the logcheck tests:
https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/migration-to-structured-logging.md#change-log-functions
Which issue(s) this PR fixes:
Fixes #165
Special notes for your reviewer:
Does this PR introduce a user-facing change?: