Skip to content

Commit

Permalink
add an environment-configured console_subscriber::init
Browse files Browse the repository at this point in the history
  • Loading branch information
jbr committed Jun 18, 2021
1 parent b345084 commit f8edc47
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 15 deletions.
14 changes: 1 addition & 13 deletions console-subscriber/examples/app.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
use std::time::Duration;
use tracing_subscriber::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (layer, server) = console_subscriber::TasksLayer::builder()
.retention(Duration::from_secs(60))
.build();
let filter =
tracing_subscriber::EnvFilter::from_default_env().add_directive("tokio=trace".parse()?);
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(filter)
.with(layer)
.init();
console_subscriber::init();

let serve = tokio::spawn(async move { server.serve().await.expect("server failed") });
let task1 = tokio::spawn(spawn_tasks(1, 10));
let task2 = tokio::spawn(spawn_tasks(10, 100));
let result = tokio::try_join! {
task1,
task2,
serve
};
result?;

Expand Down
42 changes: 41 additions & 1 deletion console-subscriber/src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{Server, TasksLayer};
use std::{net::SocketAddr, time::Duration};
use std::{
net::{SocketAddr, ToSocketAddrs},
time::Duration,
};

/// Builder for configuring [`TasksLayer`]s.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -101,4 +104,41 @@ impl Builder {
pub fn build(self) -> (TasksLayer, Server) {
TasksLayer::build(self)
}

/// Configures this builder from a standard set of environment variables:
///
/// * `TOKIO_CONSOLE_RETENTION`: The number of seconds to accumulate
/// completed tracing data. Default: 60s.
/// * `TOKIO_CONSOLE_BIND`: a HOST:PORT description, such as
/// localhost:1234 or similar. Default: 127.0.0.1:6669
/// * `TOKIO_CONSOLE_PUBLISH_INTERVAL`: The number of milliseconds to wait
/// between sending updates to the console. Default: 1000ms (1s)
pub fn from_default_env(mut self) -> Self {
if let Ok(retention) = std::env::var("TOKIO_CONSOLE_RETENTION") {
self.retention = Duration::from_secs(
retention
.parse()
.expect("TOKIO_CONSOLE_RETENTION must be an integer"),
);
}

if let Ok(bind) = std::env::var("TOKIO_CONSOLE_BIND") {
self.server_addr = bind
.to_socket_addrs()
.expect("TOKIO_CONSOLE_BIND must be formatted as HOST:PORT, such as localhost:4321")
.next()
.expect("tokio console could not resolve TOKIO_CONSOLE_BIND");
}

if let Ok(interval) = std::env::var("TOKIO_CONSOLE_PUBLISH_INTERVAL") {
self.publish_interval = Duration::from_millis(
interval
.parse()
.expect("TOKIO_CONSOLE_PUBLISH_INTERVAL must be an integer"),
);
}

self
}
}
58 changes: 57 additions & 1 deletion console-subscriber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,69 @@ use tracing_core::{
subscriber::{self, Subscriber},
Metadata,
};
use tracing_subscriber::{layer::Context, registry::LookupSpan, Layer};
use tracing_subscriber::{fmt, layer::Context, registry::LookupSpan, EnvFilter, Layer};

mod aggregator;
use aggregator::Aggregator;
mod builder;
pub use builder::Builder;

/**
Starts the console subscriber server on its own thread
This function represents the easiest way to get started using
tokio-console.
## Configuration
Tokio console subscriber is configured with sensible defaults for most
use cases. If you need to tune these parameters, several environmental
configuration variables are available:
* `TOKIO_CONSOLE_RETENTION`: The number of seconds to accumulate
completed tracing data. Default: 60s.
* `TOKIO_CONSOLE_BIND`: a HOST:PORT description, such as
localhost:1234 or similar. Default: 127.0.0.1:6669
* `TOKIO_CONSOLE_PUBLISH_INTERVAL`: The number of milliseconds to wait
between sending updates to the console. Default: 1000ms (1s)
* `RUST_LOG`: configure the tracing filter. Default: `tokio=trace`,
and any additional filtering directives will be appended to this
default. See [`EnvFilter`] for further information on the format
for this variable.
*/
pub fn init() {
std::thread::Builder::new()
.name("console_subscriber".into())
.spawn(|| {
use tracing_subscriber::prelude::*;

let (layer, server) = TasksLayer::builder().from_default_env().build();

let filter =
EnvFilter::from_default_env().add_directive("tokio=trace".parse().unwrap());

tracing_subscriber::registry()
.with(fmt::layer())
.with(filter)
.with(layer)
.init();

let runtime = tokio::runtime::Builder::new_current_thread()
.enable_io()
.enable_time()
.build()
.expect("console subscriber runtime initialization failed");

runtime.block_on(async move {
server
.serve()
.await
.expect("console subscriber server failed")
});
})
.expect("console subscriber could not spawn thread");
}

pub struct TasksLayer {
task_meta: AtomicPtr<Metadata<'static>>,
blocking_meta: AtomicPtr<Metadata<'static>>,
Expand Down

0 comments on commit f8edc47

Please sign in to comment.