Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clean up eventd and service check types #130

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions lib/saluki-event/src/eventd/alert.rs

This file was deleted.

72 changes: 49 additions & 23 deletions lib/saluki-event/src/eventd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
//! Event types
mod priority;
pub use self::priority::*;
//! Events.

mod alert;
pub use self::alert::*;
// TODO: Switch usages of `String` to `MetaString` since we should generally be able to intern these strings as they
// originate in in the DogStatsD codec, where interning is already taking place.
Comment on lines +3 to +4
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we'll try to keep TODO comments close to the area where they're most relevant, but in this case, this is a thing we'd want to apply to multiple fields on the EventD struct, so we just shove it up here as a general TODO.


/// Alert type.
#[derive(Clone, Copy, Debug)]
Copy link
Member Author

@tobz tobz Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this was previously only Clone, enums are a prime candidate for being Copy. This means the value can be copied directly -- just copy the raw memory bytes and nothing else -- without any special logic.

Generally, integral types (signed and unsigned integers, floats, booleans) and "unit" enums (the variants don't have any fields) can stand to be Copy because their size in memory is no bigger than a reference to the type itself.

This is to say that if you compare the size of this enum to a reference to this enum, the reference to it is much larger: 8 bytes versus 1 byte. References are at least a machine word (so 8 bytes on 64-bit architectures) while the compiler ends up bitpacking this enum in a single byte. The same thing holds if we use an integer type like u32: &u32 is 8 bytes while u32 itself is only 4 bytes.

pub enum AlertType {
/// Indicates an informational event.
Info,

/// Indicates an error event.
Error,

/// Indicates a warning event.
Warning,

/// Indicates a successful event.
Success,
}

/// Event priority.
#[derive(Clone, Copy, Debug)]
pub enum Priority {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For both Priority and AlertType, I've moved them to mod.rs because they're simple enough that it ends up being a little cleaner to have it all in the same file. If we had an impl block, or tests, then it might make sense to push it into a separate file.

/// The event has normal priority.
Normal,

/// The event has low priority.
Low,
}

/// EventD is an object that can be posted to the DataDog event stream.
#[derive(Clone, Debug)]
Expand All @@ -13,55 +37,57 @@ pub struct EventD {
timestamp: u64,
host: String,
aggregation_key: String,
priority: EventPriority,
priority: Priority,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've renamed EventPriority to Priority to avoid stuttering.

source_type_name: String,
alert_type: EventAlertType,
alert_type: AlertType,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, we've renamed EventAlertType to AlertType to avoid stuttering.

tags: Vec<String>,
}

impl EventD {
/// Gets a reference to the title.
/// Gets the title of the event.
pub fn title(&self) -> &str {
&self.title
}

/// Gets a reference to the text.
/// Gets the text of the event.
pub fn text(&self) -> &str {
&self.text
}

/// Gets a reference to the host.
/// Gets the host where the event originated from.
pub fn host(&self) -> &str {
&self.host
}

/// Gets a reference to the aggregation key.
/// Gets the aggregation key of the event.
pub fn aggregation_key(&self) -> &str {
&self.aggregation_key
}

/// Gets a reference to the priority.
pub fn priority(&self) -> &EventPriority {
&self.priority
/// Gets the priority of the event.
pub fn priority(&self) -> Priority {
self.priority
}

/// Gets a reference to the source type name.
/// Gets the source type name of the event.
pub fn source_type_name(&self) -> &str {
&self.source_type_name
}

/// Gets a reference to the alert type
pub fn alert_type(&self) -> &EventAlertType {
&self.alert_type
/// Gets the alert type of the event.
pub fn alert_type(&self) -> AlertType {
self.alert_type
}

/// Gets a reference to the timestamp.
pub fn timestamp(&self) -> &u64 {
&self.timestamp
/// Gets the timestamp of the event.
///
/// This is a Unix timestamp, or the number of seconds since the Unix epoch.
pub fn timestamp(&self) -> u64 {
self.timestamp
}

/// Gets a reference to the tags.
pub fn tags(&self) -> &Vec<String> {
/// Gets the tags associated with the event.
pub fn tags(&self) -> &[String] {
&self.tags
}
}
9 changes: 0 additions & 9 deletions lib/saluki-event/src/eventd/priority.rs

This file was deleted.

56 changes: 39 additions & 17 deletions lib/saluki-event/src/service_check/mod.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,68 @@
//! Service Check type
mod status;
pub use self::status::*;
//! Service checks.

/// ServiceCheck
// TODO: Switch usages of `String` to `MetaString` since we should generally be able to intern these strings as they
// originate in in the DogStatsD codec, where interning is already taking place.

/// Service status.
#[derive(Clone, Copy, Debug)]
pub enum CheckStatus {
/// The service is operating normally.
Ok,

/// The service is in a warning state.
Warn,

/// The service is in a critical state.
Critical,

/// The service is in an unknown state.
Unknown,
}

/// A service check.
///
/// Service checks represent the status of a service at a particular point in time. Checks are simplistic, with a basic
/// message, status enum (OK vs warning vs critical, etc), timestamp, and tags.
#[derive(Clone, Debug)]
pub struct ServiceCheck {
name: String,
status: ServiceCheckStatus,
status: CheckStatus,
timestamp: u64,
host: String,
message: String,
tags: Vec<String>,
}

impl ServiceCheck {
/// Gets a reference to the service check name
pub fn name(&self) -> &String {
/// Gets the name of the check.
pub fn name(&self) -> &str {
&self.name
}

/// Gets a reference to the status
pub fn status(&self) -> &ServiceCheckStatus {
&self.status
/// Gets the status of the check.
pub fn status(&self) -> CheckStatus {
self.status
}

/// Gets a reference to the timestamp
/// Gets the timestamp of the check.
///
/// This is a Unix timestamp, or the number of seconds since the Unix epoch.
pub fn timestamp(&self) -> u64 {
self.timestamp
}

/// Gets a reference to the host name
pub fn host(&self) -> &String {
/// Gets the host where the check originated from.
pub fn host(&self) -> &str {
&self.host
}

/// Gets a reference to the message
pub fn message(&self) -> &String {
/// Gets the message associated with the check.
pub fn message(&self) -> &str {
&self.message
}

/// Gets a reference to the tags
pub fn tags(&self) -> &Vec<String> {
/// Gets the tags associated with the check.
pub fn tags(&self) -> &[String] {
&self.tags
}
}
15 changes: 0 additions & 15 deletions lib/saluki-event/src/service_check/status.rs

This file was deleted.