Skip to content

Commit

Permalink
Add fields to log events when executing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
orf committed Dec 13, 2024
1 parent 71a2310 commit 6e3f247
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
19 changes: 10 additions & 9 deletions src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ pub trait Client {

/// This method provides a common interface to
/// [Endpoints][crate::endpoint::Endpoint] for execution.
#[instrument(skip(self, req), err)]
#[instrument(skip(self, req), fields(uri=%req.uri(), method=%req.method()), err)]
fn execute(&self, req: Request<Vec<u8>>) -> Result<Response<Vec<u8>>, ClientError> {
debug!(
"Client sending {} request to {} with {} bytes of data",
req.method().to_string(),
req.uri(),
req.body().len(),
name: "sending_request",
body_len=req.body().len(),
"Sending Request",
);
let response = self.send(req)?;

let status = response.status();
debug!(
"Client received {} response with {} bytes of body data",
response.status().as_u16(),
response.body().len()
name: "response_received",
status=status.as_u16(),
response_len=response.body().len(),
is_error=status.is_client_error() || status.is_server_error(),
"Response Received",
);

// Check response
Expand Down
19 changes: 10 additions & 9 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,21 @@ pub trait Client: Sync + Send {
// TODO: remove the allow when the upstream clippy issue is fixed:
// <https://github.com/rust-lang/rust-clippy/issues/12281>
#[allow(clippy::blocks_in_conditions)]
#[instrument(skip(self, req), err)]
#[instrument(skip(self, req), fields(uri=%req.uri(), method=%req.method()), err)]
async fn execute(&self, req: Request<Vec<u8>>) -> Result<Response<Vec<u8>>, ClientError> {
debug!(
"Client sending {} request to {} with {} bytes of data",
req.method().to_string(),
req.uri(),
req.body().len(),
name: "sending_request",
body_len=req.body().len(),
"Sending Request",
);
let response = self.send(req).await?;

let status = response.status();
debug!(
"Client received {} response with {} bytes of body data",
response.status().as_u16(),
response.body().len()
name: "response_received",
status=status.as_u16(),
response_len=response.body().len(),
is_error=status.is_client_error() || status.is_server_error(),
"Response Received",
);

// Check response
Expand Down
8 changes: 4 additions & 4 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<E: Endpoint, M: MiddleWare> Endpoint for MutatedEndpoint<'_, E, M> {
&self,
client: &impl Client,
) -> Result<EndpointResult<Self::Response>, ClientError> {
debug!("Executing endpoint");
trace!("Executing endpoint");

let req = self.request(client.base())?;
let resp = exec_mut(client, self, req, self.middleware).await?;
Expand All @@ -107,7 +107,7 @@ impl<E: Endpoint, M: MiddleWare> Endpoint for MutatedEndpoint<'_, E, M> {
&self,
client: &impl BlockingClient,
) -> Result<EndpointResult<Self::Response>, ClientError> {
debug!("Executing endpoint");
trace!("Executing endpoint");

let req = self.request(client.base())?;
let resp = exec_block_mut(client, self, req, self.middleware)?;
Expand Down Expand Up @@ -232,7 +232,7 @@ pub trait Endpoint: Send + Sync + Sized {
&self,
client: &impl Client,
) -> Result<EndpointResult<Self::Response>, ClientError> {
debug!("Executing endpoint");
trace!("Executing endpoint");

let req = self.request(client.base())?;
let resp = exec(client, req).await?;
Expand All @@ -250,7 +250,7 @@ pub trait Endpoint: Send + Sync + Sized {
&self,
client: &impl BlockingClient,
) -> Result<EndpointResult<Self::Response>, ClientError> {
debug!("Executing endpoint");
trace!("Executing endpoint");

let req = self.request(client.base())?;
let resp = exec_block(client, req)?;
Expand Down
2 changes: 1 addition & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn build_request(
query: Option<String>,
data: Option<Vec<u8>>,
) -> Result<Request<Vec<u8>>, ClientError> {
debug!("Building endpoint request");
trace!("Building endpoint request");
let uri = build_url(base, path, query)?;

let method_err = method.clone();
Expand Down

0 comments on commit 6e3f247

Please sign in to comment.