srad_eon/metric_manager/manager.rs
1use async_trait::async_trait;
2
3use crate::{birth::BirthInitializer, device::DeviceHandle, metric::MessageMetrics, NodeHandle};
4
5/// A type alias for a trait object implementing [`NodeMetricManager`].
6pub type DynNodeMetricManager = dyn NodeMetricManager + Send + Sync + 'static;
7/// A type alias for a trait object implementing [`DeviceMetricManager`].
8pub type DynDeviceMetricManager = dyn DeviceMetricManager + Send + Sync + 'static;
9
10/// A trait for implementing a type that defines a collection of metrics.
11///
12/// This trait acts as an interface the library uses to query a type about
13/// what metrics it has so that birth messages can be created.
14///
15/// # Examples
16///
17/// ```
18/// use srad_eon::{MetricManager, BirthInitializer, BirthMetricDetails};
19///
20/// struct MyMetricManager {
21/// counter: i32
22/// };
23///
24/// impl MetricManager for MyMetricManager {
25/// fn initialise_birth(&self, bi: &mut BirthInitializer) {
26/// // Register metrics
27/// bi.register_metric(BirthMetricDetails::new_with_initial_value("my_counter", self.counter).use_alias(true));
28/// }
29/// }
30pub trait MetricManager {
31 /// Initialises the set of metrics to be included in a birth or rebirth message.
32 ///
33 /// This method is called whenever the library needs to generate a birth message payload for the node or device
34 /// that uses the type which implements MetricManager
35 fn initialise_birth(&self, bi: &mut BirthInitializer);
36}
37
38///A trait for implementing a type that defines node-specific metrics
39#[async_trait]
40pub trait NodeMetricManager: MetricManager {
41 /// Initialise the struct.
42 ///
43 /// Called when the Node is created
44 fn init(&self, _handle: &NodeHandle) {}
45
46 /// Processes NCMD metrics.
47 ///
48 /// This async method is called when a NCMD message for the node is received, allowing
49 /// the implementation to handle command metrics as it sees fit. The default implementation does nothing.
50 async fn on_ncmd(&self, _node: NodeHandle, _metrics: MessageMetrics) {}
51}
52
53///A trait for implementing a type that defines device-specific metrics
54#[async_trait]
55pub trait DeviceMetricManager: MetricManager {
56 /// Initialise the struct.
57 ///
58 /// Called when the Device is created
59 fn init(&self, _handle: &DeviceHandle) {}
60
61 /// Processes DCMD metrics.
62 ///
63 /// This async method is called when a DCMD message for the device is received, allowing
64 /// the implementation to handle command metrics as it sees fit. The default implementation does nothing.
65 async fn on_dcmd(&self, _device: DeviceHandle, _metrics: MessageMetrics) {}
66}
67
68/// A no-op implementation [MetricManager] which will provide no metrics on Birth
69/// and will do nothing when a CMD message is received.
70pub struct NoMetricManager {}
71
72impl NoMetricManager {
73 /// Creates a new instance of `NoMetricManager`.
74 pub fn new() -> Self {
75 Self {}
76 }
77}
78
79impl Default for NoMetricManager {
80 fn default() -> Self {
81 Self::new()
82 }
83}
84
85impl MetricManager for NoMetricManager {
86 fn initialise_birth(&self, _: &mut BirthInitializer) {}
87}
88impl NodeMetricManager for NoMetricManager {}
89impl DeviceMetricManager for NoMetricManager {}