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
macro_rules! register_collectors {
(
$(
$module:ident => $collector_type:ident
),* $(,)?
) => {
// Import all collector modules
$(
pub mod $module;
pub use $module::$collector_type;
)*
// Generate the enum with all collector types
#[derive(Clone)]
pub enum CollectorType {
$(
$collector_type($collector_type),
)*
}
// Implement Collector trait for CollectorType enum
impl Collector for CollectorType {
fn name(&self) -> &'static str {
match self {
$(
CollectorType::$collector_type(c) => c.name(),
)*
}
}
async fn collect(&self, pool: &PgPool) -> Result<String> {
match self {
$(
CollectorType::$collector_type(c) => c.collect(pool).await,
)*
}
}
fn enabled_by_default(&self) -> bool {
match self {
$(
CollectorType::$collector_type(c) => c.enabled_by_default(),
)*
}
}
}
// Generate the factory function map
pub fn all_factories() -> HashMap<&'static str, fn() -> CollectorType> {
let mut map: HashMap<&'static str, fn() -> CollectorType> = HashMap::new();
$(
map.insert(
stringify!($module),
|| CollectorType::$collector_type($collector_type::new()),
);
)*
map
}
// Generate array of collector names - this is what you need for clap!
pub const COLLECTOR_NAMES: &[&'static str] = &[
$(stringify!($module),)*
];
};
}