Skip to main content

stele/
events.rs

1use datadog_protos::events::EventsPayload;
2use serde::{Deserialize, Serialize};
3
4/// A simplified event representation.
5#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
6pub struct Event {
7    title: String,
8    text: String,
9    alert_type: String,
10    aggregation_key: String,
11    hostname: String,
12    priority: String,
13    source_type_name: String,
14    tags: Vec<String>,
15    /// Unix timestamp in seconds from the `d:` field, or 0 if not present.
16    ///
17    /// NOTE: Do not compare this field directly without first normalizing pipeline-generated
18    /// fill-in values — see `EventsAnalyzer` for details.
19    pub timestamp: i64,
20}
21
22impl Event {
23    /// Returns the title of the event.
24    pub fn title(&self) -> &str {
25        &self.title
26    }
27
28    /// Returns the text of the event.
29    pub fn text(&self) -> &str {
30        &self.text
31    }
32
33    /// Returns the alert type of the event.
34    pub fn alert_type(&self) -> &str {
35        &self.alert_type
36    }
37
38    /// Returns the aggregation key of the event.
39    pub fn aggregation_key(&self) -> &str {
40        &self.aggregation_key
41    }
42
43    /// Returns the hostname of the event.
44    pub fn hostname(&self) -> &str {
45        &self.hostname
46    }
47
48    /// Returns the priority of the event.
49    pub fn priority(&self) -> &str {
50        &self.priority
51    }
52
53    /// Returns the source type name of the event.
54    pub fn source_type_name(&self) -> &str {
55        &self.source_type_name
56    }
57
58    /// Returns the tags of the event.
59    pub fn tags(&self) -> &[String] {
60        &self.tags
61    }
62
63    /// Returns the timestamp of the event (Unix seconds, 0 if not set).
64    pub fn timestamp(&self) -> i64 {
65        self.timestamp
66    }
67
68    /// Converts an `EventsPayload` into a list of `Event`s.
69    pub fn from_events_payload(payload: EventsPayload) -> Vec<Self> {
70        payload
71            .events
72            .into_iter()
73            .map(|e| {
74                let mut tags: Vec<String> = e.tags().iter().map(|t| t.to_string()).collect();
75                tags.sort_unstable();
76
77                Event {
78                    title: e.title().to_string(),
79                    text: e.text().to_string(),
80                    alert_type: e.alert_type().to_string(),
81                    aggregation_key: e.aggregation_key().to_string(),
82                    hostname: e.host().to_string(),
83                    priority: e.priority().to_string(),
84                    source_type_name: e.source_type_name().to_string(),
85                    tags,
86                    timestamp: e.ts(),
87                }
88            })
89            .collect()
90    }
91
92    /// Builds an `Event` from the fields extracted from the stock agent's `/intake/` JSON format.
93    #[allow(clippy::too_many_arguments)]
94    pub fn from_intake_event(
95        title: String, text: String, alert_type: String, aggregation_key: String, hostname: String, priority: String,
96        source_type_name: String, mut tags: Vec<String>, timestamp: i64,
97    ) -> Self {
98        tags.sort_unstable();
99        Event {
100            title,
101            text,
102            alert_type,
103            aggregation_key,
104            hostname,
105            priority,
106            source_type_name,
107            tags,
108            timestamp,
109        }
110    }
111}