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
use std::sync::atomic::{AtomicBool, Ordering};
use prometheus_client::{encoding::text::encode, registry::Registry};
#[cfg(feature = "bitswap")]
use crate::bitswap;
#[cfg(feature = "gateway")]
use crate::gateway;
#[cfg(feature = "p2p")]
use crate::p2p;
#[cfg(feature = "resolver")]
use crate::resolver;
#[cfg(feature = "store")]
use crate::store;
lazy_static! {
pub(crate) static ref CORE: Core = Core::default();
}
pub(crate) struct Core {
enabled: AtomicBool,
registry: Registry,
#[cfg(feature = "gateway")]
gateway_metrics: gateway::Metrics,
#[cfg(feature = "resolver")]
resolver_metrics: resolver::Metrics,
#[cfg(feature = "bitswap")]
bitswap_metrics: bitswap::Metrics,
#[cfg(feature = "store")]
store_metrics: store::Metrics,
#[cfg(feature = "p2p")]
libp2p_metrics: p2p::Libp2pMetrics,
#[cfg(feature = "p2p")]
p2p_metrics: p2p::Metrics,
}
impl Default for Core {
fn default() -> Self {
let mut reg = Registry::default();
Core {
enabled: AtomicBool::new(false),
#[cfg(feature = "gateway")]
gateway_metrics: gateway::Metrics::new(&mut reg),
#[cfg(feature = "resolver")]
resolver_metrics: resolver::Metrics::new(&mut reg),
#[cfg(feature = "bitswap")]
bitswap_metrics: bitswap::Metrics::new(&mut reg),
#[cfg(feature = "store")]
store_metrics: store::Metrics::new(&mut reg),
#[cfg(feature = "p2p")]
libp2p_metrics: p2p::Libp2pMetrics::new(&mut reg),
#[cfg(feature = "p2p")]
p2p_metrics: p2p::Metrics::new(&mut reg),
registry: reg,
}
}
}
impl Core {
pub(crate) fn registry(&self) -> &Registry {
&self.registry
}
#[cfg(feature = "gateway")]
pub(crate) fn gateway_metrics(&self) -> &gateway::Metrics {
&self.gateway_metrics
}
#[cfg(feature = "resolver")]
pub(crate) fn resolver_metrics(&self) -> &resolver::Metrics {
&self.resolver_metrics
}
#[cfg(feature = "bitswap")]
pub(crate) fn bitswap_metrics(&self) -> &bitswap::Metrics {
&self.bitswap_metrics
}
#[cfg(feature = "store")]
pub(crate) fn store_metrics(&self) -> &store::Metrics {
&self.store_metrics
}
#[cfg(feature = "p2p")]
pub(crate) fn libp2p_metrics(&self) -> &p2p::Libp2pMetrics {
&self.libp2p_metrics
}
#[cfg(feature = "p2p")]
pub(crate) fn p2p_metrics(&self) -> &p2p::Metrics {
&self.p2p_metrics
}
pub(crate) fn encode(&self) -> Vec<u8> {
let mut buf = vec![];
encode(&mut buf, self.registry()).unwrap();
buf
}
pub(crate) fn set_enabled(&self, enabled: bool) {
self.enabled.swap(enabled, Ordering::Relaxed);
}
pub(crate) fn enabled(&self) -> bool {
self.enabled.load(Ordering::Relaxed)
}
}
pub trait MetricType {
fn name(&self) -> &'static str;
}
pub trait HistogramType {
fn name(&self) -> &'static str;
}
pub trait MetricsRecorder {
fn record<M>(&self, m: M, value: u64)
where
M: MetricType + std::fmt::Display;
fn observe<M>(&self, m: M, value: f64)
where
M: HistogramType + std::fmt::Display;
}
pub trait MRecorder {
fn record(&self, value: u64);
}
pub trait MObserver {
fn observe(&self, value: f64);
}