Skip to content

Commit

Permalink
Strong typing for supertd_events columns
Browse files Browse the repository at this point in the history
Summary: Make sure column names are not mistyped and provide an opportunity to do convenient conversions (e.g. `Duration::as_millis`) by enumerating the allowed columns in the `scuba!` macro.

Reviewed By: ndmitchell

Differential Revision: D54956684

fbshipit-source-id: ca1b6f8af8d51c74cf113668f04a9aef88b78450
  • Loading branch information
rjbailey authored and facebook-github-bot committed Mar 15, 2024
1 parent 4cb47fc commit 5119d15
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion btd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub fn main(args: Args) -> anyhow::Result<()> {
}
td_util::scuba!(
event: BTD_SUCCESS,
duration_ms: t.elapsed().as_millis(),
duration: t.elapsed(),
data: json!({
"immediate_changes": immediate_changes,
"total_changes": total_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 @@ -122,7 +122,7 @@ pub fn run(
if status.success() {
td_util::scuba!(
event: TARGETS_SUCCESS,
duration_ms: t.elapsed().as_millis(),
duration: t.elapsed(),
);
Ok(())
} else {
Expand Down
23 changes: 15 additions & 8 deletions td_util/src/supertd_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ pub fn init(fb: fbinit::FacebookInit) -> ScubaClientGuard {
/// # Examples
///
/// ```
/// # let foos_run = 10;
/// # let bars_launched = 2;
/// # let f = || (10, 2);
/// let t = std::time::Instant::now();
/// let (foos_run, bars_launched) = f();
/// td_util::scuba!(
/// event: BTD_SUCCESS,
/// count: foos_run + bars_launched,
/// duration: t.elapsed(),
/// data: json!({
/// "arbitrary": ["JSON", "object"],
/// "foos_run": foos_run,
Expand All @@ -92,7 +93,7 @@ pub fn init(fb: fbinit::FacebookInit) -> ScubaClientGuard {
/// ```
#[macro_export]
macro_rules! scuba {
( event: $event:ident, $($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) })*
Expand All @@ -101,12 +102,12 @@ macro_rules! scuba {
"Failed to log to supertd_events Scuba: {:?}", e);
}
};
( 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, event, $value:expr ) ) => {
compile_error!("duplicate `event` field in `scuba!` macro");
};
( @SET_FIELD ( $builder:ident, data, $value:expr ) ) => {{
use $crate::supertd_events::serde_json::json;
match $crate::supertd_events::serde_json::to_string(&$value) {
Expand All @@ -119,8 +120,14 @@ macro_rules! scuba {
}
}
}};
( @SET_FIELD ( $builder:ident, duration, $value:expr ) ) => {
$builder.add("duration_ms", ::std::time::Duration::as_millis(&$value));
};
( @SET_FIELD ( $builder:ident, duration_ms, $value:expr ) ) => {
compile_error!("unrecognized column name in `scuba!` macro: duration_ms (use `duration` instead)");
};
( @SET_FIELD ( $builder:ident, $key:ident, $value:expr ) ) => {
$builder.add(stringify!($key), $value);
compile_error!(concat!("unrecognized column name in `scuba!` macro: ", stringify!($key)));
};
}

Expand Down

0 comments on commit 5119d15

Please sign in to comment.