opentelemetry-otlp 0.32.0

Exporter for the OpenTelemetry Collector
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! Error classification for OTLP exporters with protocol-specific throttling support.
//!
//! This module provides error classification functions for HTTP and gRPC protocols,
//! supporting server-provided throttling hints like HTTP Retry-After headers and
//! gRPC RetryInfo metadata.

use crate::retry::RetryErrorType;

#[cfg(feature = "grpc-tonic")]
use tonic;

#[cfg(feature = "grpc-tonic")]
use tonic_types::StatusExt;

/// HTTP-specific error classification with Retry-After header support.
#[cfg(feature = "experimental-http-retry")]
pub mod http {
    use super::*;
    use std::time::Duration;

    /// Classifies HTTP errors based on status code and headers.
    ///
    /// # Arguments
    /// * `status_code` - HTTP status code
    /// * `retry_after_header` - Value of the Retry-After header, if present
    ///
    /// # Retry-After Header Formats
    /// * Seconds: "120"
    /// * HTTP Date: "Fri, 31 Dec 1999 23:59:59 GMT"
    pub fn classify_http_error(
        status_code: u16,
        retry_after_header: Option<&str>,
    ) -> RetryErrorType {
        match status_code {
            // 429 Too Many Requests - check for Retry-After
            429 => {
                if let Some(retry_after) = retry_after_header {
                    if let Some(duration) = parse_retry_after(retry_after) {
                        return RetryErrorType::Throttled(duration);
                    }
                }
                // Fallback to retryable if no valid Retry-After
                RetryErrorType::Retryable
            }
            // 5xx Server errors - retryable
            500..=599 => RetryErrorType::Retryable,
            // 4xx Client errors (except 429) - not retryable
            400..=499 => RetryErrorType::NonRetryable,
            // Other codes - retryable (network issues, etc.)
            _ => RetryErrorType::Retryable,
        }
    }

    /// Parses the Retry-After header value.
    ///
    /// Supports both formats:
    /// - Delay seconds: "120"
    /// - HTTP date: "Fri, 31 Dec 1999 23:59:59 GMT"
    ///
    /// Returns None if parsing fails or delay is unreasonable.
    fn parse_retry_after(retry_after: &str) -> Option<Duration> {
        // Try parsing as seconds first
        if let Ok(seconds) = retry_after.trim().parse::<u64>() {
            // Cap at 10 minutes. TODO - what's sensible here?
            let capped_seconds = seconds.min(600);
            return Some(Duration::from_secs(capped_seconds));
        }

        // Try parsing as HTTP date
        if let Ok(delay_seconds) = parse_http_date_to_delay(retry_after) {
            // Cap at 10 minutes. TODO - what's sensible here?
            let capped_seconds = delay_seconds.min(600);
            return Some(Duration::from_secs(capped_seconds));
        }

        None
    }

    /// Parses HTTP date format and returns delay in seconds from now.
    fn parse_http_date_to_delay(date_str: &str) -> Result<u64, ()> {
        use std::time::SystemTime;

        // Try parse the date; if we fail, propagate an () error up to the caller.
        let target_time = httpdate::parse_http_date(date_str).map_err(|_| ())?;

        let now = SystemTime::now();
        let delay = target_time
            .duration_since(now)
            .unwrap_or(std::time::Duration::ZERO);
        Ok(delay.as_secs())
    }
}

/// gRPC-specific error classification with RetryInfo support.
#[cfg(feature = "grpc-tonic")]
pub mod grpc {
    use super::*;

    /// Classifies a tonic::Status error
    #[cfg(feature = "grpc-tonic")]
    pub fn classify_tonic_status(status: &tonic::Status) -> RetryErrorType {
        // Use tonic-types to extract RetryInfo - this is the proper way!
        let retry_info_seconds = status
            .get_details_retry_info()
            .and_then(|retry_info| retry_info.retry_delay)
            .map(|duration| duration.as_secs());

        classify_grpc_error(status.code(), retry_info_seconds)
    }

