mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-04-04 06:19:44 +00:00
remove error log reporting from backend
This commit is contained in:
@@ -20,7 +20,7 @@ fn inner_main() -> Result<(), Error> {
|
|||||||
.arg(Arg::with_name("host").long("host").short("h").takes_value(true))
|
.arg(Arg::with_name("host").long("host").short("h").takes_value(true))
|
||||||
.arg(Arg::with_name("proxy").long("proxy").short("p").takes_value(true)),
|
.arg(Arg::with_name("proxy").long("proxy").short("p").takes_value(true)),
|
||||||
context: matches => {
|
context: matches => {
|
||||||
EmbassyLogger::no_sharing();
|
EmbassyLogger::init();
|
||||||
CliContext::init(matches)?
|
CliContext::init(matches)?
|
||||||
},
|
},
|
||||||
exit: |e: RpcError| {
|
exit: |e: RpcError| {
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ fn main() {
|
|||||||
)
|
)
|
||||||
.get_matches();
|
.get_matches();
|
||||||
|
|
||||||
EmbassyLogger::no_sharing();
|
EmbassyLogger::init();
|
||||||
|
|
||||||
let cfg_path = matches.value_of("config");
|
let cfg_path = matches.value_of("config");
|
||||||
let res = {
|
let res = {
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ fn inner_main() -> Result<(), Error> {
|
|||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
),
|
),
|
||||||
context: matches => {
|
context: matches => {
|
||||||
EmbassyLogger::no_sharing();
|
EmbassyLogger::init();
|
||||||
SdkContext::init(matches)?
|
SdkContext::init(matches)?
|
||||||
},
|
},
|
||||||
exit: |e: RpcError| {
|
exit: |e: RpcError| {
|
||||||
|
|||||||
@@ -129,7 +129,6 @@ pub struct RpcContextSeed {
|
|||||||
pub shutdown: broadcast::Sender<Option<Shutdown>>,
|
pub shutdown: broadcast::Sender<Option<Shutdown>>,
|
||||||
pub websocket_count: AtomicUsize,
|
pub websocket_count: AtomicUsize,
|
||||||
pub logger: EmbassyLogger,
|
pub logger: EmbassyLogger,
|
||||||
pub log_epoch: Arc<AtomicU64>,
|
|
||||||
pub tor_socks: SocketAddr,
|
pub tor_socks: SocketAddr,
|
||||||
pub notification_manager: NotificationManager,
|
pub notification_manager: NotificationManager,
|
||||||
pub open_authed_websockets: Mutex<BTreeMap<HashSessionToken, Vec<oneshot::Sender<()>>>>,
|
pub open_authed_websockets: Mutex<BTreeMap<HashSessionToken, Vec<oneshot::Sender<()>>>>,
|
||||||
@@ -151,24 +150,13 @@ impl RpcContext {
|
|||||||
Ipv4Addr::new(127, 0, 0, 1),
|
Ipv4Addr::new(127, 0, 0, 1),
|
||||||
9050,
|
9050,
|
||||||
)));
|
)));
|
||||||
let logger = EmbassyLogger::init(
|
let logger = EmbassyLogger::init();
|
||||||
base.log_server.clone(),
|
|
||||||
false,
|
|
||||||
tor_proxy.ip(),
|
|
||||||
tor_proxy.port(),
|
|
||||||
)?;
|
|
||||||
tracing::info!("Set Logger");
|
tracing::info!("Set Logger");
|
||||||
let (shutdown, _) = tokio::sync::broadcast::channel(1);
|
let (shutdown, _) = tokio::sync::broadcast::channel(1);
|
||||||
let secret_store = base.secret_store().await?;
|
let secret_store = base.secret_store().await?;
|
||||||
tracing::info!("Opened Sqlite DB");
|
tracing::info!("Opened Sqlite DB");
|
||||||
let db = base.db(&secret_store, &get_product_key().await?).await?;
|
let db = base.db(&secret_store, &get_product_key().await?).await?;
|
||||||
tracing::info!("Opened PatchDB");
|
tracing::info!("Opened PatchDB");
|
||||||
let share = crate::db::DatabaseModel::new()
|
|
||||||
.server_info()
|
|
||||||
.share_stats()
|
|
||||||
.get(&mut db.handle(), true)
|
|
||||||
.await?;
|
|
||||||
logger.set_sharing(*share);
|
|
||||||
let docker = Docker::connect_with_unix_defaults()?;
|
let docker = Docker::connect_with_unix_defaults()?;
|
||||||
tracing::info!("Connected to Docker");
|
tracing::info!("Connected to Docker");
|
||||||
let net_controller = NetController::init(
|
let net_controller = NetController::init(
|
||||||
@@ -202,7 +190,6 @@ impl RpcContext {
|
|||||||
metrics_cache,
|
metrics_cache,
|
||||||
shutdown,
|
shutdown,
|
||||||
websocket_count: AtomicUsize::new(0),
|
websocket_count: AtomicUsize::new(0),
|
||||||
log_epoch: logger.epoch(),
|
|
||||||
logger,
|
logger,
|
||||||
tor_socks: tor_proxy,
|
tor_socks: tor_proxy,
|
||||||
notification_manager,
|
notification_manager,
|
||||||
|
|||||||
@@ -43,20 +43,6 @@ async fn ws_handler<
|
|||||||
.with_kind(crate::ErrorKind::Network)?
|
.with_kind(crate::ErrorKind::Network)?
|
||||||
.with_kind(crate::ErrorKind::Unknown)?;
|
.with_kind(crate::ErrorKind::Unknown)?;
|
||||||
|
|
||||||
// add 1 to the session counter and issue an RAII guard to subtract 1 on drop
|
|
||||||
ctx.websocket_count
|
|
||||||
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
|
||||||
let _decrementer = GeneralGuard::new(|| {
|
|
||||||
let new_count = ctx
|
|
||||||
.websocket_count
|
|
||||||
.fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
|
|
||||||
if new_count == 0 {
|
|
||||||
ctx.log_epoch
|
|
||||||
.store(rand::random(), std::sync::atomic::Ordering::SeqCst)
|
|
||||||
}
|
|
||||||
()
|
|
||||||
});
|
|
||||||
|
|
||||||
let (has_valid_session, token) = loop {
|
let (has_valid_session, token) = loop {
|
||||||
if let Some(Message::Text(cookie)) = stream
|
if let Some(Message::Text(cookie)) = stream
|
||||||
.next()
|
.next()
|
||||||
|
|||||||
@@ -65,7 +65,6 @@ impl Database {
|
|||||||
tor: Vec::new(),
|
tor: Vec::new(),
|
||||||
clearnet: Vec::new(),
|
clearnet: Vec::new(),
|
||||||
},
|
},
|
||||||
share_stats: false,
|
|
||||||
password_hash,
|
password_hash,
|
||||||
},
|
},
|
||||||
package_data: AllPackageData::default(),
|
package_data: AllPackageData::default(),
|
||||||
@@ -97,7 +96,6 @@ pub struct ServerInfo {
|
|||||||
pub wifi: WifiInfo,
|
pub wifi: WifiInfo,
|
||||||
pub unread_notification_count: u64,
|
pub unread_notification_count: u64,
|
||||||
pub connection_addresses: ConnectionAddresses,
|
pub connection_addresses: ConnectionAddresses,
|
||||||
pub share_stats: bool,
|
|
||||||
pub password_hash: String,
|
pub password_hash: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,6 @@ pub fn main_api() -> Result<(), RpcError> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[command(subcommands(
|
#[command(subcommands(
|
||||||
system::config,
|
|
||||||
system::logs,
|
system::logs,
|
||||||
system::metrics,
|
system::metrics,
|
||||||
shutdown::shutdown,
|
shutdown::shutdown,
|
||||||
|
|||||||
@@ -628,28 +628,6 @@ async fn get_disk_info() -> Result<MetricsDisk, Error> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[command(subcommands(share_stats))]
|
|
||||||
pub async fn config() -> Result<(), Error> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[command(rename = "share-stats", display(display_none))]
|
|
||||||
async fn share_stats(
|
|
||||||
#[context] ctx: RpcContext,
|
|
||||||
#[arg] value: bool,
|
|
||||||
) -> Result<WithRevision<()>, Error> {
|
|
||||||
let revision = crate::db::DatabaseModel::new()
|
|
||||||
.server_info()
|
|
||||||
.share_stats()
|
|
||||||
.put(&mut ctx.db.handle(), &value)
|
|
||||||
.await?;
|
|
||||||
ctx.logger.set_sharing(value);
|
|
||||||
Ok(WithRevision {
|
|
||||||
response: (),
|
|
||||||
revision,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
pub async fn test_get_temp() {
|
pub async fn test_get_temp() {
|
||||||
println!("{}", get_temp().await.unwrap())
|
println!("{}", get_temp().await.unwrap())
|
||||||
|
|||||||
@@ -1,94 +1,9 @@
|
|||||||
use std::net::{IpAddr, Ipv4Addr};
|
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use reqwest::{Client, Proxy, Url};
|
|
||||||
use serde::Serialize;
|
|
||||||
use tracing::Subscriber;
|
use tracing::Subscriber;
|
||||||
use tracing_subscriber::Layer;
|
use tracing_subscriber::util::SubscriberInitExt;
|
||||||
|
|
||||||
use crate::version::COMMIT_HASH;
|
|
||||||
use crate::{Error, ResultExt};
|
|
||||||
|
|
||||||
pub struct SharingLayer {
|
|
||||||
log_epoch: Arc<AtomicU64>,
|
|
||||||
sharing: Arc<AtomicBool>,
|
|
||||||
share_dest: String,
|
|
||||||
tor_proxy: Client,
|
|
||||||
}
|
|
||||||
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: String,
|
|
||||||
commit_hash: &'static str,
|
|
||||||
file: Option<&'a str>,
|
|
||||||
line: Option<u32>,
|
|
||||||
target: &'a str,
|
|
||||||
level: &'static str,
|
|
||||||
log_message: Option<String>,
|
|
||||||
}
|
|
||||||
if event.metadata().level() <= &tracing::Level::WARN {
|
|
||||||
struct Visitor(Option<String>);
|
|
||||||
impl tracing::field::Visit for Visitor {
|
|
||||||
fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
|
|
||||||
if field.name() == "message" {
|
|
||||||
self.0 = Some(value.to_owned());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn record_error(
|
|
||||||
&mut self,
|
|
||||||
field: &tracing::field::Field,
|
|
||||||
value: &(dyn std::error::Error + 'static),
|
|
||||||
) {
|
|
||||||
if field.name() == "message" {
|
|
||||||
self.0 = Some(value.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn record_debug(
|
|
||||||
&mut self,
|
|
||||||
field: &tracing::field::Field,
|
|
||||||
value: &dyn std::fmt::Debug,
|
|
||||||
) {
|
|
||||||
if field.name() == "message" {
|
|
||||||
self.0 = Some(format!("{:?}", value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut message = Visitor(None);
|
|
||||||
event.record(&mut message);
|
|
||||||
let body = LogRequest {
|
|
||||||
log_epoch: self.log_epoch.load(Ordering::SeqCst).to_string(),
|
|
||||||
commit_hash: COMMIT_HASH,
|
|
||||||
file: event.metadata().file(),
|
|
||||||
line: event.metadata().line(),
|
|
||||||
target: event.metadata().target(),
|
|
||||||
level: event.metadata().level().as_str(),
|
|
||||||
log_message: message.0,
|
|
||||||
};
|
|
||||||
// we don't care about the result and need it to be fast
|
|
||||||
tokio::spawn(self.tor_proxy.post(&self.share_dest).json(&body).send());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref LOGGER: Mutex<Option<(Arc<AtomicU64>, Arc<AtomicBool>)>> = Mutex::new(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct EmbassyLogger {
|
pub struct EmbassyLogger {}
|
||||||
log_epoch: Arc<AtomicU64>,
|
|
||||||
sharing: Arc<AtomicBool>,
|
|
||||||
}
|
|
||||||
impl EmbassyLogger {
|
impl EmbassyLogger {
|
||||||
fn base_subscriber() -> impl Subscriber {
|
fn base_subscriber() -> impl Subscriber {
|
||||||
use tracing_error::ErrorLayer;
|
use tracing_error::ErrorLayer;
|
||||||
@@ -103,55 +18,11 @@ impl EmbassyLogger {
|
|||||||
.with(fmt_layer)
|
.with(fmt_layer)
|
||||||
.with(ErrorLayer::default())
|
.with(ErrorLayer::default())
|
||||||
}
|
}
|
||||||
pub fn no_sharing() {
|
pub fn init() -> Self {
|
||||||
let _ = Self::init(None, false, IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 9050);
|
Self::base_subscriber().init();
|
||||||
}
|
|
||||||
pub fn init(
|
|
||||||
share_dest: Option<Url>,
|
|
||||||
share_errors: bool,
|
|
||||||
tor_proxy_ip: IpAddr,
|
|
||||||
tor_proxy_port: u16,
|
|
||||||
) -> Result<Self, Error> {
|
|
||||||
use tracing_subscriber::prelude::*;
|
|
||||||
|
|
||||||
let mut guard = LOGGER.lock().unwrap();
|
|
||||||
let (log_epoch, sharing) = if let Some((log_epoch, sharing)) = guard.take() {
|
|
||||||
sharing.store(share_errors, Ordering::SeqCst);
|
|
||||||
(log_epoch, sharing)
|
|
||||||
} else {
|
|
||||||
let log_epoch = Arc::new(AtomicU64::new(rand::random()));
|
|
||||||
let sharing = Arc::new(AtomicBool::new(share_errors));
|
|
||||||
let share_dest = match share_dest {
|
|
||||||
None => "http://registry.privacy34kn4ez3y3nijweec6w4g54i3g54sdv7r5mr6soma3w4begyd.onion/support/v0/error-logs".to_owned(),
|
|
||||||
Some(a) => a.to_string(),
|
|
||||||
};
|
|
||||||
let tor_proxy = Client::builder()
|
|
||||||
.proxy(
|
|
||||||
Proxy::http(format!("socks5h://{}:{}", tor_proxy_ip, tor_proxy_port))
|
|
||||||
.with_kind(crate::ErrorKind::Network)?,
|
|
||||||
)
|
|
||||||
.build()
|
|
||||||
.with_kind(crate::ErrorKind::Network)?;
|
|
||||||
let sharing_layer = SharingLayer {
|
|
||||||
log_epoch: log_epoch.clone(),
|
|
||||||
share_dest,
|
|
||||||
sharing: sharing.clone(),
|
|
||||||
tor_proxy,
|
|
||||||
};
|
|
||||||
|
|
||||||
Self::base_subscriber().with(sharing_layer).init();
|
|
||||||
color_eyre::install().expect("Color Eyre Init");
|
color_eyre::install().expect("Color Eyre Init");
|
||||||
(log_epoch, sharing)
|
|
||||||
};
|
|
||||||
*guard = Some((log_epoch.clone(), sharing.clone()));
|
|
||||||
|
|
||||||
Ok(EmbassyLogger { log_epoch, sharing })
|
EmbassyLogger {}
|
||||||
}
|
|
||||||
pub fn epoch(&self) -> Arc<AtomicU64> {
|
|
||||||
self.log_epoch.clone()
|
|
||||||
}
|
|
||||||
pub fn set_sharing(&self, sharing: bool) {
|
|
||||||
self.sharing.store(sharing, Ordering::SeqCst)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user