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

log: add tokio-console support #41

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions examples/watch_pods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,8 @@ use tracing::Instrument;
#[derive(Parser)]
#[clap(version)]
struct Args {
/// The tracing filter used for logs
#[clap(
long,
env = "KUBERT_EXAMPLE_LOG",
default_value = "watch_pods=info,warn"
)]
log_level: kubert::LogFilter,

/// The logging format
#[clap(long, default_value = "plain")]
log_format: kubert::LogFormat,
#[clap(flatten)]
log: kubert::LogArgs,

#[clap(flatten)]
client: kubert::ClientArgs,
Expand All @@ -46,8 +37,7 @@ struct Args {
#[tokio::main]
async fn main() -> Result<()> {
let Args {
log_level,
log_format,
log,
client,
admin,
exit,
Expand All @@ -62,7 +52,7 @@ async fn main() -> Result<()> {
// - an admin server with /live and /ready endpoints
// - a tracing (logging) subscriber
let rt = kubert::Runtime::builder()
.with_log(log_level, log_format)
.with_log_args(log)
.with_admin(admin)
.with_client(client);
let mut runtime = match time::timeout_at(deadline, rt.build()).await {
Expand Down
11 changes: 7 additions & 4 deletions kubert/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ index = [
"tracing",
]
initialized = ["futures-core", "futures-util", "pin-project-lite", "tokio/sync"]
log = ["thiserror", "tracing", "tracing-subscriber"]
log = ["thiserror", "tracing", "tracing-subscriber", "once_cell"]
tokio-console = ["tracing-subscriber", "console-subscriber"]
requeue = ["futures-core", "tokio/macros", "tokio/sync", "tokio-util/time", "tracing"]
runtime = [
"admin",
Expand Down Expand Up @@ -80,6 +81,7 @@ features = [

[dependencies]
ahash = { version = "0.7", optional = true }
console-subscriber = { version = "0.1", optional = true }
drain = { version = "0.1.1", optional = true, default-features = false }
futures-core = { version = "0.3", optional = true, default-features = false }
futures-util = { version = "0.3", optional = true, default-features = false }
Expand All @@ -94,12 +96,13 @@ tokio-util = { version = "0.7", optional = true, default-features = false }
tokio-rustls = { version = "0.23.2", optional = true, default-features = false }
tower-service = { version = "0.3.1", optional = true }
tracing = { version = "0.1.31", optional = true }
once_cell = { version = "1.10", optional = true }

[dependencies.clap]
version = "3.1.0"
optional = true
default-features = false
features = ["derive", "std"]
features = ["derive", "std", "env"]

# Not used directly, but required to ensure that the k8s-openapi dependency is considered part of
# the "deps" graph rather than just the "dev-deps" graph
Expand All @@ -125,7 +128,7 @@ optional = true
default-features = false

[dependencies.tracing-subscriber]
version = "0.3.9"
version = "0.3.11"
optional = true
default-features = false
features = [
Expand All @@ -146,4 +149,4 @@ tracing-subscriber = { version = "0.3", features = ["ansi"] }
[dev-dependencies.tokio]
version = "1.17"
default-features = false
features = ["macros", "test-util"]
features = ["macros", "test-util", "rt-multi-thread"]
3 changes: 2 additions & 1 deletion kubert/src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ impl Bound {
))
}));

let task = tokio::spawn(
let task = crate::spawn_named(
"kubert::admin",
async move {
debug!("Serving");
server.await
Expand Down
26 changes: 25 additions & 1 deletion kubert/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,34 @@ pub use self::client::ClientArgs;
pub use self::initialized::Initialized;

#[cfg(all(feature = "log"))]
pub use self::log::{LogFilter, LogFormat, LogInitError};
pub use self::log::{LogArgs, LogFilter, LogFormat, LogInitError};

#[cfg(all(feature = "runtime"))]
pub use self::runtime::Runtime;

#[cfg(all(feature = "server"))]
pub use self::server::ServerArgs;

#[cfg(all(tokio_unstable, feature = "tokio-console"))]
#[track_caller]
pub(crate) fn spawn_named<T>(
name: &'static str,
f: impl std::future::Future<Output = T> + Send + 'static,
) -> tokio::task::JoinHandle<T>
where
T: Send + 'static,
{
tokio::task::Builder::new().name(name).spawn(f)
}

#[cfg(not(all(tokio_unstable, feature = "tokio-console")))]
#[track_caller]
pub(crate) fn spawn_named<T>(
_: &'static str,
f: impl std::future::Future<Output = T> + Send + 'static,
) -> tokio::task::JoinHandle<T>
where
T: Send + 'static,
{
tokio::spawn(f)
}
Loading