feat(reqwest-retry): Configurable log level for retry event (#137)

* Make retry log level configurable

* Add Changelog entry

* Update macro

* Add comment with issue to macro

* Update CHANGELOG
This commit is contained in:
Simon 2024-04-11 02:16:33 -07:00 committed by GitHub
parent 24c277a763
commit abdf1844c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View file

@ -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/), 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). 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 ## [0.5.0] - 2024-04-10
### Breaking changes ### Breaking changes

View file

@ -8,6 +8,21 @@ use reqwest::{Request, Response};
use reqwest_middleware::{Error, Middleware, Next, Result}; use reqwest_middleware::{Error, Middleware, Next, Result};
use retry_policies::RetryPolicy; 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 /// `RetryTransientMiddleware` offers retry logic for requests that fail in a transient manner
/// and can be safely executed again. /// and can be safely executed again.
/// ///
@ -53,6 +68,7 @@ pub struct RetryTransientMiddleware<
> { > {
retry_policy: T, retry_policy: T,
retryable_strategy: R, retryable_strategy: R,
retry_log_level: tracing::Level,
} }
impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy> { impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableStrategy> {
@ -60,6 +76,13 @@ impl<T: RetryPolicy + Send + Sync> RetryTransientMiddleware<T, DefaultRetryableS
pub fn new_with_policy(retry_policy: T) -> Self { pub fn new_with_policy(retry_policy: T) -> Self {
Self::new_with_policy_and_strategy(retry_policy, DefaultRetryableStrategy) 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> impl<T, R> RetryTransientMiddleware<T, R>
@ -72,6 +95,7 @@ where
Self { Self {
retry_policy, retry_policy,
retryable_strategy, retryable_strategy,
retry_log_level: tracing::Level::WARN,
} }
} }
} }
@ -138,7 +162,8 @@ where
.to_std() .to_std()
.map_err(Error::middleware)?; .map_err(Error::middleware)?;
// Sleep the requested amount before we try again. // Sleep the requested amount before we try again.
tracing::warn!( log_retry!(
self.retry_log_level,
"Retry attempt #{}. Sleeping {:?} before the next attempt", "Retry attempt #{}. Sleeping {:?} before the next attempt",
n_past_retries, n_past_retries,
duration duration