From c914a4fd91f2e64284e4f3c2323c21c281b5b19b Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 11 Jun 2024 01:27:58 +0000 Subject: [PATCH] capture logs for resolve-true-destination admin cmd Signed-off-by: Jason Volk --- src/admin/debug/debug_commands.rs | 34 +++++++++++++++++++++++-------- src/service/sending/resolve.rs | 1 + 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/admin/debug/debug_commands.rs b/src/admin/debug/debug_commands.rs index 21a1751c..287c78dd 100644 --- a/src/admin/debug/debug_commands.rs +++ b/src/admin/debug/debug_commands.rs @@ -1,11 +1,15 @@ use std::{ collections::{BTreeMap, HashMap}, - sync::Arc, + sync::{Arc, Mutex}, time::Instant, }; -use api::client::validate_and_add_event_id; -use conduit::{utils::HtmlEscape, Error, Result}; +use conduit::{ + debug, info, log, + log::{capture, Capture}, + utils::HtmlEscape, + warn, Error, Result, +}; use ruma::{ api::{client::error::ErrorKind, federation::event::get_room_state}, events::room::message::RoomMessageEventContent, @@ -13,9 +17,10 @@ use ruma::{ }; use service::{rooms::event_handler::parse_incoming_pdu, sending::resolve::resolve_actual_dest, services, PduEvent}; use tokio::sync::RwLock; -use tracing::{debug, info, warn}; use tracing_subscriber::EnvFilter; +use crate::api::client::validate_and_add_event_id; + pub(crate) async fn get_auth_chain(_body: Vec<&str>, event_id: Box) -> Result { let event_id = Arc::::from(event_id); if let Some(event) = services().rooms.timeline.get_pdu_json(&event_id)? { @@ -635,11 +640,24 @@ pub(crate) async fn resolve_true_destination( )); } - let (actual_dest, hostname_uri) = resolve_actual_dest(&server_name, !no_cache).await?; + let filter: &capture::Filter = &|data| { + data.level() <= log::Level::DEBUG + && data.mod_name().starts_with("conduit") + && matches!(data.span_name(), "actual" | "well-known" | "srv") + }; - Ok(RoomMessageEventContent::text_plain(format!( - "Actual destination: {actual_dest} | Hostname URI: {hostname_uri}" - ))) + let state = &services().server.log.capture; + let logs = Arc::new(Mutex::new(String::new())); + let capture = Capture::new(state, Some(filter), capture::to_html(&logs)); + let (actual_dest, hostname_uri); + { + let _capture_scope = capture.start(); + (actual_dest, hostname_uri) = resolve_actual_dest(&server_name, !no_cache).await?; + }; + + let plain = format!("Actual destination: {actual_dest} | Hostname URI: {hostname_uri}"); + let html = format!("{}
{plain}", logs.lock().expect("locked")); + Ok(RoomMessageEventContent::text_html(plain, html)) } #[must_use] diff --git a/src/service/sending/resolve.rs b/src/service/sending/resolve.rs index 1d043d8b..23080bed 100644 --- a/src/service/sending/resolve.rs +++ b/src/service/sending/resolve.rs @@ -75,6 +75,7 @@ pub(crate) async fn get_actual_dest(server_name: &ServerName) -> Result /// Numbers in comments below refer to bullet points in linked section of /// specification +#[tracing::instrument(skip_all, name = "actual")] pub async fn resolve_actual_dest(dest: &ServerName, cache: bool) -> Result<(FedDest, String)> { trace!("Finding actual destination for {dest}"); let mut host = dest.as_str().to_owned();