Skip to content

Commit

Permalink
feat(reqwest-retry): Configurable log level for retry event (#137)
Browse files Browse the repository at this point in the history
* Make retry log level configurable

* Add Changelog entry

* Update macro

* Add comment with issue to macro

* Update CHANGELOG
  • Loading branch information
Simon089 authored Apr 11, 2024
1 parent 24c277a commit abdf184
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions reqwest-retry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- Added `with_retry_log_level` to `RetryTransientMiddleware`

## [0.5.0] - 2024-04-10

### Breaking changes
Expand Down
27 changes: 26 additions & 1 deletion reqwest-retry/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ use reqwest::{Request, Response};
use reqwest_middleware::{Error, Middleware, Next, Result};
use retry_policies::RetryPolicy;

#[doc(hidden)]
// We need this macro because tracing expects the level to be const:
// https://github.com/tokio-rs/tracing/issues/2730
macro_rules! log_retry {
($level:expr, $($args:tt)*) => {{
match $level {
::tracing::Level::TRACE => ::tracing::trace!($($args)*),
::tracing::Level::DEBUG => ::tracing::debug!($($args)*),
::tracing::Level::INFO => ::tracing::info!($($args)*),
::tracing::Level::WARN => ::tracing::warn!($($args)*),
::tracing::Level::ERROR => ::tracing::error!($($args)*),
}
}};
}

/// `RetryTransientMiddleware` offers retry logic for requests that fail in a transient manner
/// and can be safely executed again.
///
Expand Down Expand Up @@ -53,13 +68,21 @@ pub struct RetryTransientMiddleware<
> {
retry_policy: T,
retryable_strategy: R,
retry_log_level: tracing::Level,
}

impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy> {
/// Construct `RetryTransientMiddleware` with a [retry_policy][RetryPolicy].
pub fn new_with_policy(retry_policy: T) -> Self {
Self::new_with_policy_and_strategy(retry_policy, DefaultRetryableStrategy)
}

/// Set the log [level][tracing::Level] for retry events.
/// The default is [`WARN`][tracing::Level::WARN].
pub fn with_retry_log_level(mut self, level: tracing::Level) -> Self {
self.retry_log_level = level;
self
}
}

impl<T, R> RetryTransientMiddleware<T, R>
Expand All @@ -72,6 +95,7 @@ where
Self {
retry_policy,
retryable_strategy,
retry_log_level: tracing::Level::WARN,
}
}
}
Expand Down Expand Up @@ -138,7 +162,8 @@ where
.to_std()
.map_err(Error::middleware)?;
// Sleep the requested amount before we try again.
tracing::warn!(
log_retry!(
self.retry_log_level,
"Retry attempt #{}. Sleeping {:?} before the next attempt",
n_past_retries,
duration
Expand Down

0 comments on commit abdf184

Please sign in to comment.