various logging improvements.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-04-21 22:41:47 -07:00 committed by June
parent 423fc6dad0
commit 22bebb9b74
5 changed files with 40 additions and 37 deletions

View file

@ -10,7 +10,7 @@ use ruma::{
}, },
OwnedRoomAliasId, OwnedServerName, OwnedRoomAliasId, OwnedServerName,
}; };
use tracing::{debug, info, warn}; use tracing::debug;
use crate::{debug_info, debug_warn, services, Error, Result, Ruma}; use crate::{debug_info, debug_warn, services, Error, Result, Ruma};

View file

@ -30,7 +30,7 @@ use ruma::{
OwnedRoomId, OwnedServerName, RoomId, ServerName, UInt, UserId, OwnedRoomId, OwnedServerName, RoomId, ServerName, UInt, UserId,
}; };
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tracing::{debug, error, info, warn}; use tracing::{debug, error, warn};
use crate::{debug_info, services, Error, Result}; use crate::{debug_info, services, Error, Result};

View file

@ -272,7 +272,7 @@ impl Service {
}); });
} }
#[tracing::instrument(skip(self), name = "sender")] #[tracing::instrument(skip_all, name = "sender")]
async fn handler(&self) -> Result<()> { async fn handler(&self) -> Result<()> {
let receiver = self.receiver.lock().await; let receiver = self.receiver.lock().await;

View file

@ -14,9 +14,9 @@ use ruma::{
}, },
OwnedServerName, ServerName, OwnedServerName, ServerName,
}; };
use tracing::{debug, error, trace, warn}; use tracing::{debug, error, trace};
use crate::{debug_error, debug_warn, services, Error, Result}; use crate::{debug_error, debug_info, debug_warn, services, Error, Result};
/// Wraps either an literal IP address plus port, or a hostname plus complement /// Wraps either an literal IP address plus port, or a hostname plus complement
/// (colon-plus-port if it was specified). /// (colon-plus-port if it was specified).
@ -300,6 +300,7 @@ async fn resolve_actual_destination(destination: &'_ ServerName) -> Result<(FedD
Ok((actual_destination, hostname.into_uri_string())) Ok((actual_destination, hostname.into_uri_string()))
} }
#[tracing::instrument(skip_all, name = "well-known")]
async fn request_well_known(destination: &str) -> Result<Option<String>> { async fn request_well_known(destination: &str) -> Result<Option<String>> {
if !services() if !services()
.globals .globals
@ -320,22 +321,22 @@ async fn request_well_known(destination: &str) -> Result<Option<String>> {
.send() .send()
.await; .await;
trace!("Well known response: {:?}", response); trace!("response: {:?}", response);
if let Err(e) = &response { if let Err(e) = &response {
debug!("Well known error: {e:?}"); debug!("error: {e:?}");
return Ok(None); return Ok(None);
} }
let response = response?; let response = response?;
if !response.status().is_success() { if !response.status().is_success() {
debug!("Well known response not 2XX"); debug!("response not 2XX");
return Ok(None); return Ok(None);
} }
let text = response.text().await?; let text = response.text().await?;
trace!("Well known response text: {:?}", text); trace!("response text: {:?}", text);
if text.len() >= 12288 { if text.len() >= 12288 {
debug!("Well known response contains junk"); debug_warn!("response contains junk");
return Ok(None); return Ok(None);
} }
@ -348,13 +349,15 @@ async fn request_well_known(destination: &str) -> Result<Option<String>> {
.unwrap_or_default(); .unwrap_or_default();
if ruma_identifiers_validation::server_name::validate(m_server).is_err() { if ruma_identifiers_validation::server_name::validate(m_server).is_err() {
debug!("Well known response content missing or invalid"); debug_error!("response content missing or invalid");
return Ok(None); return Ok(None);
} }
debug_info!("{:?} found at {:?}", destination, m_server);
Ok(Some(m_server.to_owned())) Ok(Some(m_server.to_owned()))
} }
#[tracing::instrument(skip_all, name = "ip")]
async fn query_and_cache_override(overname: &'_ str, hostname: &'_ str, port: u16) -> Result<()> { async fn query_and_cache_override(overname: &'_ str, hostname: &'_ str, port: u16) -> Result<()> {
match services() match services()
.globals .globals
@ -362,8 +365,11 @@ async fn query_and_cache_override(overname: &'_ str, hostname: &'_ str, port: u1
.lookup_ip(hostname.to_owned()) .lookup_ip(hostname.to_owned())
.await .await
{ {
Err(e) => handle_resolve_error(&e),
Ok(override_ip) => { Ok(override_ip) => {
trace!("Caching result of {:?} overriding {:?}", hostname, overname); if hostname != overname {
debug_info!("{:?} overriden by {:?}", overname, hostname);
}
services() services()
.globals .globals
.resolver .resolver
@ -374,13 +380,10 @@ async fn query_and_cache_override(overname: &'_ str, hostname: &'_ str, port: u1
Ok(()) Ok(())
}, },
Err(e) => {
debug!("Got {:?} for {:?} to override {:?}", e.kind(), hostname, overname);
handle_resolve_error(&e)
},
} }
} }
#[tracing::instrument(skip_all, name = "srv")]
async fn query_srv_record(hostname: &'_ str) -> Result<Option<FedDest>> { async fn query_srv_record(hostname: &'_ str) -> Result<Option<FedDest>> {
fn handle_successful_srv(srv: &SrvLookup) -> Option<FedDest> { fn handle_successful_srv(srv: &SrvLookup) -> Option<FedDest> {
srv.iter().next().map(|result| { srv.iter().next().map(|result| {
@ -421,11 +424,11 @@ fn handle_resolve_error(e: &ResolveError) -> Result<()> {
ResolveErrorKind::Io { ResolveErrorKind::Io {
.. ..
} => { } => {
debug_error!("DNS IO error: {e}"); error!("{e}");
Err(Error::Error(e.to_string())) Err(Error::Error(e.to_string()))
}, },
_ => { _ => {
debug!("DNS protocol error: {e}"); debug_error!("{e}");
Ok(()) Ok(())
}, },
} }
@ -514,19 +517,17 @@ fn validate_destination(destination: &ServerName) -> Result<()> {
validate_destination_ip_literal(destination)?; validate_destination_ip_literal(destination)?;
} }
trace!("Destination ServerName is valid");
Ok(()) Ok(())
} }
fn validate_destination_ip_literal(destination: &ServerName) -> Result<()> { fn validate_destination_ip_literal(destination: &ServerName) -> Result<()> {
trace!("Destination is an IP literal, checking against IP range denylist.",);
debug_assert!( debug_assert!(
destination.is_ip_literal() || !IPAddress::is_valid(destination.host()), destination.is_ip_literal() || !IPAddress::is_valid(destination.host()),
"Destination is not an IP literal." "Destination is not an IP literal."
); );
debug!("Destination is an IP literal, checking against IP range denylist.",);
let ip = IPAddress::parse(destination.host()).map_err(|e| { let ip = IPAddress::parse(destination.host()).map_err(|e| {
debug_warn!("Failed to parse IP literal from string: {}", e); debug_error!("Failed to parse IP literal from string: {}", e);
Error::BadServerResponse("Invalid IP address") Error::BadServerResponse("Invalid IP address")
})?; })?;
@ -558,6 +559,7 @@ fn add_port_to_hostname(destination_str: &str) -> FedDest {
None => (destination_str, ":8448"), None => (destination_str, ":8448"),
Some(pos) => destination_str.split_at(pos), Some(pos) => destination_str.split_at(pos),
}; };
FedDest::Named(host.to_owned(), port.to_owned()) FedDest::Named(host.to_owned(), port.to_owned())
} }

View file

@ -1,14 +1,23 @@
/// Log event at given level in debug-mode (when debug-assertions are enabled).
/// In release mode it becomes DEBUG level, and possibly subject to elision.
#[macro_export]
macro_rules! debug_event {
( $level:expr, $($x:tt)+ ) => {
if cfg!(debug_assertions) {
tracing::event!( $level, $($x)+ );
} else {
tracing::debug!( $($x)+ );
}
}
}
/// Log message at the ERROR level in debug-mode (when debug-assertions are /// Log message at the ERROR level in debug-mode (when debug-assertions are
/// enabled). In release mode it becomes DEBUG level, and possibly subject to /// enabled). In release mode it becomes DEBUG level, and possibly subject to
/// elision. /// elision.
#[macro_export] #[macro_export]
macro_rules! debug_error { macro_rules! debug_error {
( $($x:tt)+ ) => { ( $($x:tt)+ ) => {
if cfg!(debug_assertions) { $crate::debug_event!(tracing::Level::ERROR, $($x)+ );
error!( $($x)+ );
} else {
debug!( $($x)+ );
}
} }
} }
@ -18,11 +27,7 @@ macro_rules! debug_error {
#[macro_export] #[macro_export]
macro_rules! debug_warn { macro_rules! debug_warn {
( $($x:tt)+ ) => { ( $($x:tt)+ ) => {
if cfg!(debug_assertions) { $crate::debug_event!(tracing::Level::WARN, $($x)+ );
warn!( $($x)+ );
} else {
debug!( $($x)+ );
}
} }
} }
@ -32,10 +37,6 @@ macro_rules! debug_warn {
#[macro_export] #[macro_export]
macro_rules! debug_info { macro_rules! debug_info {
( $($x:tt)+ ) => { ( $($x:tt)+ ) => {
if cfg!(debug_assertions) { $crate::debug_event!(tracing::Level::INFO, $($x)+ );
info!( $($x)+ );
} else {
debug!( $($x)+ );
}
} }
} }