From 3a544467cdfa37256c70a109105dc9020bc94ceb Mon Sep 17 00:00:00 2001 From: Jake Bailey Date: Fri, 16 Feb 2024 14:41:34 -0800 Subject: [PATCH] Use enum for event in `scuba!` macro Summary: Require the `event` column to be a variant of `supertd_events::Event`. This should help ensure that when a new event is added, we don't accidentally use a name which is already in use. Reviewed By: aniketmathur Differential Revision: D53839048 fbshipit-source-id: cbd119d91ef60978ae9e713c32e8e23ffdfcae7f --- btd/src/lib.rs | 2 +- targets/src/lib.rs | 2 +- td_util/src/supertd_events.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/btd/src/lib.rs b/btd/src/lib.rs index d87b1db..6843c54 100644 --- a/btd/src/lib.rs +++ b/btd/src/lib.rs @@ -264,7 +264,7 @@ pub fn main(args: Args) -> anyhow::Result<()> { "finish with {immediate_changes} immediate changes, {total_changes} total changes" )); td_util::scuba!( - event: "BTD_SUCCESS", + event: BTD_SUCCESS, duration_ms: t.elapsed().as_millis() as u64, data: json!({ "immediate_changes": immediate_changes, diff --git a/targets/src/lib.rs b/targets/src/lib.rs index 7ffe053..65eac5f 100644 --- a/targets/src/lib.rs +++ b/targets/src/lib.rs @@ -101,7 +101,7 @@ pub fn run( let status = command.status()?; if status.success() { td_util::scuba!( - event: "TARGETS_SUCCESS", + event: TARGETS_SUCCESS, duration_ms: t.elapsed().as_millis() as u64, ); Ok(()) diff --git a/td_util/src/supertd_events.rs b/td_util/src/supertd_events.rs index ce6aa69..957f3bb 100644 --- a/td_util/src/supertd_events.rs +++ b/td_util/src/supertd_events.rs @@ -18,6 +18,21 @@ const SCUBA_DATASET: &str = "supertd_events"; static BUILDER: OnceLock = OnceLock::new(); +/// All events logged to the `supertd_events` dataset. +/// +/// Each event should generally be logged from a single source location. +#[derive(Debug)] +#[allow(non_camel_case_types)] +pub enum Event { + BTD_SUCCESS, + INVALID_TRIGGER, + RANKER_SUCCESS, + SCHEDULER_SUCCESS, + TARGETS_SUCCESS, + VERIFIABLE_MATCHER_SUCCESS, + VERSE_SUCCESS, +} + /// Initialize the Scuba client for the `supertd_events` dataset. /// /// Returns a guard that flushes the Scuba client when dropped. @@ -68,7 +83,7 @@ pub fn init(fb: fbinit::FacebookInit) -> ScubaClientGuard { /// # let foos_run = 10; /// # let bars_launched = 2; /// td_util::scuba!( -/// event: "COMMAND_SUCCESS", +/// event: BTD_SUCCESS, /// count: foos_run + bars_launched, /// data: json!({ /// "arbitrary": ["JSON", "object"], @@ -79,11 +94,18 @@ pub fn init(fb: fbinit::FacebookInit) -> ScubaClientGuard { /// ``` #[macro_export] macro_rules! scuba { - ( $($key:ident : $value:expr),* $(,)? ) => { + ( event: $event:ident, $($key:ident : $value:expr),* $(,)? ) => { let mut builder = $crate::supertd_events::sample_builder(); + builder.add("event", format!("{:?}", &$crate::supertd_events::Event::$event)); $($crate::scuba! { @SET_FIELD(builder, $key, $value) })* builder.log(); }; + ( event: $event:ident ) => { + $crate::scuba! { event: $event, } + }; + ( $($key:ident : $value:expr),* $(,)? ) => { + compile_error!("`event` must be the first field in the `scuba!` macro"); + }; ( @SET_FIELD ( $builder:ident, data, $value:expr ) ) => {{ use $crate::supertd_events::serde_json::json; // This unwrap should only fail for map keys with invalid UTF-8.