-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3232: feat: refactor application telemetry r=fnichol a=fnichol This change aggresively refactors the telemetry setup for all services. The enabling/disabling of OpenTelemetry has been removed and is now *always* active and set up. The dynamic tracing level functionality remains but its approach is different. In this current iteration, both the console logger and the OpenTelemetry layer have their own reloadable `EnvFilter` layer on top, whereas before the `EnvFilter` layer was its own third layer in the `Registry` stack. That is: ``` - Registry - console_log - env_filter - otel - env_filter ``` At the moment both of the `EnvFilter`s are updated identically and at the same time, but these may become more independent in future work. Additionally, long-running the signal handling and level-updating tasks have been re-written to be their own structs. Finally, a new pattern is introduced into each of the service binary crates which make use of 2 types from the `tokio-util` crate: `TaskTracker` and `CancellationToken`. These 2 types work extremely well together and make cancellation/shutown signalling and waiting (i.e. a process-wide graceful shutdown) much more straight forward. Future work will embed this idea further down into each service, allowing much of the existing shutdown channel infrastructure to be pulled out. Co-authored-by: Fletcher Nichol <[email protected]>
- Loading branch information
Showing
31 changed files
with
1,139 additions
and
1,045 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,64 @@ | ||
use color_eyre::Result; | ||
use telemetry_application::{ | ||
prelude::*, ApplicationTelemetryClient, TelemetryClient, TelemetryConfig, | ||
}; | ||
use telemetry_application::prelude::*; | ||
use tokio::sync::watch; | ||
use tokio_util::{sync::CancellationToken, task::TaskTracker}; | ||
|
||
mod args; | ||
|
||
const RT_DEFAULT_THREAD_STACK_SIZE: usize = 2 * 1024 * 1024 * 3; | ||
|
||
fn main() { | ||
std::thread::Builder::new() | ||
.stack_size(RT_DEFAULT_THREAD_STACK_SIZE) | ||
.name("bin/council-std::thread".to_owned()) | ||
.spawn(move || { | ||
let runtime = tokio::runtime::Builder::new_multi_thread() | ||
.thread_stack_size(RT_DEFAULT_THREAD_STACK_SIZE) | ||
.thread_name("bin/council-tokio::runtime".to_owned()) | ||
.enable_all() | ||
.build()?; | ||
runtime.block_on(async_main()) | ||
}) | ||
.expect("council thread failed") | ||
.join() | ||
.expect("council thread panicked") | ||
.expect("council thread join failed"); | ||
fn main() -> Result<()> { | ||
let thread_builder = ::std::thread::Builder::new().stack_size(RT_DEFAULT_THREAD_STACK_SIZE); | ||
let thread_handler = thread_builder.spawn(|| { | ||
tokio::runtime::Builder::new_multi_thread() | ||
.thread_stack_size(RT_DEFAULT_THREAD_STACK_SIZE) | ||
.thread_name("bin/council-tokio::runtime") | ||
.enable_all() | ||
.build()? | ||
.block_on(async_main()) | ||
})?; | ||
thread_handler.join().unwrap() | ||
} | ||
|
||
async fn async_main() -> Result<()> { | ||
let shutdown_token = CancellationToken::new(); | ||
let task_tracker = TaskTracker::new(); | ||
|
||
color_eyre::install()?; | ||
let config = TelemetryConfig::builder() | ||
.service_name("council") | ||
.service_namespace("si") | ||
.log_env_var_prefix("SI") | ||
.app_modules(vec!["council", "council_server"]) | ||
.build()?; | ||
let telemetry = telemetry_application::init(config)?; | ||
let mut telemetry = telemetry_application::init(config, &task_tracker, shutdown_token.clone())?; | ||
let args = args::parse(); | ||
|
||
let (_shutdown_request_tx, shutdown_request_rx) = watch::channel(()); | ||
tokio::task::spawn(run(args, telemetry, shutdown_request_rx)).await??; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn run( | ||
args: args::Args, | ||
mut telemetry: ApplicationTelemetryClient, | ||
shutdown_request_rx: watch::Receiver<()>, | ||
) -> Result<()> { | ||
if args.verbose > 0 { | ||
telemetry.set_verbosity(args.verbose.into()).await?; | ||
} | ||
debug!(arguments =?args, "parsed cli arguments"); | ||
|
||
if args.disable_opentelemetry { | ||
telemetry.disable_opentelemetry().await?; | ||
} | ||
task_tracker.close(); | ||
|
||
let config = council_server::server::Config::try_from(args)?; | ||
let server = council_server::Server::new_with_config(config).await?; | ||
let (subscriber_started_tx, _subscriber_started_rx) = watch::channel(()); | ||
server | ||
.run(subscriber_started_tx, shutdown_request_rx.clone()) | ||
.await?; | ||
|
||
// TODO(fnichol): this will eventually go into the signal handler code but at the moment in | ||
// council's case, this is embedded in server library code which is incorrect. At this moment | ||
// in the program however, the main council server has shut down so it's an appropriate time to | ||
// cancel other remaining tasks and wait on their graceful shutdowns | ||
{ | ||
shutdown_token.cancel(); | ||
task_tracker.wait().await; | ||
} | ||
|
||
info!("graceful shutdown complete."); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.