Skip to content

Commit

Permalink
Report Scuba failures to stderr via tracing::error!
Browse files Browse the repository at this point in the history
Summary: Also report failures to serialize the `data` field as tracing errors.

Reviewed By: aniketmathur

Differential Revision: D53839049

fbshipit-source-id: 4ca1f7d7ccb2e50d13ab4e3be4d59e5c8a94737a
  • Loading branch information
rjbailey authored and facebook-github-bot committed Feb 17, 2024
1 parent 3a54446 commit 03c9109
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions td_util/src/supertd_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::sync::OnceLock;

use scuba::ScubaSampleBuilder;
pub use serde_json;
pub use tracing;

const SCUBA_DATASET: &str = "supertd_events";

Expand Down Expand Up @@ -73,10 +74,6 @@ pub fn init(fb: fbinit::FacebookInit) -> ScubaClientGuard {
///
/// If [`init`] has not been invoked, the sample will not be logged.
///
/// # Panics
///
/// Panics if [`serde_json::to_string`] fails for `data`.
///
/// # Examples
///
/// ```
Expand All @@ -98,7 +95,10 @@ macro_rules! scuba {
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();
if let Err(e) = builder.try_log() {
$crate::supertd_events::tracing::error!(
"Failed to log to supertd_events Scuba: {:?}", e);
}
};
( event: $event:ident ) => {
$crate::scuba! { event: $event, }
Expand All @@ -108,8 +108,15 @@ macro_rules! scuba {
};
( @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.
$builder.add("data", $crate::supertd_events::serde_json::to_string(&$value).unwrap());
match $crate::supertd_events::serde_json::to_string(&$value) {
Ok(json) => {
$builder.add("data", json);
}
Err(e) => {
$crate::supertd_events::tracing::error!(
"Failed to serialize `data` column in `scuba!` macro: {:?}", e);
}
}
}};
( @SET_FIELD ( $builder:ident, $key:ident, $value:expr ) ) => {
$builder.add(stringify!($key), $value);
Expand Down Expand Up @@ -174,7 +181,9 @@ pub struct ScubaClientGuard(());
impl Drop for ScubaClientGuard {
fn drop(&mut self) {
if let Some(builder) = BUILDER.get() {
builder.flush(std::time::Duration::from_secs(5));
if let Err(e) = builder.try_flush(std::time::Duration::from_secs(5)) {
tracing::error!("Failed to flush supertd_events Scuba: {:?}", e);
}
}
}
}

0 comments on commit 03c9109

Please sign in to comment.