    /// Classifies gRPC errors based on status code and metadata.
    ///
    /// Implements the OpenTelemetry OTLP specification for error handling:
    /// https://opentelemetry.io/docs/specs/otlp/
    /// https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#failures
    ///
    /// # Arguments
    /// * `grpc_code` - gRPC status code as tonic::Code enum
    /// * `retry_info_seconds` - Parsed retry delay from RetryInfo metadata, if present
    fn classify_grpc_error(
        grpc_code: tonic::Code,
        retry_info_seconds: Option<u64>,
    ) -> RetryErrorType {
        match grpc_code {
            // RESOURCE_EXHAUSTED: Special case per OTLP spec
            // Retryable only if server provides RetryInfo indicating recovery is possible
            tonic::Code::ResourceExhausted => {
                if let Some(seconds) = retry_info_seconds {
                    // Server signals recovery is possible - use throttled retry
                    let capped_seconds = seconds.min(600); // Cap at 10 minutes. TODO - what's sensible here?
                    return RetryErrorType::Throttled(std::time::Duration::from_secs(
                        capped_seconds,
                    ));
                }
                // No RetryInfo - treat as non-retryable per OTLP spec
                RetryErrorType::NonRetryable
            }

            // Retryable errors per OTLP specification
            tonic::Code::Cancelled
            | tonic::Code::DeadlineExceeded
            | tonic::Code::Aborted
            | tonic::Code::OutOfRange
            | tonic::Code::Unavailable
            | tonic::Code::DataLoss => RetryErrorType::Retryable,

            // Non-retryable errors per OTLP specification
            tonic::Code::Unknown
            | tonic::Code::InvalidArgument
            | tonic::Code::NotFound
            | tonic::Code::AlreadyExists
            | tonic::Code::PermissionDenied
            | tonic::Code::FailedPrecondition
            | tonic::Code::Unimplemented
            | tonic::Code::Internal
            | tonic::Code::Unauthenticated => RetryErrorType::NonRetryable,

            // OK should never reach here in error scenarios, but handle gracefully
            tonic::Code::Ok => RetryErrorType::NonRetryable,
        }
    }
}

#[cfg(test)]
mod tests {
    // Tests for HTTP error classification
    #[cfg(feature = "experimental-http-retry")]
    mod http_tests {
        use crate::retry::RetryErrorType;
        use crate::retry_classification::http::*;
        use std::time::Duration;

        #[test]
        fn test_http_429_with_retry_after_seconds() {
            let result = classify_http_error(429, Some("30"));
            assert_eq!(result, RetryErrorType::Throttled(Duration::from_secs(30)));
        }

        #[test]
        fn test_http_429_with_large_retry_after_capped() {
            let result = classify_http_error(429, Some("900")); // 15 minutes
            assert_eq!(
                result,
                RetryErrorType::Throttled(std::time::Duration::from_secs(600))
            ); // Capped at 10 minutes
        }

        #[test]
        fn test_http_429_with_invalid_retry_after() {
            let result = classify_http_error(429, Some("invalid"));
            assert_eq!(result, RetryErrorType::Retryable); // Fallback
        }

        #[test]
        fn test_http_429_without_retry_after() {
            let result = classify_http_error(429, None);
            assert_eq!(result, RetryErrorType::Retryable); // Fallback
        }

        #[test]
        fn test_http_5xx_errors() {
            assert_eq!(classify_http_error(500, None), RetryErrorType::Retryable);
            assert_eq!(classify_http_error(502, None), RetryErrorType::Retryable);
            assert_eq!(classify_http_error(503, None), RetryErrorType::Retryable);
            assert_eq!(classify_http_error(599, None), RetryErrorType::Retryable);
        }

        #[test]
        fn test_http_4xx_errors() {
            assert_eq!(classify_http_error(400, None), RetryErrorType::NonRetryable);
            assert_eq!(classify_http_error(401, None), RetryErrorType::NonRetryable);
            assert_eq!(classify_http_error(403, None), RetryErrorType::NonRetryable);
            assert_eq!(classify_http_error(404, None), RetryErrorType::NonRetryable);
            assert_eq!(classify_http_error(499, None), RetryErrorType::NonRetryable);
        }

        #[test]
        fn test_http_other_errors() {
            assert_eq!(classify_http_error(100, None), RetryErrorType::Retryable);
            assert_eq!(classify_http_error(200, None), RetryErrorType::Retryable);
            assert_eq!(classify_http_error(300, None), RetryErrorType::Retryable);
        }

        #[test]
        #[cfg(feature = "experimental-http-retry")]
        fn test_http_429_with_retry_after_valid_date() {
            use std::time::SystemTime;

            // Create a time 30 seconds in the future
            let future_time = SystemTime::now() + Duration::from_secs(30);
            let date_str = httpdate::fmt_http_date(future_time);
            let result = classify_http_error(429, Some(&date_str));
            match result {
                RetryErrorType::Throttled(duration) => {
                    let secs = duration.as_secs();
                    assert!(
                        (29..=30).contains(&secs),
                        "Expected ~30 seconds, got {}",
                        secs
                    );
                }
                _ => panic!("Expected Throttled, got {:?}", result),
            }
        }

        #[test]
        #[cfg(feature = "experimental-http-retry")]
        fn test_http_429_with_retry_after_invalid_date() {
            let result = classify_http_error(429, Some("Not a valid date"));
            assert_eq!(result, RetryErrorType::Retryable); // Falls back to retryable
        }

