-
Notifications
You must be signed in to change notification settings - Fork 17
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
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 |
Lots and lots of Also i was planning to introduce contextual logging, but I think that can wait. |
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 |
0d6caa3
to
b863c22
Compare
The flag-logging can be made in different ways:
flag.VisitAll(func(flag *flag.Flag) {
logger.Info("flag", "flag", flag)
}) {
"ts": 1731837080411.8074,
"caller": "cmd/main.go:83",
"msg": "flag",
"v": 0,
"flag": {
"Name": "nfqueue-id",
"Usage": "Number of the nfqueue used",
"Value": 100,
"DefValue": "100"
}
}
flag.VisitAll(func(flag *flag.Flag) {
logger.Info("flag", "name", flag.Name, "value", flag.Value)
}) {
"ts": 1731837858630.011,
"caller": "cmd/main.go:83",
"msg": "flag",
"v": 0,
"name": "nfqueue-id",
"value": "100"
} Non-json logging:
flag.VisitAll(func(flag *flag.Flag) {
klog.Infof("FLAG: --%s=%q", flag.Name, flag.Value)
}) {
"ts": 1731838042913.3442,
"caller": "cmd/main.go:84",
"msg": "FLAG: --nfqueue-id=\"100\"",
"v": 0
} |
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.
b863c22
to
f241d4e
Compare
The failing test seem unrelated to the updates. I ran e2e:
|
/retest |
it seems a resource problem, let me retry @paulgmiller can you please review, since you have been working on this in a separate PR /assign @paulgmiller |
@aojea: GitHub didn't allow me to assign the following users: paulgmiller. Note that only kubernetes-sigs members with read permissions, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time. In response to this:
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. |
/lgtm waiting for @paulgmiller review, we can unhold tomorrow if he does not have time to review @paulgmiller just do |
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.
Seems good sorry to be slow had to learn some things about k8s.io/component-base/logs/
@@ -444,11 +446,11 @@ func (c *Controller) Run(ctx context.Context) error { | |||
} | |||
|
|||
startTime := time.Now() | |||
klog.V(2).Infof("Processing sync for packet %d", *a.PacketID) | |||
logger.V(2).Info("Processing sync for packet", "id", *a.PacketID) |
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.
should we use the same tlogger := logger.V(2) if tlogger.Enabled tricke we use here as we use at 517 in evaluage packet?
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.
It's not as urgent in this case since only PacketID
is passed (not an entire packet analyzed)
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: aojea, paulgmiller, 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 |
/hold cancel left one perf comment but doesn't seeem worth holding up |
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.