use crate::AppConfig;
use tokio::runtime::Runtime;
pub trait RuntimeWiring {
fn run(&self, _config: &'static AppConfig) -> Runtime {
#[cfg(feature = "sentry")]
let sentry_guard = self.init_sentry(_config);
#[cfg(feature = "tracing")]
{
let tracing_subscriber = self.make_tracing_subscriber(_config);
self.init_tracing_subscriber(tracing_subscriber);
}
let runtime = self.make_runtime();
#[cfg(feature = "sentry")]
self.schedule_sentry_flushing(&runtime, sentry_guard);
runtime
}
#[cfg(feature = "sentry")]
fn init_sentry(&self, config: &'static AppConfig) -> strut_sentry::SentryGuard {
strut_sentry::SentryIntegration::init(config.sentry())
}
#[cfg(feature = "tracing-log")]
fn init_log_tracer(&self, config: &'static AppConfig) {
use tracing_log::{AsLog, LogTracer};
let tracing_config = config.tracing();
LogTracer::builder()
.with_max_level(
tracing_config
.verbosity()
.to_tracing_level_filter()
.as_log(),
)
.init()
.expect("failed to initialize log tracer");
}
#[cfg(feature = "tracing")]
fn make_tracing_subscriber(
&self,
config: &'static AppConfig,
) -> Box<dyn strut_tracing::Subscriber + Send + Sync> {
use strut_tracing::SubscriberExt;
let registry = strut_tracing::Registry::default();
let registry = registry.with(strut_tracing::make_layer(config.tracing()));
#[cfg(feature = "sentry")]
let registry = registry.with(strut_sentry::tracing::make_layer());
Box::new(registry)
}
#[cfg(feature = "tracing")]
fn init_tracing_subscriber(
&self,
subscriber: Box<dyn strut_tracing::Subscriber + Send + Sync>,
) {
use strut_tracing::SubscriberInitExt;
subscriber.init()
}
fn make_runtime(&self) -> Runtime {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.expect("it should be possible to build a tokio runtime")
}
#[cfg(feature = "sentry")]
fn schedule_sentry_flushing(&self, runtime: &Runtime, sentry_guard: strut_sentry::SentryGuard) {
strut_sentry::SentryIntegration::schedule_flushing(runtime, sentry_guard);
}
}
pub(crate) struct DefaultRuntimeWiring;
impl RuntimeWiring for DefaultRuntimeWiring {}