mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-01 21:13:09 +00:00
re-add share stats
This commit is contained in:
committed by
Aiden McClelland
parent
c3ac27865d
commit
77d34c2a64
@@ -2,9 +2,14 @@ use std::collections::BTreeMap;
|
||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
use reqwest::Url;
|
||||
use reqwest::{Client, Url};
|
||||
use sequence_trie::SequenceTrie;
|
||||
use serde::Serialize;
|
||||
use tracing::Subscriber;
|
||||
use tracing_subscriber::filter::LevelFilter;
|
||||
use tracing_subscriber::Layer;
|
||||
|
||||
use crate::version::COMMIT_HASH;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ModuleMap {
|
||||
@@ -81,48 +86,92 @@ impl ModuleMap {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EmbassyLogger {
|
||||
log_level: LevelFilter,
|
||||
pub struct SharingLayer {
|
||||
log_epoch: Arc<AtomicU64>,
|
||||
sharing: Arc<AtomicBool>,
|
||||
share_dest: String,
|
||||
}
|
||||
impl<S: Subscriber> Layer<S> for SharingLayer {
|
||||
fn on_event(
|
||||
&self,
|
||||
event: &tracing::Event<'_>,
|
||||
_ctx: tracing_subscriber::layer::Context<'_, S>,
|
||||
) {
|
||||
if self.sharing.load(Ordering::SeqCst) {
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
struct LogRequest<'a> {
|
||||
log_epoch: u64,
|
||||
commit_hash: &'static str,
|
||||
file: Option<&'a str>,
|
||||
line: Option<u32>,
|
||||
target: &'a str,
|
||||
level: &'static str,
|
||||
message: Option<String>,
|
||||
}
|
||||
if event.metadata().level() <= &tracing::Level::WARN {
|
||||
let body = LogRequest {
|
||||
log_epoch: self.log_epoch.load(Ordering::SeqCst),
|
||||
commit_hash: COMMIT_HASH,
|
||||
file: event.metadata().file(),
|
||||
line: event.metadata().line(),
|
||||
target: event.metadata().target(),
|
||||
level: event.metadata().level().as_str(),
|
||||
message: event
|
||||
.fields()
|
||||
.find(|f| f.name() == "message")
|
||||
.map(|f| f.to_string()),
|
||||
};
|
||||
// we don't care about the result and need it to be fast
|
||||
tokio::spawn(Client::new().post(&self.share_dest).json(&body).send());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EmbassyLogger {
|
||||
log_epoch: Arc<AtomicU64>,
|
||||
sharing: Arc<AtomicBool>,
|
||||
share_dest: Url,
|
||||
module_map: ModuleMap,
|
||||
}
|
||||
impl EmbassyLogger {
|
||||
pub fn init(
|
||||
log_level: LevelFilter,
|
||||
log_epoch: Arc<AtomicU64>,
|
||||
share_dest: Option<Url>,
|
||||
share_errors: bool,
|
||||
module_map: BTreeMap<String, LevelFilter>,
|
||||
) -> Self {
|
||||
let share_dest = match share_dest {
|
||||
None => Url::parse("https://beta-registry-0-3.start9labs.com/error-logs").unwrap(), // TODO
|
||||
Some(a) => a,
|
||||
};
|
||||
fn base_subscriber() -> impl Subscriber {
|
||||
use tracing_error::ErrorLayer;
|
||||
use tracing_subscriber::prelude::*;
|
||||
use tracing_subscriber::{fmt, EnvFilter};
|
||||
|
||||
let fmt_layer = fmt::layer().with_target(false);
|
||||
let filter_layer = EnvFilter::from_default_env().add_directive(log_level.into());
|
||||
let filter_layer = EnvFilter::from_default_env();
|
||||
let fmt_layer = fmt::layer().with_target(true);
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(filter_layer)
|
||||
.with(fmt_layer)
|
||||
.with(ErrorLayer::default())
|
||||
.init();
|
||||
}
|
||||
pub fn no_sharing() {
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
Self::base_subscriber().init();
|
||||
color_eyre::install().expect("Color Eyre Init");
|
||||
let embassy_logger = EmbassyLogger {
|
||||
log_level,
|
||||
log_epoch,
|
||||
sharing: Arc::new(AtomicBool::new(share_errors)),
|
||||
share_dest: share_dest,
|
||||
module_map: ModuleMap::new(module_map),
|
||||
}
|
||||
pub fn init(log_epoch: Arc<AtomicU64>, share_dest: Option<Url>, share_errors: bool) -> Self {
|
||||
use tracing_subscriber::prelude::*;
|
||||
|
||||
let share_dest = match share_dest {
|
||||
None => "https://beta-registry-0-3.start9labs.com/error-logs".to_owned(), // TODO
|
||||
Some(a) => a.to_string(),
|
||||
};
|
||||
embassy_logger
|
||||
let sharing = Arc::new(AtomicBool::new(share_errors));
|
||||
let sharing_layer = SharingLayer {
|
||||
log_epoch: log_epoch.clone(),
|
||||
share_dest,
|
||||
sharing: sharing.clone(),
|
||||
};
|
||||
|
||||
Self::base_subscriber().with(sharing_layer).init();
|
||||
color_eyre::install().expect("Color Eyre Init");
|
||||
|
||||
EmbassyLogger { log_epoch, sharing }
|
||||
}
|
||||
pub fn set_sharing(&self, sharing: bool) {
|
||||
self.sharing.store(sharing, Ordering::SeqCst)
|
||||
|
||||
Reference in New Issue
Block a user