Skip to content

Commit efe869f

Browse files
committed
Move macro to its own file
1 parent dcdd2fb commit efe869f

File tree

4 files changed

+129
-125
lines changed

4 files changed

+129
-125
lines changed

crates/core/src/db/db_metrics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::worker_metrics::metrics_group;
1+
use crate::util::typed_prometheus::metrics_group;
22
use once_cell::sync::Lazy;
33
use prometheus::{Histogram, HistogramVec};
44

crates/core/src/util/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub mod prometheus_handle;
66
mod future_queue;
77
pub mod lending_pool;
88
pub mod notify_once;
9+
pub mod typed_prometheus;
910

1011
pub use future_queue::{future_queue, FutureQueue};
1112

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
use prometheus::core::{Metric, MetricVec, MetricVecBuilder};
2+
use spacetimedb_lib::{Address, Hash, Identity};
3+
4+
#[macro_export]
5+
macro_rules! metrics_group {
6+
($(#[$attr:meta])* $type_vis:vis struct $type_name:ident {
7+
$(#[name = $name:ident] #[help = $help:expr] $(#[labels($($labels:ident: $labelty:ty),*)])? $vis:vis $field:ident: $ty:ident,)*
8+
}) => {
9+
$(#[$attr])*
10+
$type_vis struct $type_name {
11+
$($vis $field: $crate::metrics_group!(@fieldtype $field $ty $(($($labels)*))?),)*
12+
}
13+
$($crate::metrics_group!(@maketype $vis $field $ty $(($($labels: $labelty),*))?);)*
14+
impl $type_name {
15+
#[allow(clippy::new_without_default)]
16+
pub fn new() -> Self {
17+
Self {
18+
$($field: $crate::make_collector!($crate::metrics_group!(@fieldtype $field $ty $(($($labels)*))?), stringify!($name), $help),)*
19+
}
20+
}
21+
}
22+
23+
impl prometheus::core::Collector for $type_name {
24+
fn desc(&self) -> Vec<&prometheus::core::Desc> {
25+
$crate::util::typed_prometheus::itertools::concat([ $(prometheus::core::Collector::desc(&self.$field)),* ])
26+
}
27+
28+
fn collect(&self) -> Vec<prometheus::proto::MetricFamily> {
29+
$crate::util::typed_prometheus::itertools::concat([ $(prometheus::core::Collector::collect(&self.$field)),* ])
30+
}
31+
}
32+
impl prometheus::core::Collector for &$type_name {
33+
fn desc(&self) -> Vec<&prometheus::core::Desc> {
34+
(**self).desc()
35+
}
36+
37+
fn collect(&self) -> Vec<prometheus::proto::MetricFamily> {
38+
(**self).collect()
39+
}
40+
}
41+
};
42+
(@fieldtype $field:ident $ty:ident ($($labels:tt)*)) => { $crate::util::typed_prometheus::paste! { [< $field:camel $ty >] } };
43+
(@fieldtype $field:ident $ty:ident) => { $ty };
44+
(@maketype $vis:vis $field:ident $ty:ident ($($labels:tt)*)) => {
45+
$crate::util::typed_prometheus::paste! {
46+
$crate::metrics_vec!($vis [< $field:camel $ty >]: $ty($($labels)*));
47+
}
48+
};
49+
(@maketype $vis:vis $field:ident $ty:ident) => {};
50+
}
51+
pub use metrics_group;
52+
#[doc(hidden)]
53+
pub use {itertools, paste::paste};
54+
55+
#[macro_export]
56+
macro_rules! make_collector {
57+
($ty:ty, $name:expr, $help:expr $(,)?) => {
58+
<$ty>::with_opts(prometheus::Opts::new($name, $help).into()).unwrap()
59+
};
60+
($ty:ty, $name:expr, $help:expr, $labels:expr $(,)?) => {
61+
<$ty>::new(prometheus::Opts::new($name, $help).into(), $labels).unwrap()
62+
};
63+
}
64+
pub use make_collector;
65+
66+
#[macro_export]
67+
macro_rules! metrics_vec {
68+
($vis:vis $name:ident: $vecty:ident($($labels:ident: $labelty:ty),+ $(,)?)) => {
69+
#[derive(Clone)]
70+
$vis struct $name($vecty);
71+
impl $name {
72+
pub fn with_opts(opts: prometheus::Opts) -> prometheus::Result<Self> {
73+
$vecty::new(opts.into(), &[$(stringify!($labels)),+]).map(Self)
74+
}
75+
76+
pub fn with_label_values(&self, $($labels: &$labelty),+) -> <$vecty as $crate::util::typed_prometheus::ExtractMetricVecT>::M {
77+
use $crate::util::typed_prometheus::AsPrometheusLabel as _;
78+
self.0.with_label_values(&[ $($labels.as_prometheus_str().as_ref()),+ ])
79+
}
80+
}
81+
82+
impl prometheus::core::Collector for $name {
83+
fn desc(&self) -> Vec<&prometheus::core::Desc> {
84+
prometheus::core::Collector::desc(&self.0)
85+
}
86+
87+
fn collect(&self) -> Vec<prometheus::proto::MetricFamily> {
88+
prometheus::core::Collector::collect(&self.0)
89+
}
90+
}
91+
};
92+
}
93+
pub use metrics_vec;
94+
95+
pub trait AsPrometheusLabel {
96+
type Str<'a>: AsRef<str> + 'a
97+
where
98+
Self: 'a;
99+
fn as_prometheus_str(&self) -> Self::Str<'_>;
100+
}
101+
impl<T: AsRef<str> + ?Sized> AsPrometheusLabel for &T {
102+
type Str<'a> = &'a str where Self: 'a;
103+
fn as_prometheus_str(&self) -> Self::Str<'_> {
104+
self.as_ref()
105+
}
106+
}
107+
macro_rules! impl_prometheusvalue_string {
108+
($($x:ty),*) => {
109+
$(impl AsPrometheusLabel for $x {
110+
type Str<'a> = String;
111+
fn as_prometheus_str(&self) -> Self::Str<'_> {
112+
self.to_string()
113+
}
114+
})*
115+
}
116+
}
117+
impl_prometheusvalue_string!(Hash, Identity, Address, u8, u16, u32, u64, i8, i16, i32, i64);
118+
119+
#[doc(hidden)]
120+
pub trait ExtractMetricVecT {
121+
type M: Metric;
122+
}
123+
124+
impl<T: MetricVecBuilder> ExtractMetricVecT for MetricVec<T> {
125+
type M = T::M;
126+
}
Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,8 @@
1+
use crate::util::typed_prometheus::metrics_group;
12
use once_cell::sync::Lazy;
2-
use prometheus::core::{Metric, MetricVec, MetricVecBuilder};
33
use prometheus::{Gauge, GaugeVec, HistogramVec, IntCounterVec, IntGauge, IntGaugeVec};
44
use spacetimedb_lib::{Address, Hash, Identity};
55

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-
566
metrics_group!(
577
pub struct WorkerMetrics {
588
#[name = spacetime_worker_connected_clients]
@@ -135,76 +85,3 @@ metrics_group!(
13585
);
13686

13787
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

Comments
 (0)