-
Notifications
You must be signed in to change notification settings - Fork 25
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
[VC-35738] Log with klog to stdout and stderr in Kubernetes text format #596
Conversation
| grep -q "Data sent successfully" | ||
--follow \ | ||
--namespace venafi \ | ||
| timeout 60 jq 'if .msg | test("Data sent successfully") then . | halt_error(0) end' |
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 a hack but it halts the pipeline when the expected msg
value is observed and prints the matching record to stderr:
The timeout stops the pipeline after 60 seconds if the expected msg is not observed.
"k8s.io/component-base/featuregate" | ||
"k8s.io/component-base/logs" | ||
logsapi "k8s.io/component-base/logs/api/v1" | ||
_ "k8s.io/component-base/logs/json/register" |
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.
Importing this module, is what makes the json log format option available.
411461d
to
161382f
Compare
Hi, does cert-manager and the other components use klog? Do they use the conventions that this MR is adopting? |
Hey Richard, great job with adding unit tests around the logs. I really appreciate the effort. Thanks to you, I can just run
To the question of consistency:
For that reason, the choice of klogr would make more sense in the Venafi Kubernetes Agent as it would bring structured logging. My first suggestion would be for us to start with Note that I found in kubernetes/klog@44eadc3 that
My second suggestion, based on the fact that klogr is being phased out, would be to use std lib's Full picture:
* log is the standard library's |
Hi, yep, consistency was exactly my concern, thanks for the detailed response. In my opinion it's favourable to have a smaller set to make updates easier down the line, even if that smaller set doesn't include the current best practice yet. If this project were to adopt the conventions of the majority of the other projects then we would have more consistency, and could figure out how/when to migrate them en-masse. Alternatively there may be an argument to update to the latest/greatest now, it depends on the details. It sounds like you think that |
@wallrj I've reviewed your PR and made some small adjustments:
Regarding the flags: It seems we (the cert-manager project) have initiated the convention of the flags I propose that we Regarding the slog severity problem:
I've investigated and was able to configure an slog logger in d9afd95. To avoid lots of unrelated changes, the agent logs are still using the standard library's Regarding the output format: I found that none of our projects currently output JSON by default. You mentioned the Kubernetes logging conventions, which say that the JSON format must be enabled with
@wallrj IMO we should set the default output to "text"... WDYT? I haven't reverted to "text". I'll let you decide. @wallrj Feel free to merge this PR once you are happy with it. I'll approve it now. |
a87bfd3
to
4428a24
Compare
pkg/logs/logs_test.go
Outdated
flags: "", | ||
expectStdout: ` | ||
{"ts":0000000000000.000,"caller":"logs/logs.go:000","msg":"log Print","source":"vcert","v":0} | ||
{"ts":0000000000000.000,"caller":"logs/logs_test.go:000","msg":"slog Info","v":0} |
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 wish we were able to show the real source of the log line rather than the generic logs/logs_test.go
. I guess we can improve this once we have migrated the logs.Log.Printf
calls to slog.Info
.
1eb699a
to
a41b832
Compare
c2b6d69
to
9771a25
Compare
I agree that text format by default is more consistent with other TLSPK components. Done.
I'll merge this after the 1.2.0 release because I'd like to finish the other logging improvements before releasing to customers. |
// the agent, which still uses log.Printf. | ||
slog := slog.Default() | ||
|
||
Log = &log.Logger{} |
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 just use slog.SetDefault(...)
here?
Using slog.New(logr.ToSlogHandler(logger))
we can convert the klog logger to an slog logger.
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. I'll consider that in a follow up.
if f.Name == "logging-format" { | ||
f.Usage = `Sets the log format. Permitted formats: "json", "text".` | ||
} | ||
if f.Name == "log-text-split-stream" { |
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.
Why are we splitting the log streams? Doesn't this make it hard to reconstruct the order or messages?
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.
Here's my current understanding:
-
I enabled
log-json-split-stream
by default to solve the problem that Google Logs Explorer otherwise can't distinguish between JSON formatted errors and info messages.Severities: By default, logs written to the standard output are on the INFO level and logs written to the standard error are on the ERROR level. Structured logs can include a severity field, which defines the log's severity.
-- https://cloud.google.com/kubernetes-engine/docs/concepts/about-logs#best_practices(klog JSON format does not include a
severity
field). -
log-text-split-stream
is not technically necessary to solve that problem because Google Logs Explorer seems to parse theE
,W
, andI
prefix of the Kubernetes text format. So yes, I could remove this change. -
Kubernetes introduced the split-stream options to solve performance problems. Info and warn messages can be buffered and flushed periodically while errors are flushed immediately.
-
By default logs are not buffered and are flushed to stdout and stderr immediately, so there shouldn't be any out-of-order log messages.
-
If log buffering is enabled, then I think that the buffered info logs will be flushed whenever an error is logged, so there should be any out-of-order log messages in that case either.
-
Kubernetes plan to make split-stream JSON logging the default for the Kubernetes components.
Ability to route logs based on type/verbosity will be replaced with default splitting info and errors logs to stdout and stderr. We will make this change only in alternative logging formats (like JSON) as we don't want to introduce breaking change in default configuration. Splitting stream will allow treating info and errors with different priorities. It will unblock efforts like Make info logging non-blocking kubernetes/klog#209 to make info logs non-blocking.
Signed-off-by: Richard Wall <richard.wall@venafi.com>
For some reason, `assert.Equal` doesn't cause the test sub-process to exit with 1. Instead, it returns 0 which means we can't notice the error from the parent process.
That's useful to keep track what the logs look like, especially now that I'm trying to switch from plain klog to slog.
Before, there was no way to know if a given log came from the VCert library or from the agent itself. I had to tweak logToSlogWriter's Writer to remove the extraneous "\n" that log.Printf prints.
Signed-off-by: Richard Wall <richard.wall@venafi.com>
The exact logs output is now verified. Signed-off-by: Richard Wall <richard.wall@venafi.com>
9771a25
to
73526f3
Compare
Signed-off-by: Richard Wall <richard.wall@venafi.com>
I've settled on using klog and kubernetes component-base module for the reasons given in the comments in the logs.go file.
I think this logging UX is most likely to be familiar to Kubernetes platform admins who are likely to be deploying and debugging the venafi-kubernetes-agent.I gave up on Slog because it seemed to have unfamiliar severity / levels and I couldn't get the klog client-go logs to have the right levels with slog.
Using klog allows us to debug problems in client-go.
For example, you can view the API requests and responses using
--vmodule=round_tripper=6
.In followup PRs I will convert the existing log lines to use context derived logr loggers.