        #[test]
        #[cfg(feature = "experimental-http-retry")]
        fn test_http_429_with_retry_after_malformed_date() {
            let result = classify_http_error(429, Some("Sun, 99 Nov 9999 99:99:99 GMT"));
            assert_eq!(result, RetryErrorType::Retryable); // Falls back to retryable
        }
    }

    // Tests for gRPC error classification using public interface
    #[cfg(feature = "grpc-tonic")]
    mod grpc_tests {
        use crate::retry::RetryErrorType;
        use crate::retry_classification::grpc::classify_tonic_status;
        use tonic_types::{ErrorDetails, StatusExt};

        #[test]
        fn test_grpc_resource_exhausted_with_retry_info() {
            let error_details =
                ErrorDetails::with_retry_info(Some(std::time::Duration::from_secs(45)));
            let status = tonic::Status::with_error_details(
                tonic::Code::ResourceExhausted,
                "rate limited",
                error_details,
            );
            let result = classify_tonic_status(&status);
            assert_eq!(
                result,
                RetryErrorType::Throttled(std::time::Duration::from_secs(45))
            );
        }

        #[test]
        fn test_grpc_resource_exhausted_with_large_retry_info_capped() {
            let error_details =
                ErrorDetails::with_retry_info(Some(std::time::Duration::from_secs(900))); // 15 minutes
            let status = tonic::Status::with_error_details(
                tonic::Code::ResourceExhausted,
                "rate limited",
                error_details,
            );
            let result = classify_tonic_status(&status);
            assert_eq!(
                result,
                RetryErrorType::Throttled(std::time::Duration::from_secs(600))
            ); // Capped at 10 minutes
        }

        #[test]
        fn test_grpc_resource_exhausted_without_retry_info() {
            let status = tonic::Status::new(tonic::Code::ResourceExhausted, "rate limited");
            let result = classify_tonic_status(&status);
            // Per OTLP spec: RESOURCE_EXHAUSTED without RetryInfo is non-retryable
            assert_eq!(result, RetryErrorType::NonRetryable);
        }

        #[test]
        fn test_grpc_retryable_errors() {
            // Test all retryable errors per OTLP specification
            let cancelled = tonic::Status::new(tonic::Code::Cancelled, "cancelled");
            assert_eq!(classify_tonic_status(&cancelled), RetryErrorType::Retryable);

            let deadline_exceeded =
                tonic::Status::new(tonic::Code::DeadlineExceeded, "deadline exceeded");
            assert_eq!(
                classify_tonic_status(&deadline_exceeded),
                RetryErrorType::Retryable
            );

            let aborted = tonic::Status::new(tonic::Code::Aborted, "aborted");
            assert_eq!(classify_tonic_status(&aborted), RetryErrorType::Retryable);

            let out_of_range = tonic::Status::new(tonic::Code::OutOfRange, "out of range");
            assert_eq!(
                classify_tonic_status(&out_of_range),
                RetryErrorType::Retryable
            );

            let unavailable = tonic::Status::new(tonic::Code::Unavailable, "unavailable");
            assert_eq!(
                classify_tonic_status(&unavailable),
                RetryErrorType::Retryable
            );

            let data_loss = tonic::Status::new(tonic::Code::DataLoss, "data loss");
            assert_eq!(classify_tonic_status(&data_loss), RetryErrorType::Retryable);
        }

        #[test]
        fn test_grpc_non_retryable_errors() {
            // Test all non-retryable errors per OTLP specification
            let unknown = tonic::Status::new(tonic::Code::Unknown, "unknown");
            assert_eq!(
                classify_tonic_status(&unknown),
                RetryErrorType::NonRetryable
            );

            let invalid_argument =
                tonic::Status::new(tonic::Code::InvalidArgument, "invalid argument");
            assert_eq!(
                classify_tonic_status(&invalid_argument),
                RetryErrorType::NonRetryable
            );

            let not_found = tonic::Status::new(tonic::Code::NotFound, "not found");
            assert_eq!(
                classify_tonic_status(&not_found),
                RetryErrorType::NonRetryable
            );

            let already_exists = tonic::Status::new(tonic::Code::AlreadyExists, "already exists");
            assert_eq!(
                classify_tonic_status(&already_exists),
                RetryErrorType::NonRetryable
            );

            let permission_denied =
                tonic::Status::new(tonic::Code::PermissionDenied, "permission denied");
            assert_eq!(
                classify_tonic_status(&permission_denied),
                RetryErrorType::NonRetryable
            );

            let failed_precondition =
                tonic::Status::new(tonic::Code::FailedPrecondition, "failed precondition");
            assert_eq!(
                classify_tonic_status(&failed_precondition),
                RetryErrorType::NonRetryable
            );

            let unimplemented = tonic::Status::new(tonic::Code::Unimplemented, "unimplemented");
            assert_eq!(
                classify_tonic_status(&unimplemented),
                RetryErrorType::NonRetryable
            );

            let internal = tonic::Status::new(tonic::Code::Internal, "internal error");
            assert_eq!(
                classify_tonic_status(&internal),
                RetryErrorType::NonRetryable
            );

            let unauthenticated =
                tonic::Status::new(tonic::Code::Unauthenticated, "unauthenticated");
            assert_eq!(
                classify_tonic_status(&unauthenticated),
                RetryErrorType::NonRetryable
            );
        }

