trait_net/metrics/
mod.rs

1mod future_ext;
2
3#[cfg(feature = "prometheus")]
4pub mod prometheus;
5
6pub use future_ext::{ObservedFuture, ObservedFutureExt};
7
8/// Represents an observer, which reacts to different events happening to the
9/// future.
10///
11/// If any of the functions is used incorrectly, implementation's behaviour
12/// could be undefined but must always be safe.
13#[allow(unused_variables)]
14pub trait Observer<Out: ?Sized> {
15    /// Called once before the first future's poll.
16    fn on_first_poll(&mut self) {}
17
18    /// Called each time before the future is polled, but *after* `on_first_poll`.
19    fn on_poll(&mut self) {}
20
21    /// Called once after the future returns `Poll::Ready`.
22    fn on_poll_ready(&mut self, output: &Out) {}
23
24    /// Called once after `on_poll_ready` or before `on_drop`, depending on
25    /// whether future was cancelled or not. It isn't called if there was no
26    /// call to `on_first_poll`.
27    fn on_finish(&mut self, output: Option<&Out>) {}
28
29    /// Called once before the future is dropped, therefore is never called twice
30    /// and forbids calls to other trait's functions.
31    fn on_drop(&mut self) {}
32}
33
34/// Represents the object that can be seen as a status in metrics system.
35pub trait AsStatusLabel {
36    /// Get status label of this object.
37    fn as_status_label(&self) -> String;
38}
39
40impl<T, E: AsStatusLabel> AsStatusLabel for Result<T, E> {
41    fn as_status_label(&self) -> String {
42        match self {
43            Ok(_) => "ok".to_owned(),
44            Err(err) => err.as_status_label(),
45        }
46    }
47}
48
49impl AsStatusLabel for str {
50    fn as_status_label(&self) -> String {
51        self.to_string()
52    }
53}
54
55#[allow(non_snake_case)]
56mod macro_impl {
57    use super::Observer;
58
59    macro_rules! impl_observer_tuple {
60        ($($t:tt)+) => {
61            impl<Out: ?Sized, $($t),+> Observer<Out> for ($($t),+,)
62            where
63                $($t: Observer<Out>),+
64            {
65                fn on_first_poll(&mut self) {
66                    let ($($t),+,) = self;
67                    $($t.on_first_poll();)+
68                }
69
70                fn on_poll(&mut self) {
71                    let ($($t),+,) = self;
72                    $($t.on_poll();)+
73                }
74
75                fn on_poll_ready(&mut self, output: &Out) {
76                    let ($($t),+,) = self;
77                    $($t.on_poll_ready(output);)+
78                }
79
80                fn on_finish(&mut self, output: Option<&Out>) {
81                    let ($($t),+,) = self;
82                    $($t.on_finish(output);)+
83                }
84
85                fn on_drop(&mut self) {
86                    let ($($t),+,) = self;
87                    $($t.on_drop();)+
88                }
89            }
90        }
91    }
92
93    impl_observer_tuple! { O1 }
94    impl_observer_tuple! { O1 O2 }
95    impl_observer_tuple! { O1 O2 O3 }
96    impl_observer_tuple! { O1 O2 O3 O4 }
97    impl_observer_tuple! { O1 O2 O3 O4 O5 }
98    impl_observer_tuple! { O1 O2 O3 O4 O5 O6 }
99    impl_observer_tuple! { O1 O2 O3 O4 O5 O6 O7 }
100    impl_observer_tuple! { O1 O2 O3 O4 O5 O6 O7 O8 }
101}