From 3384ca9064381ddeb4a3864c6b77790d38e3f738 Mon Sep 17 00:00:00 2001 From: "Aode (Lion)" Date: Tue, 21 Sep 2021 14:32:25 -0500 Subject: [PATCH] Clean up debug impls --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/apub.rs | 14 +++++++++++-- src/config.rs | 23 +++++++++++++++++++- src/data/actor.rs | 4 ++-- src/data/media.rs | 4 ++-- src/data/node.rs | 44 ++++++++++++++++++++++++++++++++------- src/data/state.rs | 11 ++++++---- src/db.rs | 28 +++++++++++++++++++++++-- src/error.rs | 2 +- src/jobs/apub/announce.rs | 11 +++++++++- src/jobs/contact.rs | 11 +++++++++- src/jobs/deliver.rs | 11 +++++++++- src/jobs/deliver_many.rs | 19 ++++++++++++++++- src/jobs/instance.rs | 10 ++++++++- src/jobs/nodeinfo.rs | 12 +++++++++-- src/requests.rs | 5 ++++- 17 files changed, 182 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4babf37..027d09d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1941,7 +1941,7 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" [[package]] name = "relay" -version = "0.3.3" +version = "0.3.4" dependencies = [ "activitystreams", "activitystreams-ext", diff --git a/Cargo.toml b/Cargo.toml index 95904e6..220532a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "relay" description = "A simple activitypub relay" -version = "0.3.3" +version = "0.3.4" authors = ["asonix "] license-file = "LICENSE" readme = "README.md" diff --git a/src/apub.rs b/src/apub.rs index 644527c..11eff3e 100644 --- a/src/apub.rs +++ b/src/apub.rs @@ -1,12 +1,12 @@ -use activitystreams_ext::{Ext1, UnparsedExtension}; use activitystreams::{ activity::ActorAndObject, actor::{Actor, ApActor}, unparsed::UnparsedMutExt, url::Url, }; +use activitystreams_ext::{Ext1, UnparsedExtension}; -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct PublicKeyInner { pub id: Url, @@ -14,6 +14,16 @@ pub struct PublicKeyInner { pub public_key_pem: String, } +impl std::fmt::Debug for PublicKeyInner { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PublicKeyInner") + .field("id", &self.id.to_string()) + .field("owner", &self.owner.to_string()) + .field("public_key_pem", &self.public_key_pem) + .finish() + } +} + #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] pub struct PublicKey { diff --git a/src/config.rs b/src/config.rs index cf512e8..43088be 100644 --- a/src/config.rs +++ b/src/config.rs @@ -26,7 +26,7 @@ pub(crate) struct ParsedConfig { opentelemetry_url: Option, } -#[derive(Clone, Debug)] +#[derive(Clone)] pub struct Config { hostname: String, addr: IpAddr, @@ -54,6 +54,27 @@ pub enum UrlKind { Outbox, } +impl std::fmt::Debug for Config { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Config") + .field("hostname", &self.hostname) + .field("addr", &self.addr) + .field("port", &self.port) + .field("debug", &self.debug) + .field("restricted_mode", &self.restricted_mode) + .field("validate_signatures", &self.validate_signatures) + .field("publish_blocks", &self.publish_blocks) + .field("base_uri", &self.base_uri.to_string()) + .field("sled_path", &self.sled_path) + .field("source_repo", &self.source_repo.to_string()) + .field( + "opentelemetry_url", + &self.opentelemetry_url.as_ref().map(|url| url.to_string()), + ) + .finish() + } +} + impl Config { pub(crate) fn build() -> Result { let mut config = config::Config::new(); diff --git a/src/data/actor.rs b/src/data/actor.rs index 783d4de..a1b31f0 100644 --- a/src/data/actor.rs +++ b/src/data/actor.rs @@ -40,7 +40,7 @@ impl ActorCache { ActorCache { db } } - #[tracing::instrument(name = "Get Actor")] + #[tracing::instrument(name = "Get Actor", fields(id = id.to_string().as_str(), requests))] pub(crate) async fn get( &self, id: &Url, @@ -68,7 +68,7 @@ impl ActorCache { self.db.remove_connection(actor.id.clone()).await } - #[tracing::instrument(name = "Fetch remote actor")] + #[tracing::instrument(name = "Fetch remote actor", fields(id = id.to_string().as_str(), requests))] pub(crate) async fn get_no_cache(&self, id: &Url, requests: &Requests) -> Result { let accepted_actor = requests.fetch::(id.as_str()).await?; diff --git a/src/data/media.rs b/src/data/media.rs index aa15c19..5ca71d1 100644 --- a/src/data/media.rs +++ b/src/data/media.rs @@ -19,7 +19,7 @@ impl MediaCache { MediaCache { db } } - #[tracing::instrument(name = "Get media uuid")] + #[tracing::instrument(name = "Get media uuid", fields(url = url.to_string().as_str()))] pub(crate) async fn get_uuid(&self, url: Url) -> Result, Error> { self.db.media_id(url).await } @@ -55,7 +55,7 @@ impl MediaCache { Ok(None) } - #[tracing::instrument(name = "Store media url")] + #[tracing::instrument(name = "Store media url", fields(url = url.to_string().as_str()))] pub(crate) async fn store_url(&self, url: Url) -> Result { let uuid = Uuid::new_v4(); diff --git a/src/data/node.rs b/src/data/node.rs index f32434b..fb3fed8 100644 --- a/src/data/node.rs +++ b/src/data/node.rs @@ -10,7 +10,7 @@ pub struct NodeCache { db: Db, } -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub struct Node { pub(crate) base: Url, pub(crate) info: Option, @@ -18,6 +18,17 @@ pub struct Node { pub(crate) contact: Option, } +impl std::fmt::Debug for Node { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Node") + .field("base", &self.base.to_string()) + .field("info", &self.info) + .field("instance", &self.instance) + .field("contact", &self.contact) + .finish() + } +} + impl NodeCache { pub(crate) fn new(db: Db) -> Self { NodeCache { db } @@ -49,7 +60,7 @@ impl NodeCache { Ok(vec) } - #[tracing::instrument(name = "Is NodeInfo Outdated")] + #[tracing::instrument(name = "Is NodeInfo Outdated", fields(actor_id = actor_id.to_string().as_str()))] pub(crate) async fn is_nodeinfo_outdated(&self, actor_id: Url) -> bool { self.db .info(actor_id) @@ -58,7 +69,7 @@ impl NodeCache { .unwrap_or(true) } - #[tracing::instrument(name = "Is Contact Outdated")] + #[tracing::instrument(name = "Is Contact Outdated", fields(actor_id = actor_id.to_string().as_str()))] pub(crate) async fn is_contact_outdated(&self, actor_id: Url) -> bool { self.db .contact(actor_id) @@ -67,7 +78,7 @@ impl NodeCache { .unwrap_or(true) } - #[tracing::instrument(name = "Is Instance Outdated")] + #[tracing::instrument(name = "Is Instance Outdated", fields(actor_id = actor_id.to_string().as_str()))] pub(crate) async fn is_instance_outdated(&self, actor_id: Url) -> bool { self.db .instance(actor_id) @@ -76,7 +87,7 @@ impl NodeCache { .unwrap_or(true) } - #[tracing::instrument(name = "Save node info")] + #[tracing::instrument(name = "Save node info", fields(actor_id = actor_id.to_string().as_str(), software, version, reg))] pub(crate) async fn set_info( &self, actor_id: Url, @@ -97,7 +108,17 @@ impl NodeCache { .await } - #[tracing::instrument(name = "Save instance info")] + #[tracing::instrument( + name = "Save instance info", + fields( + actor_id = actor_id.to_string().as_str(), + title, + description, + version, + reg, + requires_approval + ) + )] pub(crate) async fn set_instance( &self, actor_id: Url, @@ -122,7 +143,16 @@ impl NodeCache { .await } - #[tracing::instrument(name = "Save contact info")] + #[tracing::instrument( + name = "Save contact info", + fields( + actor_id = actor_id.to_string().as_str(), + username, + display_name, + url = url.to_string().as_str(), + avatar = avatar.to_string().as_str() + ) + )] pub(crate) async fn set_contact( &self, actor_id: Url, diff --git a/src/data/state.rs b/src/data/state.rs index 36d4007..2f3df71 100644 --- a/src/data/state.rs +++ b/src/data/state.rs @@ -27,9 +27,6 @@ pub struct State { impl std::fmt::Debug for State { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("State") - .field("public_key", &"PublicKey") - .field("private_key", &"[redacted]") - .field("object_cache", &"Object Cache") .field("node_cache", &self.node_cache) .field("breakers", &self.breakers) .field("db", &self.db) @@ -51,7 +48,13 @@ impl State { ) } - #[tracing::instrument(name = "Get inboxes for other domains")] + #[tracing::instrument( + name = "Get inboxes for other domains", + fields( + existing_inbox = existing_inbox.to_string().as_str(), + domain + ) + )] pub(crate) async fn inboxes_without( &self, existing_inbox: &Url, diff --git a/src/db.rs b/src/db.rs index 8836926..a625e93 100644 --- a/src/db.rs +++ b/src/db.rs @@ -39,7 +39,7 @@ impl std::fmt::Debug for Inner { } } -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub struct Actor { pub(crate) id: Url, pub(crate) public_key: String, @@ -48,6 +48,18 @@ pub struct Actor { pub(crate) saved_at: SystemTime, } +impl std::fmt::Debug for Actor { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Actor") + .field("id", &self.id.to_string()) + .field("public_key", &self.public_key) + .field("public_key_id", &self.public_key_id.to_string()) + .field("inbox", &self.inbox.to_string()) + .field("saved_at", &self.saved_at) + .finish() + } +} + #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] pub(crate) struct MediaMeta { pub(crate) media_type: String, @@ -72,7 +84,7 @@ pub struct Instance { pub(crate) updated: SystemTime, } -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub struct Contact { pub(crate) username: String, pub(crate) display_name: String, @@ -81,6 +93,18 @@ pub struct Contact { pub(crate) updated: SystemTime, } +impl std::fmt::Debug for Contact { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Info") + .field("username", &self.username) + .field("display_name", &self.display_name) + .field("url", &self.url.to_string()) + .field("avatar", &self.avatar.to_string()) + .field("updated", &self.updated) + .finish() + } +} + impl Inner { fn connected_by_domain(&self, domains: &[String]) -> impl DoubleEndedIterator { let reversed: Vec<_> = domains diff --git a/src/error.rs b/src/error.rs index 59baae5..0d8c98a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -145,7 +145,7 @@ pub(crate) enum ErrorKind { Breaker, #[error("Failed to extract fields from {0}")] - Extract(&'static str) + Extract(&'static str), } impl ResponseError for Error { diff --git a/src/jobs/apub/announce.rs b/src/jobs/apub/announce.rs index f1cafd8..ce23d10 100644 --- a/src/jobs/apub/announce.rs +++ b/src/jobs/apub/announce.rs @@ -11,12 +11,21 @@ use activitystreams::{activity::Announce as AsAnnounce, url::Url}; use background_jobs::ActixJob; use std::{future::Future, pin::Pin}; -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub(crate) struct Announce { object_id: Url, actor: Actor, } +impl std::fmt::Debug for Announce { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Announce") + .field("object_id", &self.object_id.to_string()) + .field("actor", &self.actor) + .finish() + } +} + impl Announce { pub fn new(object_id: Url, actor: Actor) -> Self { Announce { object_id, actor } diff --git a/src/jobs/contact.rs b/src/jobs/contact.rs index 49afcab..edb89f7 100644 --- a/src/jobs/contact.rs +++ b/src/jobs/contact.rs @@ -7,12 +7,21 @@ use activitystreams::{object::Image, prelude::*, url::Url}; use background_jobs::ActixJob; use std::{future::Future, pin::Pin}; -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub(crate) struct QueryContact { actor_id: Url, contact_id: Url, } +impl std::fmt::Debug for QueryContact { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("QueryContact") + .field("actor_id", &self.actor_id.to_string()) + .field("contact_id", &self.contact_id.to_string()) + .finish() + } +} + impl QueryContact { pub(crate) fn new(actor_id: Url, contact_id: Url) -> Self { QueryContact { diff --git a/src/jobs/deliver.rs b/src/jobs/deliver.rs index 0789981..4f0eff5 100644 --- a/src/jobs/deliver.rs +++ b/src/jobs/deliver.rs @@ -3,12 +3,21 @@ use activitystreams::url::Url; use background_jobs::{ActixJob, Backoff}; use std::{future::Future, pin::Pin}; -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub(crate) struct Deliver { to: Url, data: serde_json::Value, } +impl std::fmt::Debug for Deliver { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Deliver") + .field("to", &self.to.to_string()) + .field("data", &self.data) + .finish() + } +} + impl Deliver { pub(crate) fn new(to: Url, data: T) -> Result where diff --git a/src/jobs/deliver_many.rs b/src/jobs/deliver_many.rs index 2ff8c83..a68d244 100644 --- a/src/jobs/deliver_many.rs +++ b/src/jobs/deliver_many.rs @@ -6,12 +6,29 @@ use activitystreams::url::Url; use background_jobs::ActixJob; use std::future::{ready, Ready}; -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub(crate) struct DeliverMany { to: Vec, data: serde_json::Value, } +impl std::fmt::Debug for DeliverMany { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let to = format!( + "[{}]", + self.to + .iter() + .map(|u| u.to_string()) + .collect::>() + .join(", ") + ); + f.debug_struct("DeliverMany") + .field("to", &to) + .field("data", &self.data) + .finish() + } +} + impl DeliverMany { pub(crate) fn new(to: Vec, data: T) -> Result where diff --git a/src/jobs/instance.rs b/src/jobs/instance.rs index 6cc8984..72545a0 100644 --- a/src/jobs/instance.rs +++ b/src/jobs/instance.rs @@ -7,11 +7,19 @@ use activitystreams::url::Url; use background_jobs::ActixJob; use std::{future::Future, pin::Pin}; -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub(crate) struct QueryInstance { actor_id: Url, } +impl std::fmt::Debug for QueryInstance { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("QueryInstance") + .field("actor_id", &self.actor_id.to_string()) + .finish() + } +} + impl QueryInstance { pub(crate) fn new(actor_id: Url) -> Self { QueryInstance { actor_id } diff --git a/src/jobs/nodeinfo.rs b/src/jobs/nodeinfo.rs index c4eed56..9797dd6 100644 --- a/src/jobs/nodeinfo.rs +++ b/src/jobs/nodeinfo.rs @@ -4,13 +4,21 @@ use crate::{ }; use activitystreams::url::Url; use background_jobs::ActixJob; -use std::{future::Future, pin::Pin}; +use std::{fmt::Debug, future::Future, pin::Pin}; -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] +#[derive(Clone, serde::Deserialize, serde::Serialize)] pub(crate) struct QueryNodeinfo { actor_id: Url, } +impl Debug for QueryNodeinfo { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("QueryNodeinfo") + .field("actor_id", &self.actor_id.to_string()) + .finish() + } +} + impl QueryNodeinfo { pub(crate) fn new(actor_id: Url) -> Self { QueryNodeinfo { actor_id } diff --git a/src/requests.rs b/src/requests.rs index 191aad7..de7423c 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -361,7 +361,10 @@ impl Requests { Ok((content_type, bytes)) } - #[tracing::instrument("Deliver to Inbox")] + #[tracing::instrument( + "Deliver to Inbox", + fields(self, inbox = inbox.to_string().as_str(), item) + )] pub(crate) async fn deliver(&self, inbox: Url, item: &T) -> Result<(), Error> where T: serde::ser::Serialize + std::fmt::Debug,