-
Notifications
You must be signed in to change notification settings - Fork 12
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
Use structured logging #50
base: main
Are you sure you want to change the base?
Conversation
Example output:
|
Lint complaints:
This is just as bad with func main() {
os.Exit(run())
}
func run() int {
return errorCode
}
I will call cancel() explicitly. (but since it's right before exit, it will not really matter) |
that sounds like the right way to do it |
@uablrek what is missing here? it lgtms /lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: aojea, uablrek 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 |
Lots and lots of Also i was planning to introduce contextual logging, but I think that can wait. |
New changes are detected. LGTM label has been removed. |
Structured logging allows a nice optimization possibility. Without this // evaluatePacket() should be fast unless trace logging is enabled.
// Logging optimization: We check if V(2) is enabled before hand,
// rather than evaluating the all parameters make an unnecessary logger call
tlogger := logger.V(2)
if tlogger.Enabled() {
tlogger.Info("Evaluating packet", "packet", p)
tlogger = tlogger.WithValues("id", p.id)
} |
@@ -371,7 +373,7 @@ func (c *Controller) Run(ctx context.Context) error { | |||
|
|||
nf, err := nfqueue.Open(&config) | |||
if err != nil { | |||
klog.Infof("could not open nfqueue socket: %v", err) | |||
logger.Info("could not open nfqueue socket", "error", err) |
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.
Logging like this exist at several places. I am unsure how to handle them. A general recommendation is to not log errors if you return them (let the caller decide). And normally logger.Error(err, ...)
is used for logging errors, but then it will be at a higher log-level. I made a compromise by keeping the log at Info level.
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.
A way to keep info about what caused the error is to wrap them:
return fmt.Errorf("could not open nfqueue socket %w", err)
(ref: recent discussion on slack)
Example packet log in JSON format: {
"ts": 1720777151838.3552,
"caller": "networkpolicy/controller.go:461",
"msg": "Evaluating packet",
"v": 2,
"packet": {
"Id": 9,
"Family": "IPv4",
"SrcIP": "11.0.3.3",
"DstIP": "11.0.2.2",
"Proto": "TCP",
"SrcPort": 33803,
"DstPort": 6000
}
} Text output is not altered:
|
I will squash commits later |
let me know once is ready for merging |
cmd/main.go
Outdated
ctx, cancel := signal.NotifyContext( | ||
context.Background(), os.Interrupt, unix.SIGINT) | ||
defer cancel() | ||
logger := klog.NewKlogr() |
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 shouldn't be needed and might never give you a JSON logger.
Leave the context as it is. A klog.FromContext
will then get the global default prepared by logsapi.ValidateAndApply
.
logger := klog.NewKlogr() | |
logger := klog.FromContext(ctx) |
cmd/main.go
Outdated
context.Background(), os.Interrupt, unix.SIGINT) | ||
defer cancel() | ||
logger := klog.NewKlogr() | ||
ctx = klog.NewContext(ctx, 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.
Delete this.
babda54
to
a247ecc
Compare
468ae28
to
0d6caa3
Compare
Rebased, updated after review, and use Please note that payload is empty for TCP/SYN, which is the only TCP packets we see:
|
@aojea Ready for review at least. I hope for merging. No logic is altered (except in main()), but very many log printouts |
Klog is initiated with support for json output, and a context with a logger is created. The context catches term signals. Log packets in JSON format for -logging-format=json.
0d6caa3
to
b863c22
Compare
PR needs rebase. 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-sigs/prow repository. |
Use structured logging in json format if
-logging-format=json
is specified. The same setup functions as for K8s core packages are used (component-base).Also, contextual logging is used (beta in 1.30, so no feature-gateway is needed).
Fixes #48
This PR will be a draft until all code is changed to use structured/contextual logging.