1pub(crate) mod registry;
4pub(crate) mod http;
5
6#[cfg(not(feature = "http"))]
7pub fn init<K>()
8where
9 K: Eq + std::hash::Hash + Clone + Send + Sync + 'static,
10{
11 let registry: std::sync::Arc<crate::registry::core::CounterRegistry<K>> = std::sync::Arc::new(crate::registry::core::CounterRegistry::<K>::new());
12
13 registry.clone().spawn_per_second_congregator();
14
15 registry.clone().spawn_per_key_metrics_congregator();
16
17 registry.clone().spawn_total_metrics_congregator();
18
19 crate::registry::auxiliary::REGISTRIES.insert(std::any::TypeId::of::<K>(), Box::new(registry));
20}
21
22#[cfg(feature = "http")]
23pub fn init<K>(address: &str, port: &str, path: &str, workers: &str)
24where
25 K: Eq + std::hash::Hash + Clone + Send + Sync + 'static + serde::Serialize,
26{
27 let registry: std::sync::Arc<crate::registry::core::CounterRegistry<K>> = std::sync::Arc::new(crate::registry::core::CounterRegistry::<K>::new());
28
29 registry.clone().spawn_per_second_congregator();
30
31 registry.clone().spawn_per_key_metrics_congregator();
32
33 registry.clone().spawn_total_metrics_congregator();
34
35 crate::registry::auxiliary::REGISTRIES.insert(std::any::TypeId::of::<K>(), Box::new(registry));
36
37 let listen_address: String = address.to_string();
38 let num_of_workers: String = workers.to_string();
39 let listen_port: String = port.to_string();
40 let url_path: String = path.to_string();
41
42 {
43 match std::thread::Builder::new()
44 .name("metrics-http".into())
45 .spawn(|| {
46 let system = actix_web::rt::System::new();
47 system.block_on(async {
48 match crate::http::serve::<K>(listen_address, listen_port, url_path, num_of_workers).await {
49 Ok(_ok) => {},
50 Err(error) => {
51 log::error!("{:?}", error);
52 std::process::exit(1);
53 }
54 }
55 });
56 })
57 {
58 Ok(_ok) => {},
59 Err(error) => {
60 log::error!("{:?}", error);
61 std::process::exit(1);
62 }
63 }
64 }
65}
66
67pub fn increment<K>(key: K)
68where
69 K: Eq + std::hash::Hash + Clone + Send + Sync + 'static,
70{
71 let registry: Result<std::sync::Arc<registry::core::CounterRegistry<K>>, registry::auxiliary::CounterRegistryError> = crate::registry::auxiliary::get_registry::<K>();
72 match registry {
73 Ok(registry) => {
74 registry.increment(key);
75 },
76 Err(error) => {
77 log::error!("increment failed: Was not able to add to count. Error: {:?}", error);
78 }
79 }
80}
81
82pub fn fetch<K>() -> crate::registry::auxiliary::MetricsSnapshot<K>
83where
84 K: Eq + std::hash::Hash + Clone + Send + Sync + 'static,
85{
86 let data: registry::auxiliary::MetricsSnapshot<K> = {
87 match crate::registry::auxiliary::MetricsSnapshot::<K>::fetch() {
88 Ok(snap) => {
89 snap
90 },
91 Err(error) => {
92 log::error!("fetch failed: Will return an empty value. Error: {:?}", error);
93
94 let empty: registry::auxiliary::MetricsSnapshot<K> = crate::registry::auxiliary::MetricsSnapshot {
95 per_key: std::collections::HashMap::new(),
96 total: (0, 0),
97 };
98
99 return empty;
100 }
101 }
102 };
103
104 return data;
105}