falco_plugin/plugin/base/
metrics.rs

1use falco_plugin_api::{
2    ss_plugin_metric, ss_plugin_metric_type, ss_plugin_metric_type_SS_PLUGIN_METRIC_TYPE_MONOTONIC,
3    ss_plugin_metric_type_SS_PLUGIN_METRIC_TYPE_NON_MONOTONIC, ss_plugin_metric_value,
4    ss_plugin_metric_value_type, ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_D,
5    ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_F,
6    ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_I,
7    ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_S32,
8    ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_S64,
9    ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_U32,
10    ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_U64,
11};
12use std::ffi::CStr;
13
14#[derive(Debug, Copy, Clone, Eq, PartialEq)]
15#[allow(missing_docs)]
16pub enum MetricType {
17    Monotonic,
18    NonMonotonic,
19}
20
21impl MetricType {
22    fn as_raw(&self) -> ss_plugin_metric_type {
23        match self {
24            Self::Monotonic => ss_plugin_metric_type_SS_PLUGIN_METRIC_TYPE_MONOTONIC,
25            Self::NonMonotonic => ss_plugin_metric_type_SS_PLUGIN_METRIC_TYPE_NON_MONOTONIC,
26        }
27    }
28}
29
30#[derive(Debug, Copy, Clone, PartialEq)]
31#[allow(missing_docs)]
32pub enum MetricValue {
33    U32(u32),
34    S32(i32),
35    U64(u64),
36    I64(i64),
37    Double(f64),
38    Float(f32),
39    Int(i32),
40}
41
42impl MetricValue {
43    fn as_raw(&self) -> (ss_plugin_metric_value_type, ss_plugin_metric_value) {
44        match self {
45            MetricValue::U32(v) => (
46                ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_U32,
47                ss_plugin_metric_value { u32_: *v },
48            ),
49            MetricValue::S32(v) => (
50                ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_S32,
51                ss_plugin_metric_value { s32: *v },
52            ),
53            MetricValue::U64(v) => (
54                ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_U64,
55                ss_plugin_metric_value { u64_: *v },
56            ),
57            MetricValue::I64(v) => (
58                ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_S64,
59                ss_plugin_metric_value { s64: *v },
60            ),
61            MetricValue::Double(v) => (
62                ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_D,
63                ss_plugin_metric_value { d: *v },
64            ),
65            MetricValue::Float(v) => (
66                ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_F,
67                ss_plugin_metric_value { f: *v },
68            ),
69            MetricValue::Int(v) => (
70                ss_plugin_metric_value_type_SS_PLUGIN_METRIC_VALUE_TYPE_I,
71                ss_plugin_metric_value { i: *v },
72            ),
73        }
74    }
75}
76
77/// A descriptor for a metric
78///
79/// It contains the metric name and the type (monotonic/non-monotonic) but does not
80/// contain a specific value
81#[derive(Debug, Clone)]
82pub struct MetricLabel {
83    name: &'static CStr,
84    metric_type: MetricType,
85}
86
87impl MetricLabel {
88    /// Create a new metric label
89    pub fn new(name: &'static CStr, metric_type: MetricType) -> Self {
90        Self { name, metric_type }
91    }
92
93    /// Create a [`Metric`], assigning a specific value to a label
94    pub fn with_value(&self, value: MetricValue) -> Metric {
95        Metric {
96            label: self.clone(),
97            value,
98        }
99    }
100}
101
102/// A metric with a value
103///
104/// This is what gets emitted to the Falco Plugin API (after a conversion to the required format)
105#[derive(Debug)]
106pub struct Metric {
107    label: MetricLabel,
108    value: MetricValue,
109}
110
111impl Metric {
112    /// Create a new metric, combining a label with a corresponding value
113    pub fn new(label: MetricLabel, value: MetricValue) -> Self {
114        Self { label, value }
115    }
116
117    pub(crate) fn as_raw(&self) -> ss_plugin_metric {
118        let (value_type, value) = self.value.as_raw();
119        let metric_type = self.label.metric_type.as_raw();
120
121        ss_plugin_metric {
122            name: self.label.name.as_ptr(),
123            type_: metric_type,
124            value_type,
125            value,
126        }
127    }
128}