Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow sample_rate to also be a float type #4181

Merged
merged 8 commits into from
Oct 30, 2024
103 changes: 99 additions & 4 deletions relay-sampling/src/dsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,27 @@ mod sample_rate_as_string {

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone)]
enum StringOrFloat<'a> {
String(Cow<'a, str>),
Float(f64),
}
Litarnus marked this conversation as resolved.
Show resolved Hide resolved

pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<f64>, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = match Option::<Cow<'_, str>>::deserialize(deserializer)? {
let value = match Option::<StringOrFloat>::deserialize(deserializer)? {
Some(value) => value,
None => return Ok(None),
};

let parsed_value =
serde_json::from_str(&value).map_err(|e| serde::de::Error::custom(e.to_string()))?;
let parsed_value = match value {
StringOrFloat::Float(f) => f,
StringOrFloat::String(s) => {
serde_json::from_str(&s).map_err(|e| serde::de::Error::custom(e.to_string()))?
Litarnus marked this conversation as resolved.
Show resolved Hide resolved
}
};
Litarnus marked this conversation as resolved.
Show resolved Hide resolved

if parsed_value < 0.0 {
return Err(serde::de::Error::custom("sample rate cannot be negative"));
Expand All @@ -175,6 +185,53 @@ mod sample_rate_as_string {
None => value.serialize(serializer),
}
}

struct StringOrFloatVisitor;

impl<'de> serde::de::Visitor<'de> for StringOrFloatVisitor {
type Value = StringOrFloat<'de>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a string or a float")
}

fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(StringOrFloat::Float(v))
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(StringOrFloat::String(Cow::Owned(v.to_owned())))
}

fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(StringOrFloat::String(Cow::Borrowed(v)))
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(StringOrFloat::String(Cow::Owned(v)))
}
}

impl<'de> Deserialize<'de> for StringOrFloat<'de> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_any(StringOrFloatVisitor)
}
}
}

struct BoolOptionVisitor;
Expand Down Expand Up @@ -387,7 +444,45 @@ mod tests {
"sample_rate": 0.1
}
"#;
serde_json::from_str::<DynamicSamplingContext>(json).unwrap_err();
let dsc = serde_json::from_str::<DynamicSamplingContext>(json).expect("sample rate float");
Litarnus marked this conversation as resolved.
Show resolved Hide resolved
insta::assert_ron_snapshot!(dsc, @r#"
{
"trace_id": "00000000-0000-0000-0000-000000000000",
"public_key": "abd0f232775f45feab79864e580d160b",
"release": None,
"environment": None,
"transaction": None,
"sample_rate": "0.1",
"user_id": "hello",
"replay_id": None,
}
"#);
}

#[test]
fn test_parse_sample_rate_integer() {
let json = r#"
{
"trace_id": "00000000-0000-0000-0000-000000000000",
"public_key": "abd0f232775f45feab79864e580d160b",
"user_id": "hello",
"sample_rate": "1"
}
"#;
let dsc =
serde_json::from_str::<DynamicSamplingContext>(json).expect("sample rate integer");
insta::assert_ron_snapshot!(dsc, @r#"
{
"trace_id": "00000000-0000-0000-0000-000000000000",
"public_key": "abd0f232775f45feab79864e580d160b",
"release": None,
"environment": None,
"transaction": None,
"sample_rate": "1.0",
"user_id": "hello",
"replay_id": None,
}
"#);
}

#[test]
Expand Down
Loading