saluki_core/data_model/event/eventd/
mod.rs

1//! Events.
2
3use std::{fmt, num::NonZeroU64};
4
5use serde::{Serialize, Serializer};
6use stringtheory::MetaString;
7
8use crate::data_model::tags::TagSet;
9
10/// Value supplied used to specify a low priority event
11pub const PRIORITY_LOW: &str = "low";
12
13/// Value used to specify an error alert.
14pub const ALERT_TYPE_ERROR: &str = "error";
15
16/// Value used to specify a warning alert.
17pub const ALERT_TYPE_WARNING: &str = "warning";
18
19/// Value used to specify a success alert.
20pub const ALERT_TYPE_SUCCESS: &str = "success";
21
22/// Alert type.
23#[derive(Clone, Copy, Debug, PartialEq, Eq)]
24pub enum AlertType {
25    /// Indicates an informational event.
26    Info,
27
28    /// Indicates an error event.
29    Error,
30
31    /// Indicates a warning event.
32    Warning,
33
34    /// Indicates a successful event.
35    Success,
36}
37
38impl fmt::Display for AlertType {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        f.write_str(self.as_str())
41    }
42}
43
44impl Serialize for AlertType {
45    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
46    where
47        S: Serializer,
48    {
49        serializer.serialize_str(self.as_str())
50    }
51}
52
53impl AlertType {
54    /// Creates an AlertType from a string.
55    ///
56    /// Defaults to an informational alert.
57    pub fn try_from_string(alert_type: &str) -> Option<Self> {
58        match alert_type {
59            ALERT_TYPE_ERROR => Some(AlertType::Error),
60            ALERT_TYPE_WARNING => Some(AlertType::Warning),
61            ALERT_TYPE_SUCCESS => Some(AlertType::Success),
62            _ => Some(AlertType::Info),
63        }
64    }
65
66    /// Returns the string representation of the alert type.
67    pub const fn as_str(self) -> &'static str {
68        match self {
69            AlertType::Info => "info",
70            AlertType::Error => "error",
71            AlertType::Warning => "warning",
72            AlertType::Success => "success",
73        }
74    }
75}
76
77/// Event priority.
78#[derive(Clone, Copy, Debug, PartialEq, Eq)]
79pub enum Priority {
80    /// The event has normal priority.
81    Normal,
82
83    /// The event has low priority.
84    Low,
85}
86
87impl fmt::Display for Priority {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        f.write_str(self.as_str())
90    }
91}
92
93impl Serialize for Priority {
94    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
95    where
96        S: Serializer,
97    {
98        serializer.serialize_str(self.as_str())
99    }
100}
101
102impl Priority {
103    /// Creates an event Priority from a string.
104    ///
105    /// Defaults to  normal priority.
106    pub fn try_from_string(priority: &str) -> Option<Self> {
107        match priority {
108            PRIORITY_LOW => Some(Priority::Low),
109            _ => Some(Priority::Normal),
110        }
111    }
112
113    /// Returns the string representation of the priority.
114    pub const fn as_str(self) -> &'static str {
115        match self {
116            Priority::Normal => "normal",
117            Priority::Low => "low",
118        }
119    }
120}
121
122/// EventD is an object that can be posted to the Datadog event stream.
123#[derive(Clone, Debug, PartialEq, Serialize)]
124pub struct EventD {
125    title: MetaString,
126    text: MetaString,
127    timestamp: Option<NonZeroU64>,
128    #[serde(skip_serializing_if = "MetaString::is_empty")]
129    hostname: MetaString,
130    #[serde(skip_serializing_if = "MetaString::is_empty")]
131    aggregation_key: MetaString,
132    priority: Option<Priority>,
133    #[serde(skip_serializing_if = "MetaString::is_empty")]
134    source_type_name: MetaString,
135    alert_type: Option<AlertType>,
136    tags: TagSet,
137    origin_tags: TagSet,
138}
139
140impl EventD {
141    /// Returns the title of the event.
142    pub fn title(&self) -> &str {
143        &self.title
144    }
145
146    /// Returns the text of the event.
147    pub fn text(&self) -> &str {
148        &self.text
149    }
150
151    /// Returns the host where the event originated from.
152    pub fn hostname(&self) -> Option<&str> {
153        if self.hostname.is_empty() {
154            None
155        } else {
156            Some(&self.hostname)
157        }
158    }
159
160    /// Returns the aggregation key of the event.
161    pub fn aggregation_key(&self) -> Option<&str> {
162        if self.aggregation_key.is_empty() {
163            None
164        } else {
165            Some(&self.aggregation_key)
166        }
167    }
168
169    /// Returns the priority of the event.
170    pub fn priority(&self) -> Option<Priority> {
171        self.priority
172    }
173
174    /// Returns the source type name of the event.
175    pub fn source_type_name(&self) -> Option<&str> {
176        if self.source_type_name.is_empty() {
177            None
178        } else {
179            Some(&self.source_type_name)
180        }
181    }
182
183    /// Returns the alert type of the event.
184    pub fn alert_type(&self) -> Option<AlertType> {
185        self.alert_type
186    }
187
188    /// Returns the timestamp of the event.
189    ///
190    /// This is a Unix timestamp, or the number of seconds since the Unix epoch.
191    pub fn timestamp(&self) -> Option<u64> {
192        self.timestamp.map(|ts| ts.get())
193    }
194
195    /// Returns the tags associated with the event.
196    pub fn tags(&self) -> &TagSet {
197        &self.tags
198    }
199
200    /// Returns the origin tags associated with the event.
201    pub fn origin_tags(&self) -> &TagSet {
202        &self.origin_tags
203    }
204
205    /// Set the timestamp.
206    ///
207    /// Represented as a Unix timestamp, or the number of seconds since the Unix epoch.
208    ///
209    /// This variant is specifically for use in builder-style APIs.
210    pub fn with_timestamp(mut self, timestamp: impl Into<Option<u64>>) -> Self {
211        self.timestamp = timestamp.into().and_then(NonZeroU64::new);
212        self
213    }
214
215    /// Set the timestamp.
216    ///
217    /// Represented as a Unix timestamp, or the number of seconds since the Unix epoch.
218    pub fn set_timestamp(&mut self, timestamp: impl Into<Option<u64>>) {
219        self.timestamp = timestamp.into().and_then(NonZeroU64::new);
220    }
221
222    /// Set the hostname where the event originated from.
223    ///
224    /// This variant is specifically for use in builder-style APIs.
225    pub fn with_hostname(mut self, hostname: impl Into<Option<MetaString>>) -> Self {
226        self.hostname = match hostname.into() {
227            Some(s) => s,
228            None => MetaString::empty(),
229        };
230        self
231    }
232
233    /// Set the hostname where the event originated from.
234    pub fn set_hostname(&mut self, hostname: impl Into<Option<MetaString>>) {
235        self.hostname = match hostname.into() {
236            Some(s) => s,
237            None => MetaString::empty(),
238        };
239    }
240
241    /// Set the aggregation key of the event.
242    ///
243    /// Aggregation key is use to group events together in the event stream.
244    ///
245    /// This variant is specifically for use in builder-style APIs.
246    pub fn with_aggregation_key(mut self, aggregation_key: impl Into<Option<MetaString>>) -> Self {
247        self.aggregation_key = match aggregation_key.into() {
248            Some(s) => s,
249            None => MetaString::empty(),
250        };
251        self
252    }
253
254    /// Set the hostname where the event originated from.
255    ///
256    /// Aggregation key is use to group events together in the event stream.
257    pub fn set_aggregation_key(&mut self, aggregation_key: impl Into<Option<MetaString>>) {
258        self.aggregation_key = match aggregation_key.into() {
259            Some(s) => s,
260            None => MetaString::empty(),
261        };
262    }
263
264    /// Set the priority of the event.
265    ///
266    /// This variant is specifically for use in builder-style APIs.
267    pub fn with_priority(mut self, priority: impl Into<Option<Priority>>) -> Self {
268        self.priority = priority.into();
269        self
270    }
271
272    /// Set the priority of the event.
273    pub fn set_priority(&mut self, priority: impl Into<Option<Priority>>) {
274        self.priority = priority.into();
275    }
276
277    /// Set the source type name of the event.
278    ///
279    /// This variant is specifically for use in builder-style APIs.
280    pub fn with_source_type_name(mut self, source_type_name: impl Into<Option<MetaString>>) -> Self {
281        self.source_type_name = match source_type_name.into() {
282            Some(s) => s,
283            None => MetaString::empty(),
284        };
285        self
286    }
287
288    /// Set the source type name of the event.
289    pub fn set_source_type_name(&mut self, source_type_name: impl Into<Option<MetaString>>) {
290        self.source_type_name = match source_type_name.into() {
291            Some(s) => s,
292            None => MetaString::empty(),
293        };
294    }
295
296    /// Set the alert type of the event.
297    ///
298    /// This variant is specifically for use in builder-style APIs.
299    pub fn with_alert_type(mut self, alert_type: impl Into<Option<AlertType>>) -> Self {
300        self.alert_type = alert_type.into();
301        self
302    }
303
304    /// Set the alert type name of the event.
305    pub fn set_alert_type(&mut self, alert_type: impl Into<Option<AlertType>>) {
306        self.alert_type = alert_type.into();
307    }
308
309    /// Set the tags of the event.
310    ///
311    /// This variant is specifically for use in builder-style APIs.
312    pub fn with_tags(mut self, tags: impl Into<TagSet>) -> Self {
313        self.tags = tags.into();
314        self
315    }
316
317    /// Set the tags of the event.
318    pub fn set_tags(&mut self, tags: impl Into<Option<TagSet>>) {
319        self.tags = tags.into().unwrap_or_default();
320    }
321
322    /// Set the origin tags of the event.
323    ///
324    /// This variant is specifically for use in builder-style APIs.
325    pub fn with_origin_tags(mut self, origin_tags: impl Into<TagSet>) -> Self {
326        self.origin_tags = origin_tags.into();
327        self
328    }
329
330    /// Creates an `EventD` from the given title and text.
331    ///
332    /// Defaults to an informational alert with normal priority.
333    pub fn new(title: impl Into<MetaString>, text: impl Into<MetaString>) -> Self {
334        Self {
335            title: title.into(),
336            text: text.into(),
337            timestamp: None,
338            hostname: MetaString::empty(),
339            aggregation_key: MetaString::empty(),
340            priority: Some(Priority::Normal),
341            source_type_name: MetaString::empty(),
342            alert_type: Some(AlertType::Info),
343            tags: TagSet::default(),
344            origin_tags: TagSet::default(),
345        }
346    }
347}