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