        #[test]
        fn test_grpc_ok_code_handled() {
            // OK status should be handled gracefully (though unlikely in error scenarios)
            let ok = tonic::Status::new(tonic::Code::Ok, "success");
            assert_eq!(classify_tonic_status(&ok), RetryErrorType::NonRetryable);
        }

        // Tests for tonic-types RetryInfo integration
        #[cfg(feature = "grpc-tonic")]
        mod retry_info_tests {
            use super::*;
            use crate::retry_classification::grpc::classify_tonic_status;
            use tonic_types::{ErrorDetails, StatusExt};

            #[test]
            fn test_classify_status_with_retry_info() {
                // Create a tonic::Status with RetryInfo using proper StatusExt API
                let error_details =
                    ErrorDetails::with_retry_info(Some(std::time::Duration::from_secs(30)));
                let status = tonic::Status::with_error_details(
                    tonic::Code::ResourceExhausted,
                    "rate limited",
                    error_details,
                );

                // Test classification
                let result = classify_tonic_status(&status);
                assert_eq!(
                    result,
                    RetryErrorType::Throttled(std::time::Duration::from_secs(30))
                );
            }

            #[test]
            fn test_classify_status_with_fractional_retry_info() {
                // Create a tonic::Status with fractional seconds RetryInfo
                let error_details =
                    ErrorDetails::with_retry_info(Some(std::time::Duration::from_millis(5500))); // 5.5 seconds
                let status = tonic::Status::with_error_details(
                    tonic::Code::ResourceExhausted,
                    "rate limited",
                    error_details,
                );

                // Should use exact duration (5.5s = 5s)
                let result = classify_tonic_status(&status);
                assert_eq!(
                    result,
                    RetryErrorType::Throttled(std::time::Duration::from_secs(5))
                );
            }

            #[test]
            fn test_classify_status_without_retry_info() {
                // Status with resource_exhausted but no RetryInfo
                let status = tonic::Status::new(tonic::Code::ResourceExhausted, "rate limited");

                // Per OTLP spec: should be non-retryable without RetryInfo
                let result = classify_tonic_status(&status);
                assert_eq!(result, RetryErrorType::NonRetryable);
            }

            #[test]
            fn test_classify_status_non_retryable_error() {
                // Status with non-retryable error code
                let status = tonic::Status::new(tonic::Code::InvalidArgument, "bad request");

                let result = classify_tonic_status(&status);
                assert_eq!(result, RetryErrorType::NonRetryable);
            }

            #[test]
            fn test_classify_status_retryable_error() {
                // Status with retryable error code
                let status = tonic::Status::new(tonic::Code::Unavailable, "service unavailable");

                let result = classify_tonic_status(&status);
                assert_eq!(result, RetryErrorType::Retryable);
            }

            #[test]
            fn test_classify_status_large_retry_delay() {
                // Test with large retry delay - should be capped at 10 minutes
                let error_details =
                    ErrorDetails::with_retry_info(Some(std::time::Duration::from_secs(3600))); // 1 hour
                let status = tonic::Status::with_error_details(
                    tonic::Code::ResourceExhausted,
                    "rate limited",
                    error_details,
                );

                let result = classify_tonic_status(&status);
                // Should be capped at 10 minutes (600 seconds)
                assert_eq!(
                    result,
                    RetryErrorType::Throttled(std::time::Duration::from_secs(600))
                );
            }

            #[test]
            fn test_status_ext_get_details() {
                // Test that StatusExt works correctly
                let error_details =
                    ErrorDetails::with_retry_info(Some(std::time::Duration::from_secs(45)));
                let status = tonic::Status::with_error_details(
                    tonic::Code::ResourceExhausted,
                    "rate limited",
                    error_details,
                );

                // Direct extraction should work
                let extracted = status.get_details_retry_info();
                assert!(extracted.is_some());

                let retry_delay = extracted.unwrap().retry_delay;
                assert_eq!(retry_delay, Some(std::time::Duration::from_secs(45)));
            }
        }
    }
}