|
| 1 | +use crate::util::typed_prometheus::metrics_group; |
1 | 2 | use once_cell::sync::Lazy; |
2 | | -use prometheus::core::{Metric, MetricVec, MetricVecBuilder}; |
3 | 3 | use prometheus::{Gauge, GaugeVec, HistogramVec, IntCounterVec, IntGauge, IntGaugeVec}; |
4 | 4 | use spacetimedb_lib::{Address, Hash, Identity}; |
5 | 5 |
|
6 | | -#[macro_export] |
7 | | -macro_rules! metrics_group { |
8 | | - ($(#[$attr:meta])* $type_vis:vis struct $type_name:ident { |
9 | | - $(#[name = $name:ident] #[help = $help:expr] $(#[labels($($labels:ident: $labelty:ty),*)])? $vis:vis $field:ident: $ty:ident,)* |
10 | | - }) => { |
11 | | - $(#[$attr])* |
12 | | - $type_vis struct $type_name { |
13 | | - $($vis $field: $crate::metrics_group!(@fieldtype $field $ty $(($($labels)*))?),)* |
14 | | - } |
15 | | - $($crate::metrics_group!(@maketype $vis $field $ty $(($($labels: $labelty),*))?);)* |
16 | | - impl $type_name { |
17 | | - pub fn new() -> Self { |
18 | | - Self { |
19 | | - $($field: $crate::make_collector!($crate::metrics_group!(@fieldtype $field $ty $(($($labels)*))?), stringify!($name), $help),)* |
20 | | - } |
21 | | - } |
22 | | - } |
23 | | - |
24 | | - impl prometheus::core::Collector for $type_name { |
25 | | - fn desc(&self) -> Vec<&prometheus::core::Desc> { |
26 | | - $crate::worker_metrics::itertools::concat([ $(prometheus::core::Collector::desc(&self.$field)),* ]) |
27 | | - } |
28 | | - |
29 | | - fn collect(&self) -> Vec<prometheus::proto::MetricFamily> { |
30 | | - $crate::worker_metrics::itertools::concat([ $(prometheus::core::Collector::collect(&self.$field)),* ]) |
31 | | - } |
32 | | - } |
33 | | - impl prometheus::core::Collector for &$type_name { |
34 | | - fn desc(&self) -> Vec<&prometheus::core::Desc> { |
35 | | - (**self).desc() |
36 | | - } |
37 | | - |
38 | | - fn collect(&self) -> Vec<prometheus::proto::MetricFamily> { |
39 | | - (**self).collect() |
40 | | - } |
41 | | - } |
42 | | - }; |
43 | | - (@fieldtype $field:ident $ty:ident ($($labels:tt)*)) => { $crate::worker_metrics::paste! { [< $field:camel $ty >] } }; |
44 | | - (@fieldtype $field:ident $ty:ident) => { $ty }; |
45 | | - (@maketype $vis:vis $field:ident $ty:ident ($($labels:tt)*)) => { |
46 | | - $crate::worker_metrics::paste! { |
47 | | - $crate::metrics_vec!($vis [< $field:camel $ty >]: $ty($($labels)*)); |
48 | | - } |
49 | | - }; |
50 | | - (@maketype $vis:vis $field:ident $ty:ident) => {}; |
51 | | -} |
52 | | -pub use metrics_group; |
53 | | -#[doc(hidden)] |
54 | | -pub use {itertools, paste::paste}; |
55 | | - |
56 | 6 | metrics_group!( |
57 | 7 | pub struct WorkerMetrics { |
58 | 8 | #[name = spacetime_worker_connected_clients] |
@@ -135,76 +85,3 @@ metrics_group!( |
135 | 85 | ); |
136 | 86 |
|
137 | 87 | pub static WORKER_METRICS: Lazy<WorkerMetrics> = Lazy::new(WorkerMetrics::new); |
138 | | - |
139 | | -#[macro_export] |
140 | | -macro_rules! make_collector { |
141 | | - ($ty:ty, $name:expr, $help:expr $(,)?) => { |
142 | | - <$ty>::with_opts(prometheus::Opts::new($name, $help).into()).unwrap() |
143 | | - }; |
144 | | - ($ty:ty, $name:expr, $help:expr, $labels:expr $(,)?) => { |
145 | | - <$ty>::new(prometheus::Opts::new($name, $help).into(), $labels).unwrap() |
146 | | - }; |
147 | | -} |
148 | | -pub use make_collector; |
149 | | - |
150 | | -#[macro_export] |
151 | | -macro_rules! metrics_vec { |
152 | | - ($vis:vis $name:ident: $vecty:ident($($labels:ident: $labelty:ty),+ $(,)?)) => { |
153 | | - #[derive(Clone)] |
154 | | - $vis struct $name($vecty); |
155 | | - impl $name { |
156 | | - pub fn with_opts(opts: prometheus::Opts) -> prometheus::Result<Self> { |
157 | | - $vecty::new(opts.into(), &[$(stringify!($labels)),+]).map(Self) |
158 | | - } |
159 | | - |
160 | | - pub fn with_label_values(&self, $($labels: &$labelty),+) -> <$vecty as $crate::worker_metrics::ExtractMetricVecT>::M { |
161 | | - use $crate::worker_metrics::AsPrometheusLabel as _; |
162 | | - self.0.with_label_values(&[ $($labels.as_prometheus_str().as_ref()),+ ]) |
163 | | - } |
164 | | - } |
165 | | - |
166 | | - impl prometheus::core::Collector for $name { |
167 | | - fn desc(&self) -> Vec<&prometheus::core::Desc> { |
168 | | - prometheus::core::Collector::desc(&self.0) |
169 | | - } |
170 | | - |
171 | | - fn collect(&self) -> Vec<prometheus::proto::MetricFamily> { |
172 | | - prometheus::core::Collector::collect(&self.0) |
173 | | - } |
174 | | - } |
175 | | - }; |
176 | | -} |
177 | | -pub use metrics_vec; |
178 | | - |
179 | | -pub trait AsPrometheusLabel { |
180 | | - type Str<'a>: AsRef<str> + 'a |
181 | | - where |
182 | | - Self: 'a; |
183 | | - fn as_prometheus_str(&self) -> Self::Str<'_>; |
184 | | -} |
185 | | -impl<T: AsRef<str> + ?Sized> AsPrometheusLabel for &T { |
186 | | - type Str<'a> = &'a str where Self: 'a; |
187 | | - fn as_prometheus_str(&self) -> Self::Str<'_> { |
188 | | - self.as_ref() |
189 | | - } |
190 | | -} |
191 | | -macro_rules! impl_prometheusvalue_string { |
192 | | - ($($x:ty),*) => { |
193 | | - $(impl AsPrometheusLabel for $x { |
194 | | - type Str<'a> = String; |
195 | | - fn as_prometheus_str(&self) -> Self::Str<'_> { |
196 | | - self.to_string() |
197 | | - } |
198 | | - })* |
199 | | - } |
200 | | -} |
201 | | -impl_prometheusvalue_string!(Hash, Identity, Address, u8, u16, u32, u64, i8, i16, i32, i64); |
202 | | - |
203 | | -#[doc(hidden)] |
204 | | -pub trait ExtractMetricVecT { |
205 | | - type M: Metric; |
206 | | -} |
207 | | - |
208 | | -impl<T: MetricVecBuilder> ExtractMetricVecT for MetricVec<T> { |
209 | | - type M = T::M; |
210 | | -} |
0 commit comments