srad_eon/metric_manager/
manager.rs

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