Instrument with tracing

This commit is contained in:
Aode (lion)
2021-09-18 12:55:39 -05:00
parent ebba8e3f60
commit 43e5b6d873
34 changed files with 748 additions and 707 deletions
+13 -13
View File
@@ -1,7 +1,7 @@
use crate::{
apub::AcceptedActors,
db::{Actor, Db},
error::MyError,
error::{Error, ErrorKind},
requests::Requests,
};
use activitystreams::{prelude::*, url::Url};
@@ -30,7 +30,7 @@ impl<T> MaybeCached<T> {
}
}
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct ActorCache {
db: Db,
}
@@ -40,11 +40,12 @@ impl ActorCache {
ActorCache { db }
}
#[tracing::instrument(name = "Get Actor", skip(requests))]
pub(crate) async fn get(
&self,
id: &Url,
requests: &Requests,
) -> Result<MaybeCached<Actor>, MyError> {
) -> Result<MaybeCached<Actor>, Error> {
if let Some(actor) = self.db.actor(id.clone()).await? {
if actor.saved_at + REFETCH_DURATION > SystemTime::now() {
return Ok(MaybeCached::Cached(actor));
@@ -56,26 +57,25 @@ impl ActorCache {
.map(MaybeCached::Fetched)
}
pub(crate) async fn add_connection(&self, actor: Actor) -> Result<(), MyError> {
#[tracing::instrument(name = "Add Connection")]
pub(crate) async fn add_connection(&self, actor: Actor) -> Result<(), Error> {
self.db.add_connection(actor.id.clone()).await?;
self.db.save_actor(actor).await
}
pub(crate) async fn remove_connection(&self, actor: &Actor) -> Result<(), MyError> {
#[tracing::instrument(name = "Remove Connection")]
pub(crate) async fn remove_connection(&self, actor: &Actor) -> Result<(), Error> {
self.db.remove_connection(actor.id.clone()).await
}
pub(crate) async fn get_no_cache(
&self,
id: &Url,
requests: &Requests,
) -> Result<Actor, MyError> {
#[tracing::instrument(name = "Fetch remote actor", skip(requests))]
pub(crate) async fn get_no_cache(&self, id: &Url, requests: &Requests) -> Result<Actor, Error> {
let accepted_actor = requests.fetch::<AcceptedActors>(id.as_str()).await?;
let input_domain = id.domain().ok_or(MyError::MissingDomain)?;
let input_domain = id.domain().ok_or(ErrorKind::MissingDomain)?;
let accepted_actor_id = accepted_actor
.id(&input_domain)?
.ok_or(MyError::MissingId)?;
.ok_or(ErrorKind::MissingId)?;
let inbox = get_inbox(&accepted_actor)?.clone();
@@ -93,7 +93,7 @@ impl ActorCache {
}
}
fn get_inbox(actor: &AcceptedActors) -> Result<&Url, MyError> {
fn get_inbox(actor: &AcceptedActors) -> Result<&Url, Error> {
Ok(actor
.endpoints()?
.and_then(|e| e.shared_inbox)
+14 -8
View File
@@ -1,6 +1,6 @@
use crate::{
db::{Db, MediaMeta},
error::MyError,
error::Error,
};
use activitystreams::url::Url;
use actix_web::web::Bytes;
@@ -9,7 +9,7 @@ use uuid::Uuid;
static MEDIA_DURATION: Duration = Duration::from_secs(60 * 60 * 24 * 2);
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct MediaCache {
db: Db,
}
@@ -19,15 +19,18 @@ impl MediaCache {
MediaCache { db }
}
pub(crate) async fn get_uuid(&self, url: Url) -> Result<Option<Uuid>, MyError> {
#[tracing::instrument(name = "Get media uuid")]
pub(crate) async fn get_uuid(&self, url: Url) -> Result<Option<Uuid>, Error> {
self.db.media_id(url).await
}
pub(crate) async fn get_url(&self, uuid: Uuid) -> Result<Option<Url>, MyError> {
#[tracing::instrument(name = "Get media url")]
pub(crate) async fn get_url(&self, uuid: Uuid) -> Result<Option<Url>, Error> {
self.db.media_url(uuid).await
}
pub(crate) async fn is_outdated(&self, uuid: Uuid) -> Result<bool, MyError> {
#[tracing::instrument(name = "Is media outdated")]
pub(crate) async fn is_outdated(&self, uuid: Uuid) -> Result<bool, Error> {
if let Some(meta) = self.db.media_meta(uuid).await? {
if meta.saved_at + MEDIA_DURATION > SystemTime::now() {
return Ok(false);
@@ -37,7 +40,8 @@ impl MediaCache {
Ok(true)
}
pub(crate) async fn get_bytes(&self, uuid: Uuid) -> Result<Option<(String, Bytes)>, MyError> {
#[tracing::instrument(name = "Get media bytes")]
pub(crate) async fn get_bytes(&self, uuid: Uuid) -> Result<Option<(String, Bytes)>, Error> {
if let Some(meta) = self.db.media_meta(uuid).await? {
if meta.saved_at + MEDIA_DURATION > SystemTime::now() {
return self
@@ -51,7 +55,8 @@ impl MediaCache {
Ok(None)
}
pub(crate) async fn store_url(&self, url: Url) -> Result<Uuid, MyError> {
#[tracing::instrument(name = "Store media url")]
pub(crate) async fn store_url(&self, url: Url) -> Result<Uuid, Error> {
let uuid = Uuid::new_v4();
self.db.save_url(url, uuid).await?;
@@ -59,12 +64,13 @@ impl MediaCache {
Ok(uuid)
}
#[tracing::instrument(name = "store media bytes", skip(bytes))]
pub(crate) async fn store_bytes(
&self,
uuid: Uuid,
media_type: String,
bytes: Bytes,
) -> Result<(), MyError> {
) -> Result<(), Error> {
self.db
.save_bytes(
uuid,
+13 -6
View File
@@ -1,11 +1,11 @@
use crate::{
db::{Contact, Db, Info, Instance},
error::MyError,
error::Error,
};
use activitystreams::url::Url;
use std::time::{Duration, SystemTime};
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct NodeCache {
db: Db,
}
@@ -23,7 +23,8 @@ impl NodeCache {
NodeCache { db }
}
pub(crate) async fn nodes(&self) -> Result<Vec<Node>, MyError> {
#[tracing::instrument(name = "Get nodes")]
pub(crate) async fn nodes(&self) -> Result<Vec<Node>, Error> {
let infos = self.db.connected_info().await?;
let instances = self.db.connected_instance().await?;
let contacts = self.db.connected_contact().await?;
@@ -48,6 +49,7 @@ impl NodeCache {
Ok(vec)
}
#[tracing::instrument(name = "Is NodeInfo Outdated")]
pub(crate) async fn is_nodeinfo_outdated(&self, actor_id: Url) -> bool {
self.db
.info(actor_id)
@@ -56,6 +58,7 @@ impl NodeCache {
.unwrap_or(true)
}
#[tracing::instrument(name = "Is Contact Outdated")]
pub(crate) async fn is_contact_outdated(&self, actor_id: Url) -> bool {
self.db
.contact(actor_id)
@@ -64,6 +67,7 @@ impl NodeCache {
.unwrap_or(true)
}
#[tracing::instrument(name = "Is Instance Outdated")]
pub(crate) async fn is_instance_outdated(&self, actor_id: Url) -> bool {
self.db
.instance(actor_id)
@@ -72,13 +76,14 @@ impl NodeCache {
.unwrap_or(true)
}
#[tracing::instrument(name = "Save node info")]
pub(crate) async fn set_info(
&self,
actor_id: Url,
software: String,
version: String,
reg: bool,
) -> Result<(), MyError> {
) -> Result<(), Error> {
self.db
.save_info(
actor_id,
@@ -92,6 +97,7 @@ impl NodeCache {
.await
}
#[tracing::instrument(name = "Save instance info")]
pub(crate) async fn set_instance(
&self,
actor_id: Url,
@@ -100,7 +106,7 @@ impl NodeCache {
version: String,
reg: bool,
requires_approval: bool,
) -> Result<(), MyError> {
) -> Result<(), Error> {
self.db
.save_instance(
actor_id,
@@ -116,6 +122,7 @@ impl NodeCache {
.await
}
#[tracing::instrument(name = "Save contact info")]
pub(crate) async fn set_contact(
&self,
actor_id: Url,
@@ -123,7 +130,7 @@ impl NodeCache {
display_name: String,
url: Url,
avatar: Url,
) -> Result<(), MyError> {
) -> Result<(), Error> {
self.db
.save_contact(
actor_id,
+21 -4
View File
@@ -2,17 +2,17 @@ use crate::{
config::{Config, UrlKind},
data::NodeCache,
db::Db,
error::MyError,
error::Error,
requests::{Breakers, Requests},
};
use activitystreams::url::Url;
use actix_web::web;
use async_rwlock::RwLock;
use log::info;
use lru::LruCache;
use rand::thread_rng;
use rsa::{RsaPrivateKey, RsaPublicKey};
use std::sync::Arc;
use tracing::info;
#[derive(Clone)]
pub struct State {
@@ -25,6 +25,20 @@ pub struct State {
pub(crate) db: Db,
}
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("config", &self.config)
.field("object_cache", &"Object Cache")
.field("node_cache", &self.node_cache)
.field("breakers", &self.breakers)
.field("db", &self.db)
.finish()
}
}
impl State {
pub(crate) fn node_cache(&self) -> NodeCache {
self.node_cache.clone()
@@ -44,11 +58,12 @@ impl State {
)
}
#[tracing::instrument(name = "Get inboxes for other domains")]
pub(crate) async fn inboxes_without(
&self,
existing_inbox: &Url,
domain: &str,
) -> Result<Vec<Url>, MyError> {
) -> Result<Vec<Url>, Error> {
Ok(self
.db
.inboxes()
@@ -74,8 +89,10 @@ impl State {
self.object_cache.write().await.put(object_id, actor_id);
}
pub(crate) async fn build(config: Config, db: Db) -> Result<Self, MyError> {
#[tracing::instrument(name = "Building state")]
pub(crate) async fn build(config: Config, db: Db) -> Result<Self, Error> {
let private_key = if let Ok(Some(key)) = db.private_key().await {
info!("Using existing key");
key
} else {
info!("Generating new keys");