2024-10-23 05:12:57 +09:00
|
|
|
#![feature(let_chains)]
|
2024-11-28 10:10:17 +09:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
|
2023-03-18 23:06:55 -04:00
|
|
|
use metrics_exporter_prometheus::PrometheusBuilder;
|
2024-11-21 10:45:03 +09:00
|
|
|
use sentry::IntoDsn;
|
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
|
2023-02-15 19:27:36 -05:00
|
|
|
|
2025-02-23 18:49:31 +00:00
|
|
|
use sentry_tracing::event_from_event;
|
|
|
|
|
|
2024-06-16 21:56:14 +09:00
|
|
|
pub mod db;
|
2025-03-10 15:12:56 +00:00
|
|
|
pub mod runtime_config;
|
2024-12-23 22:31:20 +00:00
|
|
|
pub mod state;
|
2024-06-16 21:56:14 +09:00
|
|
|
|
|
|
|
|
pub mod _config;
|
2023-02-15 19:27:36 -05:00
|
|
|
pub use crate::_config::CONFIG as config;
|
|
|
|
|
|
2024-11-21 10:45:03 +09:00
|
|
|
// functions in this file are only used by the main function below
|
|
|
|
|
|
2025-02-23 18:49:31 +00:00
|
|
|
pub fn init_logging(component: &str) {
|
2025-03-08 12:05:00 +00:00
|
|
|
let sentry_layer =
|
|
|
|
|
sentry_tracing::layer().event_mapper(|md, ctx| match md.metadata().level() {
|
|
|
|
|
&tracing::Level::ERROR => {
|
|
|
|
|
// for some reason this works, but letting the library handle it doesn't
|
|
|
|
|
let event = event_from_event(md, ctx);
|
|
|
|
|
sentry::capture_event(event);
|
|
|
|
|
sentry_tracing::EventMapping::Ignore
|
|
|
|
|
}
|
|
|
|
|
_ => sentry_tracing::EventMapping::Ignore,
|
|
|
|
|
});
|
2025-02-23 18:49:31 +00:00
|
|
|
|
2025-03-08 12:05:00 +00:00
|
|
|
if config.json_log {
|
2024-11-21 10:45:03 +09:00
|
|
|
let mut layer = json_subscriber::layer();
|
|
|
|
|
layer.inner_layer_mut().add_static_field(
|
|
|
|
|
"component",
|
|
|
|
|
serde_json::Value::String(component.to_string()),
|
|
|
|
|
);
|
|
|
|
|
tracing_subscriber::registry()
|
2025-02-23 18:49:31 +00:00
|
|
|
.with(sentry_layer)
|
2025-03-08 12:05:00 +00:00
|
|
|
.with(layer)
|
2024-11-21 10:45:03 +09:00
|
|
|
.with(EnvFilter::from_default_env())
|
2024-09-14 12:19:47 +09:00
|
|
|
.init();
|
2023-02-15 19:27:36 -05:00
|
|
|
} else {
|
2025-02-23 18:49:31 +00:00
|
|
|
tracing_subscriber::registry()
|
|
|
|
|
.with(sentry_layer)
|
2025-03-08 12:05:00 +00:00
|
|
|
.with(tracing_subscriber::fmt::layer())
|
2024-09-14 12:19:47 +09:00
|
|
|
.init();
|
2023-02-15 19:27:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-18 23:06:55 -04:00
|
|
|
|
|
|
|
|
pub fn init_metrics() -> anyhow::Result<()> {
|
|
|
|
|
if config.run_metrics_server {
|
2024-11-28 10:10:17 +09:00
|
|
|
PrometheusBuilder::new()
|
|
|
|
|
.with_http_listener("[::]:9000".parse::<SocketAddr>().unwrap())
|
|
|
|
|
.install()?;
|
2023-03-18 23:06:55 -04:00
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-11-21 10:45:03 +09:00
|
|
|
|
|
|
|
|
pub fn init_sentry() -> sentry::ClientInitGuard {
|
|
|
|
|
sentry::init(sentry::ClientOptions {
|
|
|
|
|
dsn: config
|
|
|
|
|
.sentry_url
|
|
|
|
|
.clone()
|
|
|
|
|
.map(|u| u.into_dsn().unwrap())
|
|
|
|
|
.flatten(),
|
|
|
|
|
release: sentry::release_name!(),
|
|
|
|
|
..Default::default()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! main {
|
|
|
|
|
($component:expr) => {
|
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
|
|
|
let _sentry_guard = libpk::init_sentry();
|
|
|
|
|
// we might also be able to use env!("CARGO_CRATE_NAME") here
|
2025-02-23 18:49:31 +00:00
|
|
|
libpk::init_logging($component);
|
2024-11-21 10:45:03 +09:00
|
|
|
tokio::runtime::Builder::new_multi_thread()
|
|
|
|
|
.enable_all()
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.block_on(async {
|
|
|
|
|
if let Err(err) = libpk::init_metrics() {
|
|
|
|
|
tracing::error!("failed to init metrics collector: {err}");
|
|
|
|
|
};
|
|
|
|
|
tracing::info!("hello world");
|
|
|
|
|
if let Err(err) = real_main().await {
|
|
|
|
|
tracing::error!("failed to run service: {err}");
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|