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
mod future_ext;
#[cfg(feature = "prometheus")]
pub mod prometheus;

pub use future_ext::MetricsFutureExt;

pub trait ScopedObserver {
    fn start(&self);
    fn stop(&self);
}

impl<O1> ScopedObserver for (O1,)
where
    O1: ScopedObserver,
{
    fn start(&self) {
        self.0.start();
    }

    fn stop(&self) {
        self.0.stop();
    }
}

impl<O1, O2> ScopedObserver for (O1, O2)
where
    O1: ScopedObserver,
    O2: ScopedObserver,
{
    fn start(&self) {
        self.0.start();
        self.1.start();
    }

    fn stop(&self) {
        self.1.stop();
        self.0.stop();
    }
}

impl<O1, O2, O3> ScopedObserver for (O1, O2, O3)
where
    O1: ScopedObserver,
    O2: ScopedObserver,
    O3: ScopedObserver,
{
    fn start(&self) {
        self.0.start();
        self.1.start();
        self.2.start();
    }

    fn stop(&self) {
        self.2.stop();
        self.1.stop();
        self.0.stop();
    }
}

pub trait AsStatusLabel {
    fn as_status_label(&self) -> String;
}

pub trait StatusObserver {
    fn record<Output: AsStatusLabel>(&self, output: &Output);
}

impl<T, E: AsStatusLabel> AsStatusLabel for Result<T, E> {
    fn as_status_label(&self) -> String {
        match self {
            Ok(_) => "ok".to_owned(),
            Err(err) => err.as_status_label(),
        }
    }
}