From 4d1ff8de23b39ef38e86a612fd31be646bfb9a24 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 15 Feb 2022 12:14:48 +0100 Subject: [PATCH] client: Add some tracing spans --- crates/ruma-client/Cargo.toml | 1 + crates/ruma-client/src/lib.rs | 37 +++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/crates/ruma-client/Cargo.toml b/crates/ruma-client/Cargo.toml index e24f7630..e4a07f83 100644 --- a/crates/ruma-client/Cargo.toml +++ b/crates/ruma-client/Cargo.toml @@ -48,6 +48,7 @@ ruma-identifiers = { version = "0.20.0", path = "../ruma-identifiers" } ruma-serde = { version = "0.5.0", path = "../ruma-serde" } serde = { version = "1.0.118", features = ["derive"] } serde_json = "1.0.61" +tracing = { version = "0.1.30", default-features = false, features = ["std"] } [dev-dependencies] ruma-client-api = { version = "0.12.3", path = "../ruma-client-api", features = ["client"] } diff --git a/crates/ruma-client/src/lib.rs b/crates/ruma-client/src/lib.rs index fd7e5fa6..d58b7227 100644 --- a/crates/ruma-client/src/lib.rs +++ b/crates/ruma-client/src/lib.rs @@ -94,12 +94,14 @@ #![cfg_attr(docsrs, feature(doc_auto_cfg))] use std::{ + any::type_name, future::Future, sync::{Arc, Mutex}, }; use ruma_api::{MatrixVersion, OutgoingRequest, SendAccessToken}; use ruma_identifiers::UserId; +use tracing::{info_span, Instrument}; // "Undo" rename from `Cargo.toml` that only serves to make crate names available as a Cargo // feature names. @@ -244,17 +246,36 @@ where R: OutgoingRequest, F: FnOnce(&mut http::Request) -> Result<(), ResponseError>, { - let http_req = request - .try_into_http_request(homeserver_url, send_access_token, for_versions) - .map_err(ResponseError::::from) - .and_then(|mut req| { - customize(&mut req)?; - Ok(req) + let http_req = + info_span!("serialize_request", request_type = type_name::()).in_scope(move || { + request + .try_into_http_request(homeserver_url, send_access_token, for_versions) + .map_err(ResponseError::::from) + .and_then(|mut req| { + customize(&mut req)?; + Ok(req) + }) }); + let send_span = info_span!( + "send_request", + request_type = type_name::(), + http_client = type_name::(), + homeserver_url, + ); + async move { - let http_res = http_client.send_http_request(http_req?).await.map_err(Error::Response)?; - Ok(ruma_api::IncomingResponse::try_from_http_response(http_res)?) + let http_res = http_client + .send_http_request(http_req?) + .instrument(send_span) + .await + .map_err(Error::Response)?; + + let res = + info_span!("deserialize_response", response_type = type_name::()) + .in_scope(move || ruma_api::IncomingResponse::try_from_http_response(http_res))?; + + Ok(res) } }