khora_telemetry/monitoring/registry.rs
1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Registry for managing resource monitors.
16
17use khora_core::telemetry::ResourceMonitor;
18use std::sync::{Arc, Mutex};
19
20/// A thread-safe registry for resource monitors.
21#[derive(Debug, Clone)]
22pub struct MonitorRegistry {
23 monitors: Arc<Mutex<Vec<Arc<dyn ResourceMonitor>>>>,
24}
25
26impl MonitorRegistry {
27 /// Creates a new, empty monitor registry.
28 pub fn new() -> Self {
29 Self {
30 monitors: Arc::new(Mutex::new(Vec::new())),
31 }
32 }
33
34 /// Registers a new resource monitor.
35 pub fn register(&self, monitor: Arc<dyn ResourceMonitor>) {
36 let mut monitors_guard = self.monitors.lock().unwrap();
37 let monitor_id = monitor.monitor_id().to_string();
38 monitors_guard.push(monitor);
39 log::info!("Registered resource monitor: {}", monitor_id);
40 }
41
42 /// Calls the `update` method on all registered monitors.
43 pub fn update_all(&self) {
44 let monitors_guard = self.monitors.lock().unwrap();
45 for monitor in monitors_guard.iter() {
46 monitor.update();
47 }
48 }
49
50 /// Returns a clone of all registered monitors.
51 pub fn get_all_monitors(&self) -> Vec<Arc<dyn ResourceMonitor>> {
52 self.monitors.lock().unwrap().clone()
53 }
54}
55
56impl Default for MonitorRegistry {
57 fn default() -> Self {
58 Self::new()
59 }
60}