use std::fmt::{self, Display, Formatter};
pub const DEFAULT_BROWSER_TRACES_SAMPLE_RATE: f32 = 0.2;
pub const DEFAULT_SERVER_TRACES_SAMPLE_RATE: f32 = 0.01;
#[cfg(any(feature = "webserver", feature = "observability"))]
pub(crate) const HTTP_SERVER_OP: &str = "http.server";
#[cfg(any(feature = "webserver", feature = "analytics"))]
#[derive(Clone, Copy, Debug)]
pub(crate) struct UpstreamFailure;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TracesSampleRate(f32);
#[derive(Debug, Clone, Copy, PartialEq, thiserror::Error)]
#[error("traces sample rate {rate} is not between 0.0 and 1.0; use 0.0 to turn tracing off")]
pub struct TracesSampleRateError {
rate: f32,
}
impl TracesSampleRate {
pub const OFF: Self = Self(0.0);
pub const ALL: Self = Self(1.0);
pub const BROWSER_DEFAULT: Self = Self(DEFAULT_BROWSER_TRACES_SAMPLE_RATE);
pub const SERVER_DEFAULT: Self = Self(DEFAULT_SERVER_TRACES_SAMPLE_RATE);
pub fn new(rate: f32) -> Result<Self, TracesSampleRateError> {
if (0.0..=1.0).contains(&rate) {
Ok(Self(rate))
} else {
Err(TracesSampleRateError { rate })
}
}
#[must_use]
pub const fn get(self) -> f32 {
self.0
}
#[must_use]
pub const fn is_on(self) -> bool {
self.0 > 0.0
}
}
impl Display for TracesSampleRate {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl TryFrom<f32> for TracesSampleRate {
type Error = TracesSampleRateError;
fn try_from(rate: f32) -> Result<Self, Self::Error> {
Self::new(rate)
}
}
#[cfg(test)]
mod tests {
use super::{
DEFAULT_BROWSER_TRACES_SAMPLE_RATE, DEFAULT_SERVER_TRACES_SAMPLE_RATE, TracesSampleRate,
};
#[test]
fn both_ends_of_the_range_are_valid() {
assert_eq!(Ok(TracesSampleRate::OFF), TracesSampleRate::new(0.0));
assert_eq!(Ok(TracesSampleRate::ALL), TracesSampleRate::new(1.0));
assert_eq!(0.25, TracesSampleRate::new(0.25).expect("valid").get());
}
#[test]
fn a_rate_outside_the_range_or_not_a_number_is_rejected() {
for rate in [
-0.001,
1.001,
2.0,
f32::NAN,
f32::INFINITY,
f32::NEG_INFINITY,
] {
assert!(TracesSampleRate::new(rate).is_err(), "{rate}");
}
}
#[test]
fn the_error_says_what_to_do() {
let message: String = TracesSampleRate::new(1.5)
.expect_err("too high")
.to_string();
assert_eq!(
"traces sample rate 1.5 is not between 0.0 and 1.0; use 0.0 to turn tracing off",
message
);
}
#[test]
fn only_a_rate_above_zero_is_on() {
assert!(!TracesSampleRate::OFF.is_on());
assert!(TracesSampleRate::SERVER_DEFAULT.is_on());
assert!(TracesSampleRate::ALL.is_on());
}
#[test]
fn the_defaults_are_the_named_constants() {
assert_eq!(
DEFAULT_BROWSER_TRACES_SAMPLE_RATE,
TracesSampleRate::BROWSER_DEFAULT.get()
);
assert_eq!(
DEFAULT_SERVER_TRACES_SAMPLE_RATE,
TracesSampleRate::SERVER_DEFAULT.get()
);
}
#[test]
fn a_rate_prints_as_the_shortest_decimal_for_a_script_literal() {
assert_eq!("0.2", TracesSampleRate::BROWSER_DEFAULT.to_string());
assert_eq!("0.01", TracesSampleRate::SERVER_DEFAULT.to_string());
assert_eq!("1", TracesSampleRate::ALL.to_string());
assert_eq!("0", TracesSampleRate::OFF.to_string());
}
}