metrics_prometheus/failure.rs
1//! Definitions for dealing with a [`prometheus::Error`].
2
3#[doc(inline)]
4pub use self::strategy::Strategy;
5
6/// Possible actions on an encountered [`prometheus::Error`] inside
7/// [`metrics::Recorder`] methods.
8#[derive(Clone, Copy, Debug)]
9pub enum Action {
10 /// Return a no-op metric implementation (see [`metrics::Counter::noop()`]
11 /// for example).
12 NoOp,
13
14 /// Panic with the encountered [`prometheus::Error`].
15 Panic,
16}
17
18/// Strategies for dealing with a [`prometheus::Error`].
19pub mod strategy {
20 use super::Action;
21
22 /// Strategy deciding which [`Action`] should be performed on an encountered
23 /// [`prometheus::Error`] inside [`metrics::Recorder`] methods.
24 pub trait Strategy {
25 /// Inspects the encountered [`prometheus::Error`] and returns the
26 /// [`Action`] to be performed.
27 fn decide(&self, res: &prometheus::Error) -> Action;
28 }
29
30 /// [`Strategy`] returning always [`Action::NoOp`].
31 #[derive(Clone, Copy, Debug, Default)]
32 pub struct NoOp;
33
34 impl Strategy for NoOp {
35 fn decide(&self, _: &prometheus::Error) -> Action {
36 Action::NoOp
37 }
38 }
39
40 /// [`Strategy`] returning always [`Action::Panic`].
41 #[derive(Clone, Copy, Debug, Default)]
42 pub struct Panic;
43
44 impl Strategy for Panic {
45 fn decide(&self, _: &prometheus::Error) -> Action {
46 Action::Panic
47 }
48 }
49
50 /// [`Strategy`] returning an [`Action::Panic`] in debug mode, and
51 /// [`Action::NoOp`] in release mode.
52 #[derive(Clone, Copy, Debug, Default)]
53 pub struct PanicInDebugNoOpInRelease;
54
55 impl Strategy for PanicInDebugNoOpInRelease {
56 fn decide(&self, _: &prometheus::Error) -> Action {
57 #[cfg(debug_assertions)]
58 {
59 Action::Panic
60 }
61 #[cfg(not(debug_assertions))]
62 {
63 Action::NoOp
64 }
65 }
66 }
67}