Skip to main content

tower_resilience_fallback/
events.rs

1//! Events emitted by the fallback service.
2
3use std::time::Instant;
4use tower_resilience_core::ResilienceEvent;
5
6/// Events emitted by the fallback service.
7#[derive(Debug, Clone)]
8pub enum FallbackEvent {
9    /// The inner service succeeded; no fallback was needed.
10    Success {
11        /// Name of the fallback instance.
12        pattern_name: String,
13        /// When the event occurred.
14        timestamp: Instant,
15    },
16
17    /// The inner service failed; fallback will be attempted.
18    FailedAttempt {
19        /// Name of the fallback instance.
20        pattern_name: String,
21        /// When the event occurred.
22        timestamp: Instant,
23    },
24
25    /// The fallback was successfully applied.
26    Applied {
27        /// Name of the fallback instance.
28        pattern_name: String,
29        /// When the event occurred.
30        timestamp: Instant,
31        /// The strategy that was applied.
32        strategy: &'static str,
33    },
34
35    /// The fallback itself failed (only possible with service fallback).
36    Failed {
37        /// Name of the fallback instance.
38        pattern_name: String,
39        /// When the event occurred.
40        timestamp: Instant,
41    },
42
43    /// The error didn't match the predicate; propagated as-is.
44    Skipped {
45        /// Name of the fallback instance.
46        pattern_name: String,
47        /// When the event occurred.
48        timestamp: Instant,
49    },
50}
51
52impl ResilienceEvent for FallbackEvent {
53    fn event_type(&self) -> &'static str {
54        match self {
55            Self::Success { .. } => "success",
56            Self::FailedAttempt { .. } => "failed_attempt",
57            Self::Applied { .. } => "applied",
58            Self::Failed { .. } => "failed",
59            Self::Skipped { .. } => "skipped",
60        }
61    }
62
63    fn timestamp(&self) -> Instant {
64        match self {
65            Self::Success { timestamp, .. }
66            | Self::FailedAttempt { timestamp, .. }
67            | Self::Applied { timestamp, .. }
68            | Self::Failed { timestamp, .. }
69            | Self::Skipped { timestamp, .. } => *timestamp,
70        }
71    }
72
73    fn pattern_name(&self) -> &str {
74        match self {
75            Self::Success { pattern_name, .. }
76            | Self::FailedAttempt { pattern_name, .. }
77            | Self::Applied { pattern_name, .. }
78            | Self::Failed { pattern_name, .. }
79            | Self::Skipped { pattern_name, .. } => pattern_name,
80        }
81    }
82}