Skip to main content

saluki_core/data_model/event/log/
mod.rs

1//! Logs.
2
3use std::collections::HashMap;
4use std::num::NonZeroU64;
5
6use serde_json::Value as JsonValue;
7use stringtheory::MetaString;
8
9use crate::data_model::tags::TagSet;
10
11/// A log event.
12#[derive(Clone, Debug, PartialEq)]
13pub struct Log {
14    /// Log message body.
15    message: MetaString,
16    /// When the log entered the pipeline, in nanoseconds since the Unix epoch, if its source recorded it.
17    ingestion_timestamp: Option<NonZeroU64>,
18    /// Log status, as the producer of the log gives it (for example, `"info"`, `"warn"`, or `"Fatal"`).
19    ///
20    /// It isn't normalized, and is empty when the producer gave no status.
21    status: MetaString,
22    /// Log source
23    source: Option<MetaString>,
24    /// Hostname associated with the log.
25    hostname: MetaString,
26    /// Service associated with the log.
27    service: MetaString,
28    /// Tags of the log.
29    tags: TagSet,
30    /// Additional properties of the log.
31    additional_properties: HashMap<MetaString, JsonValue>,
32}
33
34impl Log {
35    /// Creates a new `Log` with the given message.
36    pub fn new(message: impl Into<MetaString>) -> Self {
37        Self {
38            message: message.into(),
39            ingestion_timestamp: None,
40            status: MetaString::empty(),
41            source: None,
42            hostname: MetaString::empty(),
43            service: MetaString::empty(),
44            tags: TagSet::default(),
45            additional_properties: HashMap::new(),
46        }
47    }
48
49    /// Sets the log message.
50    pub fn with_message(mut self, message: impl Into<MetaString>) -> Self {
51        self.message = message.into();
52        self
53    }
54
55    /// Sets when the log entered the pipeline, in nanoseconds since the Unix epoch.
56    ///
57    /// Sources set this when they create a log. It isn't the time of the log itself, which belongs to its properties,
58    /// but the time it was received, for measuring how long logs take to be sent. A timestamp of 0 means unknown.
59    pub fn with_ingestion_timestamp(mut self, timestamp_ns: u64) -> Self {
60        self.ingestion_timestamp = NonZeroU64::new(timestamp_ns);
61        self
62    }
63
64    /// Sets the log status.
65    ///
66    /// The status is kept as it is given, without normalization. An empty status means the log has none.
67    pub fn with_status(mut self, status: impl Into<MetaString>) -> Self {
68        self.status = status.into();
69        self
70    }
71
72    /// Sets the log source.
73    pub fn with_source(mut self, source: impl Into<Option<MetaString>>) -> Self {
74        self.source = source.into();
75        self
76    }
77
78    /// Sets the hostname.
79    pub fn with_hostname(mut self, hostname: impl Into<Option<MetaString>>) -> Self {
80        self.hostname = hostname.into().unwrap_or_else(MetaString::empty);
81        self
82    }
83
84    /// Sets the service name.
85    pub fn with_service(mut self, service: impl Into<Option<MetaString>>) -> Self {
86        self.service = service.into().unwrap_or_else(MetaString::empty);
87        self
88    }
89
90    /// Sets the tags string.
91    pub fn with_tags(mut self, tags: impl Into<Option<TagSet>>) -> Self {
92        self.tags = tags.into().unwrap_or_else(TagSet::default);
93        self
94    }
95
96    /// Sets the additional properties map.
97    pub fn with_additional_properties(
98        mut self, additional_properties: impl Into<Option<HashMap<MetaString, JsonValue>>>,
99    ) -> Self {
100        self.additional_properties = additional_properties.into().unwrap_or_default();
101        self
102    }
103
104    /// Returns the message string slice.
105    pub fn message(&self) -> &str {
106        &self.message
107    }
108
109    /// Returns when the log entered the pipeline, in nanoseconds since the Unix epoch, if its source recorded it.
110    pub fn ingestion_timestamp(&self) -> Option<u64> {
111        self.ingestion_timestamp.map(NonZeroU64::get)
112    }
113
114    /// Returns the log status, or an empty string if the log has none.
115    pub fn status(&self) -> &str {
116        &self.status
117    }
118
119    /// Returns the log source, if set.
120    pub fn source(&self) -> &Option<MetaString> {
121        &self.source
122    }
123
124    /// Returns the hostname, if set.
125    pub fn hostname(&self) -> &str {
126        &self.hostname
127    }
128
129    /// Returns the service name, if set.
130    pub fn service(&self) -> &str {
131        &self.service
132    }
133
134    /// Returns the tags, if set.
135    pub fn tags(&self) -> &TagSet {
136        &self.tags
137    }
138
139    /// Returns the additional properties map.
140    pub fn additional_properties(&self) -> &HashMap<MetaString, JsonValue> {
141        &self.additional_properties
142    }
143}