Fix tracing in send_request()

This commit is contained in:
Lambda 2024-06-02 16:48:22 +00:00 committed by Charles Hall
parent c6f75a1b93
commit 7dbae9ac2b
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF

View file

@ -63,7 +63,7 @@ use ruma::{
}; };
use serde_json::value::{to_raw_value, RawValue as RawJsonValue}; use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tracing::{debug, error, warn}; use tracing::{debug, error, field, warn};
use crate::{ use crate::{
api::client_server::{self, claim_keys_helper, get_keys_helper}, api::client_server::{self, claim_keys_helper, get_keys_helper},
@ -129,7 +129,7 @@ impl FedDest {
} }
} }
#[tracing::instrument(skip(request), fields(destination_cache_result))] #[tracing::instrument(skip(request), fields(destination_cache_result, url))]
pub(crate) async fn send_request<T>( pub(crate) async fn send_request<T>(
destination: &ServerName, destination: &ServerName,
request: T, request: T,
@ -147,7 +147,7 @@ where
)); ));
} }
debug!("Preparing to send request to {destination}"); debug!("Preparing to send request");
let mut write_destination_to_cache = false; let mut write_destination_to_cache = false;
@ -250,16 +250,17 @@ where
let reqwest_request = reqwest::Request::try_from(http_request)?; let reqwest_request = reqwest::Request::try_from(http_request)?;
let url = reqwest_request.url().clone(); let url = reqwest_request.url().clone();
tracing::Span::current().record("url", field::display(url));
debug!("Sending request to {destination} at {url}"); debug!("Sending request");
let response = let response =
services().globals.federation_client().execute(reqwest_request).await; services().globals.federation_client().execute(reqwest_request).await;
debug!("Received response from {destination} at {url}");
match response { match response {
Ok(mut response) => { Ok(mut response) => {
// reqwest::Response -> http::Response conversion // reqwest::Response -> http::Response conversion
let status = response.status(); let status = response.status();
debug!(status = u16::from(status), "Received response");
let mut http_response_builder = http::Response::builder() let mut http_response_builder = http::Response::builder()
.status(status) .status(status)
.version(response.version()); .version(response.version());
@ -270,23 +271,22 @@ where
.expect("http::response::Builder is usable"), .expect("http::response::Builder is usable"),
); );
debug!("Getting response bytes from {destination}"); debug!("Getting response bytes");
// TODO: handle timeout // TODO: handle timeout
let body = response.bytes().await.unwrap_or_else(|e| { let body = response.bytes().await.unwrap_or_else(|e| {
warn!("server error {}", e); warn!("server error {}", e);
Vec::new().into() Vec::new().into()
}); });
debug!("Got response bytes from {destination}"); debug!("Got response bytes");
if status != 200 { if status != 200 {
warn!( warn!(
"{} {}: {}", status = u16::from(status),
url, response = String::from_utf8_lossy(&body)
status,
String::from_utf8_lossy(&body)
.lines() .lines()
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(" ") .join(" "),
"Received error over federation",
); );
} }
@ -295,7 +295,7 @@ where
.expect("reqwest body is valid http body"); .expect("reqwest body is valid http body");
if status == 200 { if status == 200 {
debug!("Parsing response bytes from {destination}"); debug!("Parsing response bytes");
let response = let response =
T::IncomingResponse::try_from_http_response(http_response); T::IncomingResponse::try_from_http_response(http_response);
if response.is_ok() && write_destination_to_cache { if response.is_ok() && write_destination_to_cache {
@ -312,16 +312,12 @@ where
} }
response.map_err(|e| { response.map_err(|e| {
warn!( warn!(error = %e, "Invalid 200 response",);
"Invalid 200 response from {} on: {} {}",
&destination, url, e
);
Error::BadServerResponse( Error::BadServerResponse(
"Server returned bad 200 response.", "Server returned bad 200 response.",
) )
}) })
} else { } else {
debug!("Returning error from {destination}");
Err(Error::Federation( Err(Error::Federation(
destination.to_owned(), destination.to_owned(),
RumaError::from_http_response(http_response), RumaError::from_http_response(http_response),
@ -330,8 +326,8 @@ where
} }
Err(e) => { Err(e) => {
warn!( warn!(
"Could not send request to {} at {}: {}", error = %e,
destination, actual_destination_str, e "Could not send request",
); );
Err(e.into()) Err(e.into())
} }