use buffa::EnumValue;
use obs_proto::obs::v1::{ObsEnvelope, SamplingReason, Severity, Tier};
#[must_use]
pub fn self_event(full_name: &str, tier: Tier, sev: Severity) -> ObsEnvelope {
ObsEnvelope {
full_name: full_name.to_string(),
tier: EnumValue::Known(tier),
sev: EnumValue::Known(sev),
sampling_reason: EnumValue::Known(SamplingReason::SAMPLING_REASON_RUNTIME),
ts_ns: now_ns(),
..Default::default()
}
}
#[must_use]
pub fn now_ns() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| u64::try_from(d.as_nanos()).unwrap_or(u64::MAX))
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
use buffa::EnumValue;
use super::*;
#[test]
fn test_should_populate_full_name_tier_sev() {
let env = self_event("obs.runtime.v1.ObsTest", Tier::Log, Severity::Info);
assert_eq!(env.full_name, "obs.runtime.v1.ObsTest");
assert!(matches!(env.tier, EnumValue::Known(Tier::TIER_LOG)));
assert!(matches!(env.sev, EnumValue::Known(Severity::SEVERITY_INFO)));
}
#[test]
fn test_should_set_sampling_reason_runtime() {
let env = self_event("obs.runtime.v1.ObsTest", Tier::Metric, Severity::Warn);
assert!(matches!(
env.sampling_reason,
EnumValue::Known(SamplingReason::SAMPLING_REASON_RUNTIME)
));
}
#[test]
fn test_should_populate_ts_ns() {
let before = now_ns();
let env = self_event("obs.runtime.v1.ObsTest", Tier::Log, Severity::Info);
let after = now_ns();
assert!(env.ts_ns >= before);
assert!(env.ts_ns <= after);
}
}