Skip to content

Commit

Permalink
Merge branch 'tracing-update'
Browse files Browse the repository at this point in the history
  • Loading branch information
ackwell committed Aug 1, 2024
2 parents 4b8d9bb + d806e3a commit e8fc424
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 17 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ tokio = { version = "1.32.0", features = ["full", "tracing"] }
tokio-util = "0.7.4"
tower-http = { version = "0.5.2", features = ["cors", "trace"] }
tracing = "0.1.34"
tracing-subscriber = "0.3.11"
tracing-subscriber = { version = "0.3.11", features = ["json"] }
uuid = { version = "1.3.2", features = ["v4", "fast-rng", "serde"] }

[dev-dependencies]
Expand Down
11 changes: 8 additions & 3 deletions boilmaster.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[tracing.filters]
default = "debug"
hyper = "info"
[tracing.console]
enabled = false

[tracing.stdout]
enabled = true
format = "full"
filters.default = "debug"
filters.hyper = "info"

[http]
# address = "0.0.0.0"
Expand Down
76 changes: 63 additions & 13 deletions src/tracing.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
use std::{collections::HashMap, fmt, str::FromStr};

use serde::{de, Deserialize};
use tracing::metadata::LevelFilter;
use tracing_subscriber::{filter, layer::SubscriberExt, util::SubscriberInitExt, Layer};
use tracing::{metadata::LevelFilter, Subscriber};
use tracing_subscriber::{
filter, layer::SubscriberExt, registry::LookupSpan, util::SubscriberInitExt, Layer,
};

// TODO: tracing should proooobably be it's own file at this point
#[derive(Debug, Deserialize)]
pub struct Config {
// TODO: log file config? or like, sink config? work out how that's going to work i guess.
console: ConsoleConfig,
stdout: StdoutConfig,
}

#[derive(Debug, Deserialize)]
struct ConsoleConfig {
enabled: bool,
}

#[derive(Debug, Deserialize)]
struct StdoutConfig {
enabled: bool,
format: StdoutFormat,
filters: TracingFilters,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
enum StdoutFormat {
Full,
Json,
}

#[derive(Debug, Deserialize)]
struct TracingFilters {
default: ConfigLevelFilter,
Expand Down Expand Up @@ -46,19 +66,49 @@ impl<'de> Deserialize<'de> for ConfigLevelFilter {
}

pub fn init(config: Config) {
// TODO: consider enabling this with a config flag or something tracing.console?
let console_filter = filter::Targets::new()
// TODO: env filter (will need feature enabled). consider enabling pulling from log! too.
// TODO: now that i have config working, is it worth using env filter here or should i handle it via config env?
tracing_subscriber::registry()
.with(tokio_console(config.console))
.with(stdout(config.stdout))
.init();
}

fn tokio_console<S>(config: ConsoleConfig) -> Option<impl Layer<S>>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
if !config.enabled {
return None;
}

let layer = console_subscriber::spawn();

let filter = filter::Targets::new()
.with_target("tokio", LevelFilter::TRACE)
.with_target("runtime", LevelFilter::TRACE);

let tracing_filter = filter::Targets::new()
Some(layer.with_filter(filter))
}

fn stdout<S>(config: StdoutConfig) -> Option<impl Layer<S>>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
if !config.enabled {
return None;
}

let layer = tracing_subscriber::fmt::layer();

let layer = match config.format {
StdoutFormat::Full => layer.boxed(),
StdoutFormat::Json => layer.json().boxed(),
};

let filter = filter::Targets::new()
.with_default(config.filters.default)
.with_targets(config.filters.targets);

// TODO: env filter (will need feature enabled). consider enabling pulling from log! too.
// TODO: now that i have config working, is it worth using env filter here or should i handle it via config env?
tracing_subscriber::registry()
.with(console_subscriber::spawn().with_filter(console_filter))
.with(tracing_subscriber::fmt::layer().with_filter(tracing_filter))
.init();
Some(layer.with_filter(filter))
}

0 comments on commit e8fc424

Please sign in to comment.