Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
tl-eirik-albrigtsen marked this conversation as resolved.
Show resolved Hide resolved
"Retry attempt #{}. Sleeping {:?} before the next attempt",
n_past_retries,
duration
Expand Down
Loading