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

Use RUST_LOG env to configure logging if present #247

Merged
merged 1 commit into from
Mar 6, 2024

Conversation

jsturtevant
Copy link
Contributor

We identified a perf impact when writing logs. In this case it would be helpful to only run with logs set at a "warning" or "error" level. We attempted to turn most logs off via containerd with containerd -l fatal but it still dumped logs.

After some investigation it appears containerd only sets a flag not a level (https://github.com/containerd/containerd/blob/7467d81987e1a54dd2a89e052ae9429a4c921e9e/core/runtime/v2/binary.go#L66-L68) and our code will default to info

let level = if debug {
log::LevelFilter::Debug
} else {
log::LevelFilter::Info
};

changes

This reads the environment variable RUST_LOG and will use value from there if set. It can be set to any values in the log crate. Otherwise the behavior is the same. If the -debug flag is passed it will choose the highest of the two values.

This also introduces ability to configure the default setting through the shim Config object:

run::<Shim>(&id, Some(Config{default_log_level: "error", ..Default::default()}));

If all the configuration fails it will fallback to logging to to info level.

Signed-off-by: James Sturtevant <jstur@microsoft.com>
Copy link
Member

@Mossaka Mossaka left a comment

Choose a reason for hiding this comment

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

Love to see this!

fn configure_logging_level(debug: bool, default_log_level: &str) {
let debug_level = std::env::var(LOG_ENV).unwrap_or(default_log_level.to_string());
let debug_level = log::LevelFilter::from_str(&debug_level).unwrap_or(log::LevelFilter::Info);
let level = if debug && log::LevelFilter::Debug > debug_level {
Copy link
Member

Choose a reason for hiding this comment

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

Why do you still need this special handling of debug logic?

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 kept it there so that it was backwards compatible. If a user turns that value on they would expect logs and I didn't want to break that.

Copy link
Member

Choose a reason for hiding this comment

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

Some context about defaults is above, but we may simplify this a bit to:

  • level = DEFAULT_LOG_LEVEL
  • if there is an env - parse & set level from env
  • if debug { level = debug } (may be output a warning that containerd forced debug mode)
    ?

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 wasn't sure that we could push out a warning as the logger wasn't fully set up at this point but I can try it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the reason for if debug && log::LevelFilter::Debug > debug_level was I wanted to ensure that if the ENV variable was more verbose than debug that we honor it.

@@ -34,6 +34,8 @@ use crate::error::Error;
#[cfg(windows)]
use crate::sys::windows::NamedPipeLogger;

pub const LOG_ENV: &str = "RUST_LOG";
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should name the env var to be SHIM_LOG because this is probably not specific to rust, no?

Copy link
Member

@mxpv mxpv Feb 23, 2024

Choose a reason for hiding this comment

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

RUST_LOG is used by another popular (128m downloads) logging crate - env_logger to customize log output. We could integrate env_logger with pipes to offer same level of functionality or use other name here to avoid confusion - users might expect same level of customization, which we don't offer.

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 did consider trying to integrate env_logger initially and can take closer look if we want to add that level of customization or can change the name

pub struct Config {
/// Disables automatic configuration of logrus to use the shim FIFO
pub no_setup_logger: bool,
// Sets the the default log level. Default is info
pub default_log_level: String,
Copy link
Member

Choose a reason for hiding this comment

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

nit: Is this case sensitive?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

from my testing it was not

Copy link
Member

@mxpv mxpv Feb 23, 2024

Choose a reason for hiding this comment

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

default_log_level is a bit confusing to me. We have a default one and env variable to customize it if default doesn't work. Customizing both default value and env log is a bit excessive?

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 was thinking that some shims owners would want to set it different from info with requiring customization through an Environment variable

Copy link
Member

Choose a reason for hiding this comment

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

but they can use environment variable to customize it, if they don't like default value, right?

@mxpv
Copy link
Member

mxpv commented Feb 23, 2024

We identified a perf impact when writing logs.

Would be curious to learn more about costs of logging to pipe/fifo.

@jsturtevant
Copy link
Contributor Author

Would be curious to learn more about costs of logging to pipe/fifo.

@radu-matei @lann could you share some of your findings?

@lann
Copy link

lann commented Feb 23, 2024

I suspect that the performance issues have more to do with mutex contention in FifoLogger, but I haven't done any analysis to confirm.

@mxpv
Copy link
Member

mxpv commented Feb 24, 2024

mutex contention

I wonder if going this route would be more viable instead of introducing a custom env var? parking_lot's mutex or maintaining a message queue.

@jsturtevant
Copy link
Contributor Author

I think it should be both. There is an expectation from users that since they are using env crate they can adjust the log level. In the current set up, it is impossible to get trace logs from the shim or other components since we only go to the debug level.

Copy link
Member

@mxpv mxpv left a comment

Choose a reason for hiding this comment

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

LGTM

@jsturtevant
Copy link
Contributor Author

@mxpv I've logged #249 for further investigation on the logging issue.

Of the feedback in the PR above above which changes would you like to see before merging this?

@mxpv mxpv added this pull request to the merge queue Mar 6, 2024
@mxpv
Copy link
Member

mxpv commented Mar 6, 2024

Let's have this one in. We can follow up with further improvements.

Merged via the queue into containerd:main with commit 0e77608 Mar 6, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-shim Containerd shim
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants