diff --git a/sentry-core/src/metrics/mod.rs b/sentry-core/src/metrics/mod.rs index f4a38726..777115fc 100644 --- a/sentry-core/src/metrics/mod.rs +++ b/sentry-core/src/metrics/mod.rs @@ -534,11 +534,11 @@ impl Metric { .as_secs(); let data = format!( "{}@{}:{}|{}|#{}|T{}", - NormalizedName::from(self.name.as_ref()), - NormalizedUnit::from(self.unit.to_string().as_ref()), + NormalizedName::from_str(self.name.as_ref()), + NormalizedUnit::from_str(self.unit.to_string().as_ref()), self.value, self.value.ty(), - NormalizedTags::from(&self.tags), + NormalizedTags::from_tag_map(&self.tags), timestamp ); EnvelopeItem::Statsd(data.into_bytes()).into() @@ -836,10 +836,10 @@ impl Worker { for (timestamp, buckets) in buckets { for (key, value) in buckets { - write!(&mut out, "{}", NormalizedName::from(key.name.as_ref()))?; + write!(&mut out, "{}", NormalizedName::from_str(key.name.as_ref()))?; match key.unit { MetricUnit::Custom(u) => { - write!(&mut out, "@{}", NormalizedUnit::from(u.as_ref()))? + write!(&mut out, "@{}", NormalizedUnit::from_str(u.as_ref()))? } _ => write!(&mut out, "@{}", key.unit)?, } @@ -868,7 +868,7 @@ impl Worker { write!(&mut out, "|{}", key.ty.as_str())?; let normalized_tags = - NormalizedTags::from(&key.tags).with_default_tags(&self.default_tags); + NormalizedTags::from_tag_map(&key.tags).with_default_tags(&self.default_tags); write!(&mut out, "|#{}", normalized_tags)?; writeln!(&mut out, "|T{}", timestamp)?; } diff --git a/sentry-core/src/metrics/normalization/normalized_name.rs b/sentry-core/src/metrics/normalization/normalized_name.rs index 00277fdc..3e3d1a10 100644 --- a/sentry-core/src/metrics/normalization/normalized_name.rs +++ b/sentry-core/src/metrics/normalization/normalized_name.rs @@ -6,8 +6,8 @@ pub struct NormalizedName<'a> { name: Cow<'a, str>, } -impl<'a> From<&'a str> for NormalizedName<'a> { - fn from(name: &'a str) -> Self { +impl<'a> NormalizedName<'a> { + pub fn from_str(name: &'a str) -> Self { static METRIC_NAME_RE: OnceLock = OnceLock::new(); Self { name: METRIC_NAME_RE @@ -31,7 +31,7 @@ mod test { fn test_from() { let expected = "aA1_-.____________"; - let actual = NormalizedName::from("aA1_-./+รถ{๐Ÿ˜€\n\t\r\\| ,").to_string(); + let actual = NormalizedName::from_str("aA1_-./+รถ{๐Ÿ˜€\n\t\r\\| ,").to_string(); assert_eq!(expected, actual); } @@ -40,7 +40,7 @@ mod test { fn test_length_restriction() { let expected = "a".repeat(150); - let actual = NormalizedName::from("a".repeat(155).as_ref()).to_string(); + let actual = NormalizedName::from_str("a".repeat(155).as_ref()).to_string(); assert_eq!(expected, actual); } diff --git a/sentry-core/src/metrics/normalization/normalized_tags.rs b/sentry-core/src/metrics/normalization/normalized_tags.rs index 27fe7a46..abf464f0 100644 --- a/sentry-core/src/metrics/normalization/normalized_tags.rs +++ b/sentry-core/src/metrics/normalization/normalized_tags.rs @@ -8,8 +8,8 @@ pub struct NormalizedTags<'a> { tags: HashMap, String>, } -impl<'a> From<&'a TagMap> for NormalizedTags<'a> { - fn from(tags: &'a TagMap) -> Self { +impl<'a> NormalizedTags<'a> { + pub fn from_tag_map(tags: &'a TagMap) -> Self { Self { tags: tags .iter() @@ -95,7 +95,7 @@ mod test { ); let expected = "aa:a\\na,bb:b\\rb,cc:c\\tc,dd:d\\\\d,ee:e\\u{7c}e,ff:f\\u{2c}f"; - let actual = NormalizedTags::from(&tags).to_string(); + let actual = NormalizedTags::from_tag_map(&tags).to_string(); assert_eq!(expected, actual); } @@ -109,7 +109,7 @@ mod test { ); let expected = ""; - let actual = NormalizedTags::from(&tags).to_string(); + let actual = NormalizedTags::from_tag_map(&tags).to_string(); assert_eq!(expected, actual); } @@ -119,7 +119,7 @@ mod test { let tags = TagMap::from([("aA1_-./+รถ{ ๐Ÿ˜€".into(), "aA1_-./+รถ{ ๐Ÿ˜€".into())]); let expected = "aA1_-./:aA1_-./+รถ{ ๐Ÿ˜€"; - let actual = NormalizedTags::from(&tags).to_string(); + let actual = NormalizedTags::from_tag_map(&tags).to_string(); assert_eq!(expected, actual); } @@ -132,7 +132,7 @@ mod test { ]); let expected = "environment:production,release:default_release"; - let actual = NormalizedTags::from(&TagMap::new()) + let actual = NormalizedTags::from_tag_map(&TagMap::new()) .with_default_tags(&default_tags) .to_string(); @@ -147,7 +147,7 @@ mod test { ]); let expected = "environment:custom_env,release:custom_release"; - let actual = NormalizedTags::from(&TagMap::from([ + let actual = NormalizedTags::from_tag_map(&TagMap::from([ ("release".into(), "custom_release".into()), ("environment".into(), "custom_env".into()), ])) @@ -167,7 +167,7 @@ mod test { + ":" + "v".repeat(200).as_str(); - let actual = NormalizedTags::from(&TagMap::from([( + let actual = NormalizedTags::from_tag_map(&TagMap::from([( "k".repeat(35).into(), "v".repeat(210).into(), )])) diff --git a/sentry-core/src/metrics/normalization/normalized_unit.rs b/sentry-core/src/metrics/normalization/normalized_unit.rs index 40fbb04e..f0605de9 100644 --- a/sentry-core/src/metrics/normalization/normalized_unit.rs +++ b/sentry-core/src/metrics/normalization/normalized_unit.rs @@ -8,8 +8,8 @@ pub struct NormalizedUnit<'a> { unit: Cow<'a, str>, } -impl<'a> From<&'a str> for NormalizedUnit<'a> { - fn from(unit: &'a str) -> Self { +impl<'a> NormalizedUnit<'a> { + pub fn from_str(unit: &'a str) -> Self { static METRIC_UNIT_RE: OnceLock = OnceLock::new(); let normalized_unit = METRIC_UNIT_RE .get_or_init(|| Regex::new(r"[^a-zA-Z0-9_]").expect("Regex should compile")) @@ -37,7 +37,7 @@ mod test { fn test_from() { let expected = "aA1_"; - let actual = NormalizedUnit::from("aA1_-./+รถ{๐Ÿ˜€\n\t\r\\| ,").to_string(); + let actual = NormalizedUnit::from_str("aA1_-./+รถ{๐Ÿ˜€\n\t\r\\| ,").to_string(); assert_eq!(expected, actual); } @@ -46,7 +46,7 @@ mod test { fn test_from_empty() { let expected = "none"; - let actual = NormalizedUnit::from("").to_string(); + let actual = NormalizedUnit::from_str("").to_string(); assert_eq!(expected, actual); } @@ -55,7 +55,7 @@ mod test { fn test_from_empty_after_normalization() { let expected = "none"; - let actual = NormalizedUnit::from("+").to_string(); + let actual = NormalizedUnit::from_str("+").to_string(); assert_eq!(expected, actual); } @@ -64,7 +64,7 @@ mod test { fn test_length_restriction() { let expected = "a".repeat(15); - let actual = NormalizedUnit::from("a".repeat(20).as_ref()).to_string(); + let actual = NormalizedUnit::from_str("a".repeat(20).as_ref()).to_string(); assert_eq!(expected, actual); }