Skip to content

Commit 603c31e

Browse files
committed
simplify retry interface
1 parent fb141db commit 603c31e

File tree

4 files changed

+22
-59
lines changed

4 files changed

+22
-59
lines changed

opentelemetry-otlp/src/exporter/tonic/logs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use opentelemetry_proto::transform::logs::tonic::group_logs_by_resource_and_scop
1515
use super::BoxInterceptor;
1616

1717
use crate::retry_classification::grpc::classify_tonic_status;
18-
use opentelemetry_sdk::retry::{retry_with_exponential_backoff_classified, RetryPolicy};
18+
use opentelemetry_sdk::retry::{retry_with_backoff, RetryPolicy};
1919
use opentelemetry_sdk::runtime::Tokio;
2020

2121
pub(crate) struct TonicLogsClient {
@@ -72,7 +72,7 @@ impl LogExporter for TonicLogsClient {
7272

7373
let batch = Arc::new(batch);
7474

75-
match retry_with_exponential_backoff_classified(
75+
match retry_with_backoff(
7676
Tokio,
7777
policy,
7878
classify_tonic_status,

opentelemetry-otlp/src/exporter/tonic/metrics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::BoxInterceptor;
1313
use crate::metric::MetricsClient;
1414

1515
use crate::retry_classification::grpc::classify_tonic_status;
16-
use opentelemetry_sdk::retry::{retry_with_exponential_backoff_classified, RetryPolicy};
16+
use opentelemetry_sdk::retry::{retry_with_backoff, RetryPolicy};
1717
use opentelemetry_sdk::runtime::Tokio;
1818

1919
pub(crate) struct TonicMetricsClient {
@@ -64,7 +64,7 @@ impl MetricsClient for TonicMetricsClient {
6464
jitter_ms: 100,
6565
};
6666

67-
match retry_with_exponential_backoff_classified(
67+
match retry_with_backoff(
6868
Tokio,
6969
policy,
7070
classify_tonic_status,

opentelemetry-otlp/src/exporter/tonic/trace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use tonic::{codegen::CompressionEncoding, service::Interceptor, transport::Chann
1717
use super::BoxInterceptor;
1818

1919
use crate::retry_classification::grpc::classify_tonic_status;
20-
use opentelemetry_sdk::retry::{retry_with_exponential_backoff_classified, RetryPolicy};
20+
use opentelemetry_sdk::retry::{retry_with_backoff, RetryPolicy};
2121
use opentelemetry_sdk::runtime::Tokio;
2222

2323
pub(crate) struct TonicTracesClient {
@@ -74,7 +74,7 @@ impl SpanExporter for TonicTracesClient {
7474

7575
let batch = Arc::new(batch);
7676

77-
match retry_with_exponential_backoff_classified(
77+
match retry_with_backoff(
7878
Tokio,
7979
policy,
8080
classify_tonic_status,

opentelemetry-sdk/src/retry.rs

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
//! The `RetryPolicy` struct defines the configuration for the retry behavior, including the maximum
44
//! number of retries, initial delay, maximum delay, and jitter.
55
//!
6-
//! The `retry_with_exponential_backoff` function retries the given operation according to the
6+
//! The `retry_with_backoff` function retries the given operation according to the
77
//! specified retry policy, using exponential backoff and jitter to determine the delay between
8-
//! retries. The function logs errors and retries the operation until it succeeds or the maximum
9-
//! number of retries is reached.
8+
//! retries. The function uses error classification to determine retry behavior and can honor
9+
//! server-provided throttling hints.
1010
1111
#[cfg(feature = "experimental_async_runtime")]
1212
use opentelemetry::otel_warn;
@@ -69,45 +69,7 @@ fn generate_jitter(max_jitter: u64) -> u64 {
6969
nanos as u64 % (max_jitter + 1)
7070
}
7171

72-
/// Retries the given operation with exponential backoff and jitter.
73-
///
74-
/// # Arguments
75-
///
76-
/// * `runtime` - The async runtime to use for delays.
77-
/// * `policy` - The retry policy configuration.
78-
/// * `operation_name` - The name of the operation being retried.
79-
/// * `operation` - The operation to be retried.
80-
///
81-
/// # Returns
82-
///
83-
/// A `Result` containing the operation's result or an error if the maximum retries are reached.
84-
#[cfg(feature = "experimental_async_runtime")]
85-
pub async fn retry_with_exponential_backoff<R, F, Fut, T, E>(
86-
runtime: R,
87-
policy: RetryPolicy,
88-
operation_name: &str,
89-
operation: F,
90-
) -> Result<T, E>
91-
where
92-
R: Runtime,
93-
F: FnMut() -> Fut,
94-
E: std::fmt::Debug,
95-
Fut: Future<Output = Result<T, E>>,
96-
{
97-
// Use a simple classifier that treats all errors as retryable
98-
let simple_classifier = |_: &E| RetryErrorType::Retryable;
99-
100-
retry_with_exponential_backoff_classified(
101-
runtime,
102-
policy,
103-
simple_classifier,
104-
operation_name,
105-
operation,
106-
)
107-
.await
108-
}
109-
110-
/// Enhanced retry with exponential backoff, jitter, and error classification.
72+
/// Retries the given operation with exponential backoff, jitter, and error classification.
11173
///
11274
/// This function provides sophisticated retry behavior by classifying errors
11375
/// and honoring server-provided throttling hints (e.g., HTTP Retry-After, gRPC RetryInfo).
@@ -125,7 +87,7 @@ where
12587
/// A `Result` containing the operation's result or an error if max retries are reached
12688
/// or a non-retryable error occurs.
12789
#[cfg(feature = "experimental_async_runtime")]
128-
pub async fn retry_with_exponential_backoff_classified<R, F, Fut, T, E, C>(
90+
pub async fn retry_with_backoff<R, F, Fut, T, E, C>(
12991
runtime: R,
13092
policy: RetryPolicy,
13193
error_classifier: C,
@@ -186,9 +148,10 @@ where
186148
/// No-op retry function for when experimental_async_runtime is not enabled.
187149
/// This function will execute the operation exactly once without any retries.
188150
#[cfg(not(feature = "experimental_async_runtime"))]
189-
pub async fn retry_with_exponential_backoff<R, F, Fut, T, E>(
151+
pub async fn retry_with_backoff<R, F, Fut, T, E, C>(
190152
_runtime: R,
191153
_policy: RetryPolicy,
154+
_error_classifier: C,
192155
_operation_name: &str,
193156
mut operation: F,
194157
) -> Result<T, E>
@@ -227,7 +190,7 @@ mod tests {
227190
jitter_ms: 100,
228191
};
229192

230-
let result = retry_with_exponential_backoff(runtime, policy, "test_operation", || {
193+
let result = retry_with_backoff(runtime, policy, |_: &()| RetryErrorType::Retryable, "test_operation", || {
231194
Box::pin(async { Ok::<_, ()>("success") })
232195
})
233196
.await;
@@ -248,7 +211,7 @@ mod tests {
248211

249212
let attempts = AtomicUsize::new(0);
250213

251-
let result = retry_with_exponential_backoff(runtime, policy, "test_operation", || {
214+
let result = retry_with_backoff(runtime, policy, |_: &&str| RetryErrorType::Retryable, "test_operation", || {
252215
let attempt = attempts.fetch_add(1, Ordering::SeqCst);
253216
Box::pin(async move {
254217
if attempt < 2 {
@@ -277,7 +240,7 @@ mod tests {
277240

278241
let attempts = AtomicUsize::new(0);
279242

280-
let result = retry_with_exponential_backoff(runtime, policy, "test_operation", || {
243+
let result = retry_with_backoff(runtime, policy, |_: &&str| RetryErrorType::Retryable, "test_operation", || {
281244
attempts.fetch_add(1, Ordering::SeqCst);
282245
Box::pin(async { Err::<(), _>("error") }) // Always fail
283246
})
@@ -300,7 +263,7 @@ mod tests {
300263

301264
let result = timeout(
302265
Duration::from_secs(1),
303-
retry_with_exponential_backoff(runtime, policy, "test_operation", || {
266+
retry_with_backoff(runtime, policy, |_: &&str| RetryErrorType::Retryable, "test_operation", || {
304267
Box::pin(async { Err::<(), _>("error") }) // Always fail
305268
}),
306269
)
@@ -337,7 +300,7 @@ mod tests {
337300
// Classifier that returns non-retryable
338301
let classifier = |_: &()| RetryErrorType::NonRetryable;
339302

340-
let result = retry_with_exponential_backoff_classified(
303+
let result = retry_with_backoff(
341304
runtime,
342305
policy,
343306
classifier,
@@ -368,7 +331,7 @@ mod tests {
368331
// Classifier that returns retryable
369332
let classifier = |_: &()| RetryErrorType::Retryable;
370333

371-
let result = retry_with_exponential_backoff_classified(
334+
let result = retry_with_backoff(
372335
runtime,
373336
policy,
374337
classifier,
@@ -407,7 +370,7 @@ mod tests {
407370

408371
let start_time = std::time::Instant::now();
409372

410-
let result = retry_with_exponential_backoff_classified(
373+
let result = retry_with_backoff(
411374
runtime,
412375
policy,
413376
classifier,
@@ -447,7 +410,7 @@ mod tests {
447410
// Classifier that returns retryable
448411
let classifier = |_: &()| RetryErrorType::Retryable;
449412

450-
let result = retry_with_exponential_backoff_classified(
413+
let result = retry_with_backoff(
451414
runtime,
452415
policy,
453416
classifier,
@@ -482,7 +445,7 @@ mod tests {
482445
_ => RetryErrorType::Retryable,
483446
};
484447

485-
let result = retry_with_exponential_backoff_classified(
448+
let result = retry_with_backoff(
486449
runtime,
487450
policy,
488451
classifier,

0 commit comments

Comments
 (0)