mirror of
https://github.com/TrueLayer/reqwest-middleware.git
synced 2024-12-25 18:36:30 +00:00
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:
parent
24c277a763
commit
abdf1844c3
2 changed files with 31 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
///
|
||||
|
@ -53,6 +68,7 @@ pub struct RetryTransientMiddleware<
|
|||
> {
|
||||
retry_policy: T,
|
||||
retryable_strategy: R,
|
||||
retry_log_level: tracing::Level,
|
||||
}
|
||||
|
||||
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 {
|
||||
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>
|
||||
|
@ -72,6 +95,7 @@ where
|
|||
Self {
|
||||
retry_policy,
|
||||
retryable_strategy,
|
||||
retry_log_level: tracing::Level::WARN,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue