From a54a421f9115058bb7ddd41bcadc39b94e897a36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 19 Jan 2023 11:50:52 +0100 Subject: [PATCH] chore(sdk): Log the response/request sizes as well --- Cargo.lock | 7 +++++++ crates/matrix-sdk/Cargo.toml | 1 + crates/matrix-sdk/src/http_client.rs | 29 +++++++++++++++++++++------- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 33cb108ed..34a146d4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -517,6 +517,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfb24e866b15a1af2a1b663f10c6b6b8f397a84aadb828f12e5b289ec23a3a3c" +[[package]] +name = "bytesize" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c58ec36aac5066d5ca17df51b3e70279f5670a72102f5752cb7e7c856adfc70" + [[package]] name = "camino" version = "1.1.2" @@ -2576,6 +2582,7 @@ dependencies = [ "async-trait", "backoff", "bytes", + "bytesize", "chrono", "ctor", "dashmap", diff --git a/crates/matrix-sdk/Cargo.toml b/crates/matrix-sdk/Cargo.toml index ed813ec05..6315a210f 100644 --- a/crates/matrix-sdk/Cargo.toml +++ b/crates/matrix-sdk/Cargo.toml @@ -64,6 +64,7 @@ anymap2 = "0.13.0" async-stream = { workspace = true } async-trait = { workspace = true } bytes = "1.1.0" +bytesize = "1.1" chrono = { version = "0.4.23", optional = true } dashmap = { workspace = true } derive_builder = { version = "0.11.2", optional = true } diff --git a/crates/matrix-sdk/src/http_client.rs b/crates/matrix-sdk/src/http_client.rs index ba8e4ea93..d412ad303 100644 --- a/crates/matrix-sdk/src/http_client.rs +++ b/crates/matrix-sdk/src/http_client.rs @@ -24,6 +24,7 @@ use std::{ use async_trait::async_trait; use bytes::{Bytes, BytesMut}; +use bytesize::ByteSize; use matrix_sdk_common::AsyncTraitDeps; use ruma::{ api::{ @@ -164,7 +165,7 @@ impl HttpClient { &self, request: http::Request, config: RequestConfig, - ) -> Result<(http::StatusCode, R::IncomingResponse), HttpError> + ) -> Result<(http::StatusCode, ByteSize, R::IncomingResponse), HttpError> where R: OutgoingRequest + Debug, HttpError: From>, @@ -230,11 +231,12 @@ impl HttpClient { .map_err(error_type)?; let status_code = response.status(); + let response_size = ByteSize(response.body().len().try_into().unwrap_or(u64::MAX)); let response = R::IncomingResponse::try_from_http_response(response) .map_err(|e| error_type(HttpError::from(e)))?; - Ok((status_code, response)) + Ok((status_code, response_size, response)) }; retry::<_, HttpError, _, _, _>(backoff, send_request).await? @@ -244,8 +246,9 @@ impl HttpClient { let ret = { let response = self.inner.send_request(request, config.timeout).await?; let status_code = response.status(); + let response_size = ByteSize(response.body().len().try_into().unwrap_or(u64::MAX)); - (status_code, R::IncomingResponse::try_from_http_response(response)?) + (status_code, response_size, R::IncomingResponse::try_from_http_response(response)?) }; Ok(ret) @@ -253,7 +256,16 @@ impl HttpClient { #[instrument( skip(self, access_token, config, request, user_id), - fields(config, path, user_id, request_body, request_id, status) + fields( + config, + path, + user_id, + request_size, + request_body, + request_id, + status, + response_size, + ) )] pub async fn send( &self, @@ -300,7 +312,9 @@ impl HttpClient { server_versions, )?; - span.record("path", request.uri().path()); + let request_size = ByteSize(request.body().len().try_into().unwrap_or(u64::MAX)); + span.record("path", request.uri().path()) + .record("request_size", request_size.to_string_as(true)); // Since sliding sync is experimental, and the proxy might not do what we expect // it to do given a specific request body, it's useful to log the @@ -313,8 +327,9 @@ impl HttpClient { debug!("Sending request"); match self.send_request::(request, config).await { - Ok((status_code, response)) => { - span.record("status", status_code.as_u16()); + Ok((status_code, response_size, response)) => { + span.record("status", status_code.as_u16()) + .record("response_size", response_size.to_string_as(true)); debug!("Got response"); Ok(response)