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
+5 -4
View File
@@ -1,7 +1,7 @@
use crate::{
config::{Config, UrlKind},
db::Actor,
error::MyError,
error::Error,
jobs::{
apub::{get_inboxes, prepare_activity},
DeliverMany, JobState,
@@ -22,7 +22,8 @@ impl Announce {
Announce { object_id, actor }
}
async fn perform(self, state: JobState) -> Result<(), anyhow::Error> {
#[tracing::instrument(name = "Announce")]
async fn perform(self, state: JobState) -> Result<(), Error> {
let activity_id = state.config.generate_url(UrlKind::Activity);
let announce = generate_announce(&state.config, &activity_id, &self.object_id)?;
@@ -41,7 +42,7 @@ fn generate_announce(
config: &Config,
activity_id: &Url,
object_id: &Url,
) -> Result<AsAnnounce, MyError> {
) -> Result<AsAnnounce, Error> {
let announce = AsAnnounce::new(config.generate_url(UrlKind::Actor), object_id.clone());
prepare_activity(
@@ -58,6 +59,6 @@ impl ActixJob for Announce {
const NAME: &'static str = "relay::jobs::apub::Announce";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+7 -6
View File
@@ -2,7 +2,7 @@ use crate::{
apub::AcceptedActivities,
config::{Config, UrlKind},
db::Actor,
error::MyError,
error::{Error, ErrorKind},
jobs::{apub::prepare_activity, Deliver, JobState, QueryInstance, QueryNodeinfo},
};
use activitystreams::{
@@ -24,7 +24,8 @@ impl Follow {
Follow { input, actor }
}
async fn perform(self, state: JobState) -> Result<(), anyhow::Error> {
#[tracing::instrument(name = "Follow")]
async fn perform(self, state: JobState) -> Result<(), Error> {
let my_id = state.config.generate_url(UrlKind::Actor);
// if following relay directly, not just following 'public', followback
@@ -42,7 +43,7 @@ impl Follow {
let accept = generate_accept_follow(
&state.config,
&self.actor.id,
self.input.id_unchecked().ok_or(MyError::MissingId)?,
self.input.id_unchecked().ok_or(ErrorKind::MissingId)?,
&my_id,
)?;
@@ -61,7 +62,7 @@ impl Follow {
}
// Generate a type that says "I want to follow you"
fn generate_follow(config: &Config, actor_id: &Url, my_id: &Url) -> Result<AsFollow, MyError> {
fn generate_follow(config: &Config, actor_id: &Url, my_id: &Url) -> Result<AsFollow, Error> {
let follow = AsFollow::new(my_id.clone(), actor_id.clone());
prepare_activity(
@@ -77,7 +78,7 @@ fn generate_accept_follow(
actor_id: &Url,
input_id: &Url,
my_id: &Url,
) -> Result<AsAccept, MyError> {
) -> Result<AsAccept, Error> {
let mut follow = AsFollow::new(actor_id.clone(), my_id.clone());
follow.set_id(input_id.clone());
@@ -98,6 +99,6 @@ impl ActixJob for Follow {
const NAME: &'static str = "relay::jobs::apub::Follow";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+5 -4
View File
@@ -1,7 +1,7 @@
use crate::{
apub::AcceptedActivities,
db::Actor,
error::MyError,
error::{Error, ErrorKind},
jobs::{apub::get_inboxes, DeliverMany, JobState},
};
use activitystreams::prelude::*;
@@ -19,12 +19,13 @@ impl Forward {
Forward { input, actor }
}
async fn perform(self, state: JobState) -> Result<(), anyhow::Error> {
#[tracing::instrument(name = "Forward")]
async fn perform(self, state: JobState) -> Result<(), Error> {
let object_id = self
.input
.object()
.as_single_id()
.ok_or(MyError::MissingId)?;
.ok_or(ErrorKind::MissingId)?;
let inboxes = get_inboxes(&state.state, &self.actor, object_id).await?;
@@ -43,6 +44,6 @@ impl ActixJob for Forward {
const NAME: &'static str = "relay::jobs::apub::Forward";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+6 -6
View File
@@ -2,7 +2,7 @@ use crate::{
config::{Config, UrlKind},
data::State,
db::Actor,
error::MyError,
error::{Error, ErrorKind},
};
use activitystreams::{
activity::{Follow as AsFollow, Undo as AsUndo},
@@ -23,8 +23,8 @@ pub(crate) use self::{
announce::Announce, follow::Follow, forward::Forward, reject::Reject, undo::Undo,
};
async fn get_inboxes(state: &State, actor: &Actor, object_id: &Url) -> Result<Vec<Url>, MyError> {
let domain = object_id.host().ok_or(MyError::Domain)?.to_string();
async fn get_inboxes(state: &State, actor: &Actor, object_id: &Url) -> Result<Vec<Url>, Error> {
let domain = object_id.host().ok_or(ErrorKind::Domain)?.to_string();
state.inboxes_without(&actor.inbox, &domain).await
}
@@ -33,10 +33,10 @@ fn prepare_activity<T, U, V, Kind>(
mut t: T,
id: impl TryInto<Url, Error = U>,
to: impl TryInto<Url, Error = V>,
) -> Result<T, MyError>
) -> Result<T, Error>
where
T: ObjectExt<Kind> + BaseExt<Kind>,
MyError: From<U> + From<V>,
Error: From<U> + From<V>,
{
t.set_id(id.try_into()?)
.set_many_tos(vec![to.try_into()?])
@@ -45,7 +45,7 @@ where
}
// Generate a type that says "I want to stop following you"
fn generate_undo_follow(config: &Config, actor_id: &Url, my_id: &Url) -> Result<AsUndo, MyError> {
fn generate_undo_follow(config: &Config, actor_id: &Url, my_id: &Url) -> Result<AsUndo, Error> {
let mut follow = AsFollow::new(my_id.clone(), actor_id.clone());
follow.set_id(config.generate_url(UrlKind::Activity));
+4 -2
View File
@@ -1,6 +1,7 @@
use crate::{
config::UrlKind,
db::Actor,
error::Error,
jobs::{apub::generate_undo_follow, Deliver, JobState},
};
use background_jobs::ActixJob;
@@ -10,7 +11,8 @@ use std::{future::Future, pin::Pin};
pub(crate) struct Reject(pub(crate) Actor);
impl Reject {
async fn perform(self, state: JobState) -> Result<(), anyhow::Error> {
#[tracing::instrument(name = "Reject")]
async fn perform(self, state: JobState) -> Result<(), Error> {
state.actors.remove_connection(&self.0).await?;
let my_id = state.config.generate_url(UrlKind::Actor);
@@ -29,6 +31,6 @@ impl ActixJob for Reject {
const NAME: &'static str = "relay::jobs::apub::Reject";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+4 -2
View File
@@ -2,6 +2,7 @@ use crate::{
apub::AcceptedActivities,
config::UrlKind,
db::Actor,
error::Error,
jobs::{apub::generate_undo_follow, Deliver, JobState},
};
use background_jobs::ActixJob;
@@ -18,7 +19,8 @@ impl Undo {
Undo { input, actor }
}
async fn perform(self, state: JobState) -> Result<(), anyhow::Error> {
#[tracing::instrument(name = "Undo")]
async fn perform(self, state: JobState) -> Result<(), Error> {
let was_following = state.state.db.is_connected(self.actor.id.clone()).await?;
state.actors.remove_connection(&self.actor).await?;
@@ -42,6 +44,6 @@ impl ActixJob for Undo {
const NAME: &'static str = "relay::jobs::apub::Undo";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+4 -4
View File
@@ -1,5 +1,4 @@
use crate::jobs::JobState;
use anyhow::Error;
use crate::{error::Error, jobs::JobState};
use background_jobs::ActixJob;
use std::{future::Future, pin::Pin};
use uuid::Uuid;
@@ -14,6 +13,7 @@ impl CacheMedia {
CacheMedia { uuid }
}
#[tracing::instrument(name = "Cache media")]
async fn perform(self, state: JobState) -> Result<(), Error> {
if !state.media.is_outdated(self.uuid).await? {
return Ok(());
@@ -34,11 +34,11 @@ impl CacheMedia {
impl ActixJob for CacheMedia {
type State = JobState;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
const NAME: &'static str = "relay::jobs::CacheMedia";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+9 -6
View File
@@ -1,6 +1,9 @@
use crate::{apub::AcceptedActors, jobs::JobState};
use crate::{
apub::AcceptedActors,
error::{Error, ErrorKind},
jobs::JobState,
};
use activitystreams::{object::Image, prelude::*, url::Url};
use anyhow::Error;
use background_jobs::ActixJob;
use std::{future::Future, pin::Pin};
@@ -33,8 +36,8 @@ impl QueryContact {
.fetch::<AcceptedActors>(self.contact_id.as_str())
.await?;
let (username, display_name, url, avatar) = to_contact(contact)
.ok_or_else(|| anyhow::anyhow!("Failed to extract fields from contact"))?;
let (username, display_name, url, avatar) =
to_contact(contact).ok_or_else(|| ErrorKind::Extract("contact"))?;
state
.node_cache
@@ -63,12 +66,12 @@ fn to_contact(contact: AcceptedActors) -> Option<(String, String, Url, Url)> {
impl ActixJob for QueryContact {
type State = JobState;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
const NAME: &'static str = "relay::jobs::QueryContact";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+10 -9
View File
@@ -1,6 +1,5 @@
use crate::{error::MyError, jobs::JobState};
use crate::{error::Error, jobs::JobState};
use activitystreams::url::Url;
use anyhow::Error;
use background_jobs::{ActixJob, Backoff};
use std::{future::Future, pin::Pin};
@@ -11,7 +10,7 @@ pub(crate) struct Deliver {
}
impl Deliver {
pub(crate) fn new<T>(to: Url, data: T) -> Result<Self, MyError>
pub(crate) fn new<T>(to: Url, data: T) -> Result<Self, Error>
where
T: serde::ser::Serialize,
{
@@ -20,20 +19,22 @@ impl Deliver {
data: serde_json::to_value(data)?,
})
}
#[tracing::instrument(name = "Deliver")]
async fn permform(self, state: JobState) -> Result<(), Error> {
state.requests.deliver(self.to, &self.data).await?;
Ok(())
}
}
impl ActixJob for Deliver {
type State = JobState;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
const NAME: &'static str = "relay::jobs::Deliver";
const BACKOFF: Backoff = Backoff::Exponential(8);
fn run(self, state: Self::State) -> Self::Future {
Box::pin(async move {
state.requests.deliver(self.to, &self.data).await?;
Ok(())
})
Box::pin(async move { self.permform(state).await.map_err(Into::into) })
}
}
+6 -6
View File
@@ -1,11 +1,10 @@
use crate::{
error::MyError,
error::Error,
jobs::{Deliver, JobState},
};
use activitystreams::url::Url;
use anyhow::Error;
use background_jobs::ActixJob;
use futures::future::{ready, Ready};
use std::future::{ready, Ready};
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub(crate) struct DeliverMany {
@@ -14,7 +13,7 @@ pub(crate) struct DeliverMany {
}
impl DeliverMany {
pub(crate) fn new<T>(to: Vec<Url>, data: T) -> Result<Self, MyError>
pub(crate) fn new<T>(to: Vec<Url>, data: T) -> Result<Self, Error>
where
T: serde::ser::Serialize,
{
@@ -24,6 +23,7 @@ impl DeliverMany {
})
}
#[tracing::instrument(name = "Deliver many")]
fn perform(self, state: JobState) -> Result<(), Error> {
for inbox in self.to {
state
@@ -37,11 +37,11 @@ impl DeliverMany {
impl ActixJob for DeliverMany {
type State = JobState;
type Future = Ready<Result<(), Error>>;
type Future = Ready<Result<(), anyhow::Error>>;
const NAME: &'static str = "relay::jobs::DeliverMany";
fn run(self, state: Self::State) -> Self::Future {
ready(self.perform(state))
ready(self.perform(state).map_err(Into::into))
}
}
+4 -3
View File
@@ -1,9 +1,9 @@
use crate::{
config::UrlKind,
error::Error,
jobs::{cache_media::CacheMedia, JobState},
};
use activitystreams::url::Url;
use anyhow::Error;
use background_jobs::ActixJob;
use std::{future::Future, pin::Pin};
@@ -17,6 +17,7 @@ impl QueryInstance {
QueryInstance { actor_id }
}
#[tracing::instrument(name = "Query instance")]
async fn perform(self, state: JobState) -> Result<(), Error> {
let contact_outdated = state
.node_cache
@@ -91,12 +92,12 @@ impl QueryInstance {
impl ActixJob for QueryInstance {
type State = JobState;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
const NAME: &'static str = "relay::jobs::QueryInstance";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+15 -4
View File
@@ -16,7 +16,7 @@ use crate::{
config::Config,
data::{ActorCache, MediaCache, NodeCache, State},
db::Db,
error::MyError,
error::{Error, ErrorKind},
jobs::process_listeners::Listeners,
requests::Requests,
};
@@ -67,7 +67,7 @@ pub(crate) fn create_workers(
.start(remote_handle);
}
#[derive(Clone)]
#[derive(Clone, Debug)]
pub(crate) struct JobState {
db: Db,
requests: Requests,
@@ -84,6 +84,14 @@ pub(crate) struct JobServer {
remote: QueueHandle,
}
impl std::fmt::Debug for JobServer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JobServer")
.field("queue_handle", &"QueueHandle")
.finish()
}
}
impl JobState {
fn new(
db: Db,
@@ -113,10 +121,13 @@ impl JobServer {
}
}
pub(crate) fn queue<J>(&self, job: J) -> Result<(), MyError>
pub(crate) fn queue<J>(&self, job: J) -> Result<(), Error>
where
J: Job,
{
self.remote.queue(job).map_err(MyError::Queue)
self.remote
.queue(job)
.map_err(ErrorKind::Queue)
.map_err(Into::into)
}
}
+7 -4
View File
@@ -1,6 +1,8 @@
use crate::jobs::{JobState, QueryContact};
use crate::{
error::Error,
jobs::{JobState, QueryContact},
};
use activitystreams::url::Url;
use anyhow::Error;
use background_jobs::ActixJob;
use std::{future::Future, pin::Pin};
@@ -14,6 +16,7 @@ impl QueryNodeinfo {
QueryNodeinfo { actor_id }
}
#[tracing::instrument(name = "Query node info")]
async fn perform(self, state: JobState) -> Result<(), Error> {
if !state
.node_cache
@@ -65,12 +68,12 @@ impl QueryNodeinfo {
impl ActixJob for QueryNodeinfo {
type State = JobState;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
const NAME: &'static str = "relay::jobs::QueryNodeinfo";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}
+7 -4
View File
@@ -1,5 +1,7 @@
use crate::jobs::{instance::QueryInstance, nodeinfo::QueryNodeinfo, JobState};
use anyhow::Error;
use crate::{
error::Error,
jobs::{instance::QueryInstance, nodeinfo::QueryNodeinfo, JobState},
};
use background_jobs::ActixJob;
use std::{future::Future, pin::Pin};
@@ -7,6 +9,7 @@ use std::{future::Future, pin::Pin};
pub(crate) struct Listeners;
impl Listeners {
#[tracing::instrument(name = "Spawn query instances")]
async fn perform(self, state: JobState) -> Result<(), Error> {
for actor_id in state.state.db.connected_ids().await? {
state
@@ -21,11 +24,11 @@ impl Listeners {
impl ActixJob for Listeners {
type State = JobState;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>>>>;
type Future = Pin<Box<dyn Future<Output = Result<(), anyhow::Error>>>>;
const NAME: &'static str = "relay::jobs::Listeners";
fn run(self, state: Self::State) -> Self::Future {
Box::pin(self.perform(state))
Box::pin(async move { self.perform(state).await.map_err(Into::into) })
}
}