From b8b4e9bb9dd60346cd66089418de1e0ae3ab4eaa Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 18 Jul 2026 12:57:26 +0200 Subject: [PATCH] Outline span creation in HttpClient::send --- crates/matrix-sdk/src/http_client/mod.rs | 75 ++++++++++++------------ 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/crates/matrix-sdk/src/http_client/mod.rs b/crates/matrix-sdk/src/http_client/mod.rs index 04343e0bf..08bb15798 100644 --- a/crates/matrix-sdk/src/http_client/mod.rs +++ b/crates/matrix-sdk/src/http_client/mod.rs @@ -36,7 +36,7 @@ use ruma::api::{ path_builder, }; use tokio::sync::{Semaphore, SemaphorePermit}; -use tracing::{debug, error, field::debug, instrument, trace}; +use tracing::{Instrument, debug, error, field::debug, trace}; use crate::{HttpResult, config::RequestConfig, error::HttpError}; @@ -134,21 +134,7 @@ impl HttpClient { Ok(request) } - #[allow(clippy::too_many_arguments)] - #[instrument( - skip(self, request, config, homeserver, access_token, path_builder_input, send_progress), - fields( - uri, - method, - request_id, - request_size, - request_duration, - status, - response_size, - sentry_event_id - ) - )] - pub async fn send( + pub fn send( &self, request: R, config: Option, @@ -156,7 +142,7 @@ impl HttpClient { access_token: Option<&str>, path_builder_input: ::Input<'_>, send_progress: SharedObservable, - ) -> Result + ) -> impl Future> where R: OutgoingRequest + Debug, R::Authentication: SupportedAuthScheme, @@ -164,12 +150,21 @@ impl HttpClient { { // some functions split out so they only get compiled once, // not monomorphized per request type - fn span_record_fields_1(client: &HttpClient, config: &RequestConfig) { - tracing::Span::current() - .record("config", debug(config)) - .record("request_id", client.get_request_id()); + fn make_span(client: &HttpClient, config: &RequestConfig) -> tracing::Span { + tracing::info_span!( + "send", + uri = tracing::field::Empty, + ?config, + method = tracing::field::Empty, + request_id = client.get_request_id(), + request_size = tracing::field::Empty, + request_duration = tracing::field::Empty, + status = tracing::field::Empty, + response_size = tracing::field::Empty, + sentry_event_id = tracing::field::Empty + ) } - fn span_record_fields_2(request: &http::Request) { + fn record_request_uri_and_size(request: &http::Request) { let method = request.method(); let mut uri_parts = request.uri().clone().into_parts(); @@ -210,27 +205,29 @@ impl HttpClient { None => self.request_config, }; - span_record_fields_1(self, &config); - let request = self - .serialize_request(request, config, homeserver, access_token, path_builder_input) - .map_err(HttpError::IntoHttp)?; - span_record_fields_2(&request); + async move { + let request = self + .serialize_request(request, config, homeserver, access_token, path_builder_input) + .map_err(HttpError::IntoHttp)?; + record_request_uri_and_size(&request); - // will be automatically dropped at the end of this function - let _handle = self.concurrent_request_semaphore.acquire().await; + // will be automatically dropped at the end of this function + let _handle = self.concurrent_request_semaphore.acquire().await; - // There's a bunch of state in send_request, factor out a pinned inner - // future to reduce the size of futures that await this function. - match Box::pin(self.send_request::(request, config, send_progress)).await { - Ok(response) => { - log_got_response(); - Ok(response) - } - Err(e) => { - log_error(&e); - Err(e) + // There's a bunch of state in send_request, factor out a pinned inner + // future to reduce the size of futures that await this function. + match Box::pin(self.send_request::(request, config, send_progress)).await { + Ok(response) => { + log_got_response(); + Ok(response) + } + Err(e) => { + log_error(&e); + Err(e) + } } } + .instrument(make_span(self, &config)) } }