Skip to content

Commit

Permalink
Add sample_rate to scuba! macro
Browse files Browse the repository at this point in the history
Summary: Make it easy to add sampling by adding a sample_rate "column". This behaves a little differently from the other columns (it doesn't invoke `builder.add`, and has the side effect of causing the sample not to be logged sometimes), but it does result in a sample_rate column being set in the underlying Scuba sample with the given value (set [here](https://www.internalfb.com/code/fbsource/[e6c006d6f13a]/fbcode/common/rust/shed/scuba_sample/src/sampling.rs?lines=68)), so that doesn't seem so bad.

Differential Revision: D54959900

fbshipit-source-id: 2ab102614ec0f1383f4cf37129842e3636400355
  • Loading branch information
rjbailey authored and facebook-github-bot committed Mar 15, 2024
1 parent 5119d15 commit 77a38fc
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions td_util/src/supertd_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,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, sample_rate, $value:expr ) ) => {
if let Some(sample_rate) = ::std::num::NonZeroU64::new($value) {
$builder.sampled(sample_rate);
} else {
$crate::supertd_events::tracing::error!(
"`sample_rate` must be nonzero in `scuba!` macro. This sample will always be logged.");
}
};
( @SET_FIELD ( $builder:ident, duration_ms, $value:expr ) ) => {
compile_error!("unrecognized column name in `scuba!` macro: duration_ms (use `duration` instead)");
};
Expand All @@ -133,9 +141,10 @@ macro_rules! scuba {

/// Get the sample builder for the `supertd_events` dataset.
///
/// Most use cases should use the [`scuba!`] macro instead of this function, but
/// this function can be used to access the underlying [`ScubaSampleBuilder`]
/// (in order to do things like set the sampling rate).
/// Please use the [`scuba!`] macro instead of this function, since it provides
/// additional type safety (e.g., prevents typos in column names). This function
/// is exposed only for internal use by the macro.
#[doc(hidden)]
pub fn sample_builder() -> ScubaSampleBuilder {
BUILDER
.get()
Expand Down

0 comments on commit 77a38fc

Please sign in to comment.