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

Enabled: always must skip two levels #216

Closed
wants to merge 1 commit into from

Conversation

pohly
Copy link
Contributor

@pohly pohly commented Sep 2, 2023

klog does stack unwinding in LogSink.Enabled to implement per-source code verbosity thresholds (-vmodule). This worked for logger.Info and logger.Error because the code was written such that it matches how logr is implemented (Logger.Info -> Logger.Enabled -> LogSink.Enabled).

It did not work for direct calls (if logger.Enabled) because then the call chain is Logger.Enabled -> LogSink.Enabled. That callchain is less common, so to fix this problem the callchains get arranges so that all calls go through one additional intermediate level.

This is unnecessary extra work, but it is necessary to avoid breaking the more common normal logging calls with klog and -vmodule.

Fixes: #215

@pohly
Copy link
Contributor Author

pohly commented Sep 2, 2023

Let's not merge yet! I first want to add test cases in klog for logger.Enabled.

@pohly pohly mentioned this pull request Sep 2, 2023
// the original logging site (e.g. file & line) should climb this many
// additional frames to find it.
//
// The CallDepth is given for LogSink.Info and LogSink.Error such that 1 means
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this now fixed (Info -> enabled parallel to Enabled -> enabled)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

I have a test for this in kubernetes/klog#383. That test fails with current logr and passes with this PR.

We can merge it now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we remove this part of the comment?

klog does stack unwinding in `LogSink.Enabled` to implement per-source code
verbosity thresholds (`-vmodule`). This worked for `logger.Info` and
`logger.Error` because the code was written such that it matches how logr is
implemented (Logger.Info -> Logger.Enabled -> LogSink.Enabled).

It did not work for direct calls (`if logger.Enabled`) because then the call
chain is `Logger.Enabled -> LogSink.Enabled`. That callchain is less common, so
to fix this problem the callchains get arranges so that all calls go through
one additional intermediate level.

This is unnecessary extra work, but it is necessary to avoid breaking the more
common normal logging calls with klog and -vmodule.
@pohly pohly force-pushed the enabled-calldepth branch from 374a35a to a53aadb Compare September 3, 2023 18:47
@pohly pohly changed the title WIP: Enabled: always must skip two levels Enabled: always must skip two levels Sep 3, 2023
// enabled ensures that Enabled and Info both call LogSink.Enabled with one
// intermediate stack frame. This wouldn't be necessary if Info had called
// LogSink.Enabled directly in logr v1.0.0, but it didn't and now that cannot
// be changed anymore because implementations depend on this behavior.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understsand the "implementations depend on this behavior" point. It seems like we could either do

(Info, Error, Enabled) -> sink.Enabled

or

(Info, Error, Enabled) -> enabled -> sink.Enabled

And it would be correct, as long as it is consistent - right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understsand the "implementations depend on this behavior" point.

I mean this code:

https://github.com/kubernetes/klog/blob/6632ba5cc9a5331c853a2c6dbc0fbb125e6e7b94/klogr.go#L55-L56

That code works with logr v1.2.0 for logger.Info.

If we switch to (Info, Error, Enabled) -> sink.Enabled, logger.Info will not work correctly in combination with -vmodule anymore when someone uses klog <= v2.100.1 and updates to a future logr release where we made that change.

We can change klog in v2.200.0 and make it depend on such a new logr release, but we cannot prevent the combination above.

And it would be correct, as long as it is consistent - right?

Both are "correct", but (Info, Error, Enabled) -> enabled -> sink.Enabled will cause less pain for downstream users.

Copy link
Contributor

@thockin thockin Sep 4, 2023

Choose a reason for hiding this comment

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

But then we are left with this weird document that Enabled() needs one more frame. I hate it. If it's already buggy, how bad is it really to just fix the bug?

@pohly
Copy link
Contributor Author

pohly commented Sep 4, 2023

Closing in favor of #218

@thockin thockin closed this Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enabled + stack unwinding
2 participants