Skip to content

Commit

Permalink
Use enum for event in scuba! macro
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rjbailey authored and facebook-github-bot committed Feb 16, 2024
1 parent 60a46fc commit 3a54446
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion btd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion targets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
26 changes: 24 additions & 2 deletions td_util/src/supertd_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ const SCUBA_DATASET: &str = "supertd_events";

static BUILDER: OnceLock<ScubaSampleBuilder> = 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.
Expand Down Expand Up @@ -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"],
Expand All @@ -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.
Expand Down

0 comments on commit 3a54446

Please sign in to comment.