diff --git a/backend/src/controller/crypto.rs b/backend/src/controller/crypto.rs index 11b9fc3b..2f6efbcd 100644 --- a/backend/src/controller/crypto.rs +++ b/backend/src/controller/crypto.rs @@ -46,6 +46,7 @@ impl From for Vec { } impl CryptoController { + #[tracing::instrument(level = "info")] pub fn new(config: &GlobalConfig) -> Self { let rng = rand::SystemRandom::new(); let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap(); @@ -73,9 +74,11 @@ impl CryptoController { .to_vec(); HashValue(hashed) } + #[tracing::instrument(level = "trace", skip_all)] pub fn sign(&self, src: &str) -> Vec { self.signer.sign(src.as_bytes()).as_ref().to_vec() } + #[tracing::instrument(level = "trace", skip_all)] pub fn verify(&self, src: &[u8], signature: &[u8]) -> bool { let peer_public_key = signature::UnparsedPublicKey::new( &signature::ED25519, @@ -83,6 +86,7 @@ impl CryptoController { ); peer_public_key.verify(src, signature).is_ok() } + #[tracing::instrument(level = "trace", skip_all)] pub fn encode(&self, obj: M) -> Result> { let mut raw = bincode::serialize(&obj)?; let signature = self.signer.sign(&raw); @@ -91,6 +95,7 @@ impl CryptoController { Ok(raw) } + #[tracing::instrument(level = "trace", skip_all)] pub fn decode(&self, raw: &[u8]) -> Result { let (raw, signature) = raw.split_at(raw.len() - 64); if !self.verify(raw, signature) { diff --git a/backend/src/controller/duplicate.rs b/backend/src/controller/duplicate.rs index 10ed75e0..9a73001b 100644 --- a/backend/src/controller/duplicate.rs +++ b/backend/src/controller/duplicate.rs @@ -21,9 +21,11 @@ impl DupController { #[cfg(feature = "single-instance")] self.dups.insert((user_id, uuid), result); } + #[tracing::instrument(level = "debug", skip(self))] pub fn check(&self, user_id: i32, uuid: &Uuid) -> Option { #[cfg(feature = "single-instance")] if let Some(x) = self.dups.get(&(user_id, *uuid)) { + log::debug!("duplicated request_id: {}, result: {}", uuid, x); return Some(x); } None diff --git a/backend/src/controller/judger/route/mod.rs b/backend/src/controller/judger/route/mod.rs index 0292a79d..75ef6adf 100644 --- a/backend/src/controller/judger/route/mod.rs +++ b/backend/src/controller/judger/route/mod.rs @@ -76,7 +76,9 @@ pub struct ConnGuard { impl ConnGuard { pub fn report_success(&mut self) { self.upstream.healthy.fetch_add(3, Ordering::Acquire); - self.upstream.healthy.fetch_min(HEALTHY_THRESHOLD, Ordering::Acquire); + self.upstream + .healthy + .fetch_min(HEALTHY_THRESHOLD, Ordering::Acquire); } } @@ -108,6 +110,7 @@ async fn discover( loop { match instance.discover().await { RouteStatus::NewConnection(detail) => { + log::info!("new upstream found: {}", detail.uri); let router = match router.upgrade() { Some(x) => x, None => break, @@ -141,6 +144,8 @@ pub struct Router { } impl Router { + // skip because config contain basic auth secret + #[tracing::instrument(level = "debug",skip_all)] pub async fn new(config: Vec) -> Result, Error> { let self_ = Arc::new(Self { routing_table: Map::new(), diff --git a/backend/src/controller/token.rs b/backend/src/controller/token.rs index 1e032c7f..a1111f38 100644 --- a/backend/src/controller/token.rs +++ b/backend/src/controller/token.rs @@ -189,6 +189,7 @@ impl TokenController { Ok(Some(())) } + #[instrument(skip_all, name="token_removal", fields(uid = user_id))] pub async fn remove_by_user_id( &self, user_id: i32, diff --git a/backend/src/endpoint/contest.rs b/backend/src/endpoint/contest.rs index b618cccc..09558b74 100644 --- a/backend/src/endpoint/contest.rs +++ b/backend/src/endpoint/contest.rs @@ -92,6 +92,7 @@ impl From for ContestInfo { #[async_trait] impl ContestSet for Arc { + #[instrument(skip_all, level = "debug")] async fn list( &self, req: Request, @@ -120,6 +121,7 @@ impl ContestSet for Arc { Ok(Response::new(ListContestResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn search_by_text( &self, req: Request, @@ -146,6 +148,7 @@ impl ContestSet for Arc { Ok(Response::new(ListContestResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn full_info( &self, req: Request, @@ -162,6 +165,7 @@ impl ContestSet for Arc { Ok(Response::new(model.into())) } + #[instrument(skip_all, level = "debug")] async fn create( &self, req: Request, @@ -202,6 +206,7 @@ impl ContestSet for Arc { Ok(Response::new(model.id.unwrap().into())) } + #[instrument(skip_all, level = "debug")] async fn update(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -253,6 +258,7 @@ impl ContestSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn remove(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -264,6 +270,7 @@ impl ContestSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn join(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -298,6 +305,7 @@ impl ContestSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn exit(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -314,6 +322,7 @@ impl ContestSet for Arc { } #[doc = " return up to 10 user"] + #[instrument(skip_all, level = "debug")] async fn rank(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; diff --git a/backend/src/endpoint/education.rs b/backend/src/endpoint/education.rs index de618359..6c26c015 100644 --- a/backend/src/endpoint/education.rs +++ b/backend/src/endpoint/education.rs @@ -90,6 +90,7 @@ impl From for EducationInfo { #[async_trait] impl EducationSet for Arc { + #[instrument(skip_all, level = "debug")] async fn create( &self, req: Request, @@ -118,6 +119,7 @@ impl EducationSet for Arc { Ok(Response::new(model.id.unwrap().into())) } + #[instrument(skip_all, level = "debug")] async fn update(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -148,6 +150,7 @@ impl EducationSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn remove(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -159,6 +162,7 @@ impl EducationSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn link(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -183,6 +187,7 @@ impl EducationSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn unlink(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -208,6 +213,7 @@ impl EducationSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn list_by_problem( &self, req: Request, @@ -234,6 +240,7 @@ impl EducationSet for Arc { Ok(Response::new(ListEducationResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn full_info_by_problem( &self, req: Request, diff --git a/backend/src/endpoint/mod.rs b/backend/src/endpoint/mod.rs index 6083d0ff..6e6170fa 100644 --- a/backend/src/endpoint/mod.rs +++ b/backend/src/endpoint/mod.rs @@ -13,6 +13,7 @@ pub mod tools { pub use super::util::error::Error; pub use crate::grpc::TonicStream; pub use crate::init::db::DB; + pub use tracing::instrument; } pub mod endpoints { pub use super::util::{ diff --git a/backend/src/endpoint/playground.rs b/backend/src/endpoint/playground.rs index a1560b33..2f418313 100644 --- a/backend/src/endpoint/playground.rs +++ b/backend/src/endpoint/playground.rs @@ -9,6 +9,7 @@ impl PlaygroundSet for Arc { #[doc = " Server streaming response type for the Run method."] type RunStream = TonicStream; + #[instrument(skip_all, level = "debug")] async fn run( &self, req: Request, diff --git a/backend/src/endpoint/problem.rs b/backend/src/endpoint/problem.rs index b2a6a07a..e8aaf647 100644 --- a/backend/src/endpoint/problem.rs +++ b/backend/src/endpoint/problem.rs @@ -98,6 +98,7 @@ impl From for ProblemFullInfo { #[async_trait] impl ProblemSet for Arc { + #[instrument(skip_all, level = "debug")] async fn list( &self, req: Request, @@ -126,6 +127,7 @@ impl ProblemSet for Arc { Ok(Response::new(ListProblemResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn search_by_text( &self, req: Request, @@ -152,6 +154,7 @@ impl ProblemSet for Arc { Ok(Response::new(ListProblemResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn full_info( &self, req: Request, @@ -168,6 +171,7 @@ impl ProblemSet for Arc { Ok(Response::new(model.into())) } + #[instrument(skip_all, level = "debug")] async fn create( &self, req: Request, @@ -198,6 +202,7 @@ impl ProblemSet for Arc { Ok(Response::new(model.id.unwrap().into())) } + #[instrument(skip_all, level = "debug")] async fn update(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -240,6 +245,7 @@ impl ProblemSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn remove(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -251,6 +257,7 @@ impl ProblemSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn link(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -275,6 +282,7 @@ impl ProblemSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn unlink(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -299,6 +307,7 @@ impl ProblemSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn publish(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -320,6 +329,7 @@ impl ProblemSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn unpublish(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -341,6 +351,7 @@ impl ProblemSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn full_info_by_contest( &self, req: Request, @@ -368,6 +379,7 @@ impl ProblemSet for Arc { Ok(Response::new(model.into())) } + #[instrument(skip_all, level = "debug")] async fn list_by_contest( &self, req: Request, diff --git a/backend/src/endpoint/submit.rs b/backend/src/endpoint/submit.rs index 0b4faabc..4d331919 100644 --- a/backend/src/endpoint/submit.rs +++ b/backend/src/endpoint/submit.rs @@ -59,6 +59,7 @@ impl From for SubmitInfo { #[async_trait] impl SubmitSet for Arc { + #[instrument(skip_all, level = "debug")] async fn list( &self, req: Request, @@ -88,6 +89,7 @@ impl SubmitSet for Arc { Ok(Response::new(ListSubmitResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn list_by_problem( &self, req: Request, @@ -115,6 +117,7 @@ impl SubmitSet for Arc { Ok(Response::new(ListSubmitResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn info(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -128,6 +131,7 @@ impl SubmitSet for Arc { Ok(Response::new(model.into())) } + #[instrument(skip_all, level = "debug")] async fn create( &self, req: Request, @@ -173,6 +177,7 @@ impl SubmitSet for Arc { Ok(Response::new(self.submit.submit(submit).await?.into())) } + #[instrument(skip_all, level = "debug")] async fn remove(&self, req: Request) -> std::result::Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -193,6 +198,7 @@ impl SubmitSet for Arc { type FollowStream = TonicStream; #[doc = " are not guarantee to yield status"] + #[instrument(skip_all, level = "debug")] async fn follow(&self, req: Request) -> Result, Status> { let (_, req) = self.parse_request(req).await?; @@ -204,6 +210,7 @@ impl SubmitSet for Arc { )) } + #[instrument(skip_all, level = "debug")] async fn rejudge(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -253,6 +260,7 @@ impl SubmitSet for Arc { #[doc = " Server streaming response type for the ListLangs method."] type ListLangsStream = TonicStream; + #[instrument(skip_all, level = "debug")] async fn list_langs(&self, _: Request<()>) -> Result, Status> { let langs = self.submit.list_lang().into_iter().map(|x| Ok(x.into())); diff --git a/backend/src/endpoint/testcase.rs b/backend/src/endpoint/testcase.rs index 5ec82b5d..dd112344 100644 --- a/backend/src/endpoint/testcase.rs +++ b/backend/src/endpoint/testcase.rs @@ -81,6 +81,7 @@ impl From for TestcaseInfo { #[async_trait] impl TestcaseSet for Arc { + #[instrument(skip_all, level = "debug")] async fn list( &self, req: Request, @@ -109,6 +110,7 @@ impl TestcaseSet for Arc { Ok(Response::new(ListTestcaseResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn create( &self, req: Request, @@ -137,6 +139,7 @@ impl TestcaseSet for Arc { Ok(Response::new(model.id.unwrap().into())) } + #[instrument(skip_all, level = "debug")] async fn update(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -166,6 +169,7 @@ impl TestcaseSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn remove(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -177,6 +181,7 @@ impl TestcaseSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn link(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -201,6 +206,7 @@ impl TestcaseSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn unlink(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -260,6 +266,7 @@ impl TestcaseSet for Arc { Ok(Response::new(model.into())) } + #[instrument(skip_all, level = "debug")] async fn list_by_problem( &self, req: Request, diff --git a/backend/src/endpoint/token.rs b/backend/src/endpoint/token.rs index 7343da08..32d38b4d 100644 --- a/backend/src/endpoint/token.rs +++ b/backend/src/endpoint/token.rs @@ -29,6 +29,7 @@ impl From for Token { #[async_trait] impl TokenSet for Arc { + #[instrument(skip_all, level = "debug")] async fn list(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, _) = self.parse_request(req).await?; @@ -44,6 +45,7 @@ impl TokenSet for Arc { list: tokens.into_iter().map(Into::into).collect(), })) } + #[instrument(skip_all, level = "debug")] async fn create(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (_, req) = self.parse_request(req).await?; @@ -73,6 +75,7 @@ impl TokenSet for Arc { Err(Error::PremissionDeny("password").into()) } } + #[instrument(skip_all, level = "debug")] async fn refresh(&self, req: Request<()>) -> Result, Status> { let db = DB.get().unwrap(); let (meta, _, _) = req.into_parts(); @@ -100,6 +103,7 @@ impl TokenSet for Arc { Err(Error::Unauthenticated.into()) } + #[instrument(skip_all, level = "debug")] async fn logout(&self, req: Request<()>) -> Result, Status> { let meta = req.metadata(); diff --git a/backend/src/endpoint/user.rs b/backend/src/endpoint/user.rs index a62822d0..dedda83f 100644 --- a/backend/src/endpoint/user.rs +++ b/backend/src/endpoint/user.rs @@ -94,6 +94,7 @@ impl From for UserPermBytes { #[async_trait] impl UserSet for Arc { + #[instrument(skip_all, level = "debug")] async fn list(&self, req: Request) -> Result, Status> { let (auth, req) = self.parse_request(req).await?; @@ -119,6 +120,7 @@ impl UserSet for Arc { Ok(Response::new(ListUserResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn search_by_text( &self, req: Request, @@ -145,6 +147,7 @@ impl UserSet for Arc { Ok(Response::new(ListUserResponse { list, next_session })) } + #[instrument(skip_all, level = "debug")] async fn full_info(&self, req: Request) -> Result, Status> { let (auth, _) = self.parse_request(req).await?; @@ -154,6 +157,7 @@ impl UserSet for Arc { Err(Status::cancelled("deprecated")) } + #[instrument(skip_all, level = "debug")] async fn create(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -187,6 +191,7 @@ impl UserSet for Arc { Ok(Response::new(model.id.unwrap().into())) } + #[instrument(skip_all, level = "debug")] async fn update(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -229,6 +234,7 @@ impl UserSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn remove(&self, req: Request) -> Result, Status> { let db = DB.get().unwrap(); let (auth, req) = self.parse_request(req).await?; @@ -240,6 +246,7 @@ impl UserSet for Arc { Ok(Response::new(())) } + #[instrument(skip_all, level = "debug")] async fn update_password( &self, req: Request, diff --git a/backend/src/init/db.rs b/backend/src/init/db.rs index e92e1b99..1b0e4642 100644 --- a/backend/src/init/db.rs +++ b/backend/src/init/db.rs @@ -1,7 +1,10 @@ use std::path::PathBuf; use ring::digest; -use sea_orm::{ActiveModelTrait, ActiveValue, Database, DatabaseConnection, Schema, EntityTrait, ConnectionTrait}; +use sea_orm::{ + ActiveModelTrait, ActiveValue, ConnectionTrait, Database, DatabaseConnection, EntityTrait, + Schema, +}; use tokio::fs; use tokio::sync::OnceCell; @@ -26,8 +29,8 @@ pub async fn init(config: &GlobalConfig) { let db: DatabaseConnection = Database::connect(&uri).await.unwrap(); - first_migration(config,&db).await; - + first_migration(config, &db).await; + DB.set(db).unwrap(); log::info!("Database created"); @@ -48,7 +51,11 @@ where E: EntityTrait, { let builder = db.get_database_backend(); - let stmt = builder.build(Schema::new(builder).create_table_from_entity(entity).if_not_exists()); + let stmt = builder.build( + Schema::new(builder) + .create_table_from_entity(entity) + .if_not_exists(), + ); match db.execute(stmt).await { Ok(_) => log::info!("Migrated {}", entity.table_name()), @@ -56,7 +63,7 @@ where } } -pub async fn first_migration(config: &GlobalConfig,db:&DatabaseConnection) { +pub async fn first_migration(config: &GlobalConfig, db: &DatabaseConnection) { // create tables create_table(db, entity::user::Entity).await; create_table(db, entity::token::Entity).await; diff --git a/backend/src/server.rs b/backend/src/server.rs index ac4e9815..52d76a84 100644 --- a/backend/src/server.rs +++ b/backend/src/server.rs @@ -27,8 +27,12 @@ impl Server { pub async fn start() { let config = config::init().await; log::info!("Loading TLS certificate..."); - let cert = fs::read_to_string(&config.grpc.public_pem).await.expect("public key.pem not found"); - let key = fs::read_to_string(&config.grpc.private_pem).await.expect("privite key.pem not found"); + let cert = fs::read_to_string(&config.grpc.public_pem) + .await + .expect("public key.pem not found"); + let key = fs::read_to_string(&config.grpc.private_pem) + .await + .expect("privite key.pem not found"); let identity = transport::Identity::from_pem(cert, key); log::info!("Constructing server...");