saluki_core/data_model/event/
mod.rs

1//! Telemetry events.
2
3use std::fmt;
4
5use bitmask_enum::bitmask;
6
7pub mod metric;
8use self::metric::Metric;
9
10pub mod eventd;
11use self::eventd::EventD;
12
13pub mod service_check;
14use self::service_check::ServiceCheck;
15
16pub mod log;
17use self::log::Log;
18
19pub mod trace;
20use self::trace::Trace;
21
22/// Telemetry event type.
23///
24/// This type is a bitmask, which means different event types can be combined together. This makes `EventType` mainly
25/// useful for defining the type of telemetry events that a component emits, or can handle.
26#[bitmask(u8)]
27#[bitmask_config(vec_debug)]
28pub enum EventType {
29    /// Metrics.
30    Metric,
31
32    /// Datadog Events.
33    EventD,
34
35    /// Service checks.
36    ServiceCheck,
37
38    /// Logs.
39    Log,
40
41    /// Traces.
42    Trace,
43}
44
45impl Default for EventType {
46    fn default() -> Self {
47        Self::none()
48    }
49}
50
51impl fmt::Display for EventType {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        let mut types = Vec::new();
54
55        if self.contains(Self::Metric) {
56            types.push("Metric");
57        }
58
59        if self.contains(Self::EventD) {
60            types.push("DatadogEvent");
61        }
62
63        if self.contains(Self::ServiceCheck) {
64            types.push("ServiceCheck");
65        }
66
67        if self.contains(Self::Log) {
68            types.push("Log");
69        }
70
71        if self.contains(Self::Trace) {
72            types.push("Trace");
73        }
74
75        write!(f, "{}", types.join("|"))
76    }
77}
78
79/// A telemetry event.
80#[derive(Clone, Debug, PartialEq)]
81pub enum Event {
82    /// A metric.
83    Metric(Metric),
84
85    /// A Datadog Event.
86    EventD(EventD),
87
88    /// A service check.
89    ServiceCheck(ServiceCheck),
90
91    /// A log.
92    Log(Log),
93
94    /// A trace.
95    Trace(Trace),
96}
97
98impl Event {
99    /// Gets the type of this event.
100    pub fn event_type(&self) -> EventType {
101        match self {
102            Event::Metric(_) => EventType::Metric,
103            Event::EventD(_) => EventType::EventD,
104            Event::ServiceCheck(_) => EventType::ServiceCheck,
105            Event::Log(_) => EventType::Log,
106            Event::Trace(_) => EventType::Trace,
107        }
108    }
109
110    /// Returns the inner event value, if this event is a `Metric`.
111    ///
112    /// Otherwise, `None` is returned and the original event is consumed.
113    pub fn try_into_metric(self) -> Option<Metric> {
114        match self {
115            Event::Metric(metric) => Some(metric),
116            _ => None,
117        }
118    }
119
120    /// Returns a reference inner event value, if this event is a `Metric`.
121    ///
122    /// Otherwise, `None` is returned.
123    pub fn try_as_metric(&self) -> Option<&Metric> {
124        match self {
125            Event::Metric(metric) => Some(metric),
126            _ => None,
127        }
128    }
129
130    /// Returns a mutable reference inner event value, if this event is a `Metric`.
131    ///
132    /// Otherwise, `None` is returned.
133    pub fn try_as_metric_mut(&mut self) -> Option<&mut Metric> {
134        match self {
135            Event::Metric(metric) => Some(metric),
136            _ => None,
137        }
138    }
139
140    /// Returns the inner event value, if this event is a `EventD`.
141    ///
142    /// Otherwise, `None` is returned and the original event is consumed.
143    pub fn try_into_eventd(self) -> Option<EventD> {
144        match self {
145            Event::EventD(eventd) => Some(eventd),
146            _ => None,
147        }
148    }
149
150    /// Returns the inner event value, if this event is a `ServiceCheck`.
151    ///
152    /// Otherwise, `None` is returned and the original event is consumed.
153    pub fn try_into_service_check(self) -> Option<ServiceCheck> {
154        match self {
155            Event::ServiceCheck(service_check) => Some(service_check),
156            _ => None,
157        }
158    }
159
160    #[allow(unused)]
161    /// Returns `true` if the event is a metric.
162    pub fn is_metric(&self) -> bool {
163        matches!(self, Event::Metric(_))
164    }
165
166    /// Returns `true` if the event is a eventd.
167    pub fn is_eventd(&self) -> bool {
168        matches!(self, Event::EventD(_))
169    }
170
171    /// Returns `true` if the event is a service check.
172    pub fn is_service_check(&self) -> bool {
173        matches!(self, Event::ServiceCheck(_))
174    }
175
176    /// Returns `true` if the event is a log.
177    pub fn is_log(&self) -> bool {
178        matches!(self, Event::Log(_))
179    }
180
181    /// Returns `true` if the event is a trace.
182    pub fn is_trace(&self) -> bool {
183        matches!(self, Event::Trace(_))
184    }
185}
186
187#[cfg(test)]
188mod tests {
189    use super::*;
190
191    #[test]
192    #[ignore = "only used to get struct sizes for core event data types"]
193    fn sizes() {
194        println!("Event: {} bytes", std::mem::size_of::<Event>());
195        println!("Metric: {} bytes", std::mem::size_of::<Metric>());
196        println!("  Context: {} bytes", std::mem::size_of::<saluki_context::Context>());
197        println!(
198            "  MetricValues: {} bytes",
199            std::mem::size_of::<super::metric::MetricValues>()
200        );
201        println!(
202            "  MetricMetadata: {} bytes",
203            std::mem::size_of::<super::metric::MetricMetadata>()
204        );
205        println!("EventD: {} bytes", std::mem::size_of::<EventD>());
206        println!("ServiceCheck: {} bytes", std::mem::size_of::<ServiceCheck>());
207        println!("Log: {} bytes", std::mem::size_of::<Log>());
208        println!("Trace: {} bytes", std::mem::size_of::<Trace>());
209    }
210}