From 430ebec81068a2d4681b2a8119211bafbe0816db Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 13 Dec 2022 23:36:40 -0600 Subject: [PATCH] Improve tracing, immediately stringify spantrace, remove join macros --- src/config.rs | 1 - src/data/actor.rs | 12 ++++-------- src/data/node.rs | 8 +++----- src/error.rs | 4 ++-- src/extractors.rs | 16 ++++++++-------- src/routes/inbox.rs | 8 +++----- src/routes/nodeinfo.rs | 16 ++++++++-------- src/routes/statics.rs | 2 +- 8 files changed, 29 insertions(+), 38 deletions(-) diff --git a/src/config.rs b/src/config.rs index 61aa7a9..957cbc8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -396,7 +396,6 @@ impl Config { self.do_generate_url(kind).expect("Generated valid IRI") } - #[tracing::instrument(level = "debug", skip_all, fields(base_uri = tracing::field::debug(&self.base_uri), kind = tracing::field::debug(&kind)))] fn do_generate_url(&self, kind: UrlKind) -> Result { let iri = match kind { UrlKind::Activity => FixedBaseResolver::new(self.base_uri.as_ref()) diff --git a/src/data/actor.rs b/src/data/actor.rs index c0b3ddd..784e99f 100644 --- a/src/data/actor.rs +++ b/src/data/actor.rs @@ -37,7 +37,7 @@ impl ActorCache { ActorCache { db } } - #[tracing::instrument(level = "debug" name = "Get Actor", skip_all, fields(id = id.to_string().as_str(), requests))] + #[tracing::instrument(level = "debug" name = "Get Actor", skip_all, fields(id = id.to_string().as_str()))] pub(crate) async fn get( &self, id: &IriString, @@ -56,12 +56,8 @@ impl ActorCache { #[tracing::instrument(level = "debug", name = "Add Connection", skip(self))] pub(crate) async fn add_connection(&self, actor: Actor) -> Result<(), Error> { - let add_connection = self.db.add_connection(actor.id.clone()); - let save_actor = self.db.save_actor(actor); - - tokio::try_join!(add_connection, save_actor)?; - - Ok(()) + self.db.add_connection(actor.id.clone()).await?; + self.db.save_actor(actor).await } #[tracing::instrument(level = "debug", name = "Remove Connection", skip(self))] @@ -69,7 +65,7 @@ impl ActorCache { self.db.remove_connection(actor.id.clone()).await } - #[tracing::instrument(level = "debug", name = "Fetch remote actor", skip_all, fields(id = id.to_string().as_str(), requests))] + #[tracing::instrument(level = "debug", name = "Fetch remote actor", skip_all, fields(id = id.to_string().as_str()))] pub(crate) async fn get_no_cache( &self, id: &IriString, diff --git a/src/data/node.rs b/src/data/node.rs index 815cc7c..4ba6eb5 100644 --- a/src/data/node.rs +++ b/src/data/node.rs @@ -36,11 +36,9 @@ impl NodeCache { #[tracing::instrument(level = "debug", name = "Get nodes", skip(self))] pub(crate) async fn nodes(&self) -> Result, Error> { - let infos = self.db.connected_info(); - let instances = self.db.connected_instance(); - let contacts = self.db.connected_contact(); - - let (infos, instances, contacts) = tokio::try_join!(infos, instances, contacts)?; + let infos = self.db.connected_info().await?; + let instances = self.db.connected_instance().await?; + let contacts = self.db.connected_contact().await?; let vec = self .db diff --git a/src/error.rs b/src/error.rs index f399ebe..98df5c9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -10,7 +10,7 @@ use std::{convert::Infallible, fmt::Debug, io}; use tracing_error::SpanTrace; pub(crate) struct Error { - context: SpanTrace, + context: String, kind: ErrorKind, } @@ -53,7 +53,7 @@ where { fn from(error: T) -> Self { Error { - context: SpanTrace::capture(), + context: SpanTrace::capture().to_string(), kind: error.into(), } } diff --git a/src/extractors.rs b/src/extractors.rs index 572da34..f56b9af 100644 --- a/src/extractors.rs +++ b/src/extractors.rs @@ -80,7 +80,7 @@ impl Admin { #[derive(Debug, thiserror::Error)] #[error("Failed authentication")] pub(crate) struct Error { - context: SpanTrace, + context: String, #[source] kind: ErrorKind, } @@ -88,49 +88,49 @@ pub(crate) struct Error { impl Error { fn invalid() -> Self { Error { - context: SpanTrace::capture(), + context: SpanTrace::capture().to_string(), kind: ErrorKind::Invalid, } } fn missing_config() -> Self { Error { - context: SpanTrace::capture(), + context: SpanTrace::capture().to_string(), kind: ErrorKind::MissingConfig, } } fn missing_db() -> Self { Error { - context: SpanTrace::capture(), + context: SpanTrace::capture().to_string(), kind: ErrorKind::MissingDb, } } fn bcrypt_verify(e: BcryptError) -> Self { Error { - context: SpanTrace::capture(), + context: SpanTrace::capture().to_string(), kind: ErrorKind::BCryptVerify(e), } } fn bcrypt_hash(e: BcryptError) -> Self { Error { - context: SpanTrace::capture(), + context: SpanTrace::capture().to_string(), kind: ErrorKind::BCryptHash(e), } } fn parse_header(e: ParseError) -> Self { Error { - context: SpanTrace::capture(), + context: SpanTrace::capture().to_string(), kind: ErrorKind::ParseHeader(e), } } fn canceled(_: BlockingError) -> Self { Error { - context: SpanTrace::capture(), + context: SpanTrace::capture().to_string(), kind: ErrorKind::Canceled, } } diff --git a/src/routes/inbox.rs b/src/routes/inbox.rs index 38d492a..b4db18d 100644 --- a/src/routes/inbox.rs +++ b/src/routes/inbox.rs @@ -36,10 +36,8 @@ pub(crate) async fn route( .await? .into_inner(); - let is_allowed = state.db.is_allowed(actor.id.clone()); - let is_connected = state.db.is_connected(actor.id.clone()); - - let (is_allowed, is_connected) = tokio::try_join!(is_allowed, is_connected)?; + let is_allowed = state.db.is_allowed(actor.id.clone()).await?; + let is_connected = state.db.is_connected(actor.id.clone()).await?; if !is_allowed { return Err(ErrorKind::NotAllowed(actor.id.to_string()).into()); @@ -54,7 +52,7 @@ pub(crate) async fn route( } else if config.validate_signatures() { if let Some((verified, _)) = verified { if actor.public_key_id.as_str() != verified.key_id() { - tracing::error!("Bad actor, more info: {:?}", input); + tracing::error!("Actor signed with wrong key"); return Err(ErrorKind::BadActor( actor.public_key_id.to_string(), verified.key_id().to_owned(), diff --git a/src/routes/nodeinfo.rs b/src/routes/nodeinfo.rs index 18b1c3a..a504496 100644 --- a/src/routes/nodeinfo.rs +++ b/src/routes/nodeinfo.rs @@ -24,18 +24,18 @@ struct Links { links: Vec, } -#[tracing::instrument(name = "NodeInfo")] +#[tracing::instrument(name = "NodeInfo", skip_all)] pub(crate) async fn route( config: web::Data, state: web::Data, ) -> web::Json { - let (inboxes, blocks) = tokio::join!(state.db.inboxes(), async { - if config.publish_blocks() { - Some(state.db.blocks().await.unwrap_or_default()) - } else { - None - } - }); + let inboxes = state.db.inboxes().await; + + let blocks = if config.publish_blocks() { + Some(state.db.blocks().await.unwrap_or_default()) + } else { + None + }; let peers = inboxes .unwrap_or_default() diff --git a/src/routes/statics.rs b/src/routes/statics.rs index 31e37c1..6c7788a 100644 --- a/src/routes/statics.rs +++ b/src/routes/statics.rs @@ -5,7 +5,7 @@ use actix_web::{ }; #[allow(clippy::async_yields_async)] -#[tracing::instrument(name = "Statistics")] +#[tracing::instrument(name = "Statics")] pub(crate) async fn route(filename: web::Path) -> HttpResponse { if let Some(data) = StaticFile::get(&filename.into_inner()) { HttpResponse::Ok()