Skip to content

Commit f3e7a57

Browse files
author
Elias Ram
committed
removed From trait
1 parent 835f025 commit f3e7a57

File tree

5 files changed

+56
-82
lines changed

5 files changed

+56
-82
lines changed

sentry-core/src/metrics/mod.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ use std::sync::{Arc, Mutex};
5454
use std::thread::{self, JoinHandle};
5555
use std::time::{Duration, SystemTime, UNIX_EPOCH};
5656

57-
use normalization::normalized_name::NormalizedName;
58-
use normalization::normalized_tags::NormalizedTags;
59-
use normalization::normalized_unit::NormalizedUnit;
6057
use sentry_types::protocol::latest::{Envelope, EnvelopeItem};
6158

6259
use crate::client::TransportArc;
@@ -534,11 +531,11 @@ impl Metric {
534531
.as_secs();
535532
let data = format!(
536533
"{}@{}:{}|{}|#{}|T{}",
537-
NormalizedName::from(self.name.as_ref()),
538-
NormalizedUnit::from(self.unit.to_string().as_ref()),
534+
normalization::normalize_name(self.name.as_ref()),
535+
normalization::normalize_unit(self.unit.to_string().as_ref()),
539536
self.value,
540537
self.value.ty(),
541-
NormalizedTags::from(&self.tags),
538+
normalization::normalize_tags(&self.tags),
542539
timestamp
543540
);
544541
EnvelopeItem::Statsd(data.into_bytes()).into()
@@ -836,10 +833,14 @@ impl Worker {
836833

837834
for (timestamp, buckets) in buckets {
838835
for (key, value) in buckets {
839-
write!(&mut out, "{}", NormalizedName::from(key.name.as_ref()))?;
836+
write!(
837+
&mut out,
838+
"{}",
839+
normalization::normalize_name(key.name.as_ref())
840+
)?;
840841
match key.unit {
841842
MetricUnit::Custom(u) => {
842-
write!(&mut out, "@{}", NormalizedUnit::from(u.as_ref()))?
843+
write!(&mut out, "@{}", normalization::normalize_unit(u.as_ref()))?
843844
}
844845
_ => write!(&mut out, "@{}", key.unit)?,
845846
}
@@ -868,7 +869,7 @@ impl Worker {
868869

869870
write!(&mut out, "|{}", key.ty.as_str())?;
870871
let normalized_tags =
871-
NormalizedTags::from(&key.tags).with_default_tags(&self.default_tags);
872+
normalization::normalize_tags(&key.tags).with_default_tags(&self.default_tags);
872873
write!(&mut out, "|#{}", normalized_tags)?;
873874
writeln!(&mut out, "|T{}", timestamp)?;
874875
}

sentry-core/src/metrics/normalization/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ pub mod normalized_name;
22
pub mod normalized_tags;
33
pub mod normalized_unit;
44

5+
pub use normalized_name::normalize_name;
6+
pub use normalized_tags::normalize_tags;
7+
pub use normalized_unit::normalize_unit;
8+
59
pub fn truncate(s: &str, max_chars: usize) -> &str {
610
match s.char_indices().nth(max_chars) {
711
None => s,

sentry-core/src/metrics/normalization/normalized_name.rs

+8-22
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,21 @@ use std::{borrow::Cow, sync::OnceLock};
22

33
use regex::Regex;
44

5-
pub struct NormalizedName<'a> {
6-
name: Cow<'a, str>,
7-
}
8-
9-
impl<'a> From<&'a str> for NormalizedName<'a> {
10-
fn from(name: &'a str) -> Self {
11-
static METRIC_NAME_RE: OnceLock<Regex> = OnceLock::new();
12-
Self {
13-
name: METRIC_NAME_RE
14-
.get_or_init(|| Regex::new(r"[^a-zA-Z0-9_\-.]").expect("Regex should compile"))
15-
.replace_all(super::truncate(name, 150), "_"),
16-
}
17-
}
18-
}
19-
20-
impl std::fmt::Display for NormalizedName<'_> {
21-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
22-
write!(f, "{}", self.name)
23-
}
5+
pub fn normalize_name(name: &str) -> Cow<str> {
6+
static METRIC_NAME_RE: OnceLock<Regex> = OnceLock::new();
7+
METRIC_NAME_RE
8+
.get_or_init(|| Regex::new(r"[^a-zA-Z0-9_\-.]").expect("Regex should compile"))
9+
.replace_all(super::truncate(name, 150), "_")
2410
}
2511

2612
#[cfg(test)]
2713
mod test {
28-
use crate::metrics::NormalizedName;
2914

3015
#[test]
3116
fn test_from() {
3217
let expected = "aA1_-.____________";
3318

34-
let actual = NormalizedName::from("aA1_-./+ö{😀\n\t\r\\| ,").to_string();
19+
let actual = super::normalize_name("aA1_-./+ö{😀\n\t\r\\| ,");
3520

3621
assert_eq!(expected, actual);
3722
}
@@ -40,7 +25,8 @@ mod test {
4025
fn test_length_restriction() {
4126
let expected = "a".repeat(150);
4227

43-
let actual = NormalizedName::from("a".repeat(155).as_ref()).to_string();
28+
let too_long_name = "a".repeat(155);
29+
let actual = super::normalize_name(&too_long_name);
4430

4531
assert_eq!(expected, actual);
4632
}

sentry-core/src/metrics/normalization/normalized_tags.rs

+21-24
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@ use std::{borrow::Cow, collections::HashMap, sync::OnceLock};
44

55
use crate::metrics::TagMap;
66

7-
pub struct NormalizedTags<'a> {
8-
tags: HashMap<Cow<'a, str>, String>,
7+
pub fn normalize_tags(tags: &TagMap) -> NormalizedTags {
8+
NormalizedTags {
9+
tags: tags
10+
.iter()
11+
.map(|(k, v)| {
12+
(
13+
NormalizedTags::normalize_key(super::truncate(k, 32)),
14+
NormalizedTags::normalize_value(super::truncate(v, 200)),
15+
)
16+
})
17+
.filter(|(k, v)| !k.is_empty() && !v.is_empty())
18+
.collect(),
19+
}
920
}
1021

11-
impl<'a> From<&'a TagMap> for NormalizedTags<'a> {
12-
fn from(tags: &'a TagMap) -> Self {
13-
Self {
14-
tags: tags
15-
.iter()
16-
.map(|(k, v)| {
17-
(
18-
Self::normalize_key(super::truncate(k, 32)),
19-
Self::normalize_value(super::truncate(v, 200)),
20-
)
21-
})
22-
.filter(|(k, v)| !k.is_empty() && !v.is_empty())
23-
.collect(),
24-
}
25-
}
22+
pub struct NormalizedTags<'a> {
23+
tags: HashMap<Cow<'a, str>, String>,
2624
}
2725

2826
impl<'a> NormalizedTags<'a> {
@@ -76,7 +74,6 @@ impl std::fmt::Display for NormalizedTags<'_> {
7674

7775
#[cfg(test)]
7876
mod test {
79-
use super::NormalizedTags;
8077
use super::TagMap;
8178

8279
#[test]
@@ -95,7 +92,7 @@ mod test {
9592
);
9693
let expected = "aa:a\\na,bb:b\\rb,cc:c\\tc,dd:d\\\\d,ee:e\\u{7c}e,ff:f\\u{2c}f";
9794

98-
let actual = NormalizedTags::from(&tags).to_string();
95+
let actual = super::normalize_tags(&tags).to_string();
9996

10097
assert_eq!(expected, actual);
10198
}
@@ -109,7 +106,7 @@ mod test {
109106
);
110107
let expected = "";
111108

112-
let actual = NormalizedTags::from(&tags).to_string();
109+
let actual = super::normalize_tags(&tags).to_string();
113110

114111
assert_eq!(expected, actual);
115112
}
@@ -119,7 +116,7 @@ mod test {
119116
let tags = TagMap::from([("aA1_-./+ö{ 😀".into(), "aA1_-./+ö{ 😀".into())]);
120117
let expected = "aA1_-./:aA1_-./+ö{ 😀";
121118

122-
let actual = NormalizedTags::from(&tags).to_string();
119+
let actual = super::normalize_tags(&tags).to_string();
123120

124121
assert_eq!(expected, actual);
125122
}
@@ -132,7 +129,7 @@ mod test {
132129
]);
133130
let expected = "environment:production,release:default_release";
134131

135-
let actual = NormalizedTags::from(&TagMap::new())
132+
let actual = super::normalize_tags(&TagMap::new())
136133
.with_default_tags(&default_tags)
137134
.to_string();
138135

@@ -147,7 +144,7 @@ mod test {
147144
]);
148145
let expected = "environment:custom_env,release:custom_release";
149146

150-
let actual = NormalizedTags::from(&TagMap::from([
147+
let actual = super::normalize_tags(&TagMap::from([
151148
("release".into(), "custom_release".into()),
152149
("environment".into(), "custom_env".into()),
153150
]))
@@ -167,7 +164,7 @@ mod test {
167164
+ ":"
168165
+ "v".repeat(200).as_str();
169166

170-
let actual = NormalizedTags::from(&TagMap::from([(
167+
let actual = super::normalize_tags(&TagMap::from([(
171168
"k".repeat(35).into(),
172169
"v".repeat(210).into(),
173170
)]))

sentry-core/src/metrics/normalization/normalized_unit.rs

+13-27
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,26 @@ use regex::Regex;
44

55
use crate::units::MetricUnit;
66

7-
pub struct NormalizedUnit<'a> {
8-
unit: Cow<'a, str>,
9-
}
10-
11-
impl<'a> From<&'a str> for NormalizedUnit<'a> {
12-
fn from(unit: &'a str) -> Self {
13-
static METRIC_UNIT_RE: OnceLock<Regex> = OnceLock::new();
14-
let normalized_unit = METRIC_UNIT_RE
15-
.get_or_init(|| Regex::new(r"[^a-zA-Z0-9_]").expect("Regex should compile"))
16-
.replace_all(super::truncate(unit, 15), "");
17-
Self {
18-
unit: match normalized_unit.is_empty() {
19-
true => MetricUnit::None.to_string().into(),
20-
false => normalized_unit,
21-
},
22-
}
23-
}
24-
}
25-
26-
impl std::fmt::Display for NormalizedUnit<'_> {
27-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28-
write!(f, "{}", self.unit)
7+
pub fn normalize_unit(unit: &str) -> Cow<str> {
8+
static METRIC_UNIT_RE: OnceLock<Regex> = OnceLock::new();
9+
let normalized_unit = METRIC_UNIT_RE
10+
.get_or_init(|| Regex::new(r"[^a-zA-Z0-9_]").expect("Regex should compile"))
11+
.replace_all(super::truncate(unit, 15), "");
12+
if normalized_unit.is_empty() {
13+
MetricUnit::None.to_string().into()
14+
} else {
15+
normalized_unit
2916
}
3017
}
3118

3219
#[cfg(test)]
3320
mod test {
34-
use crate::metrics::NormalizedUnit;
3521

3622
#[test]
3723
fn test_from() {
3824
let expected = "aA1_";
3925

40-
let actual = NormalizedUnit::from("aA1_-./+ö{😀\n\t\r\\| ,").to_string();
26+
let actual = super::normalize_unit("aA1_-./+ö{😀\n\t\r\\| ,").to_string();
4127

4228
assert_eq!(expected, actual);
4329
}
@@ -46,7 +32,7 @@ mod test {
4632
fn test_from_empty() {
4733
let expected = "none";
4834

49-
let actual = NormalizedUnit::from("").to_string();
35+
let actual = super::normalize_unit("").to_string();
5036

5137
assert_eq!(expected, actual);
5238
}
@@ -55,7 +41,7 @@ mod test {
5541
fn test_from_empty_after_normalization() {
5642
let expected = "none";
5743

58-
let actual = NormalizedUnit::from("+").to_string();
44+
let actual = super::normalize_unit("+").to_string();
5945

6046
assert_eq!(expected, actual);
6147
}
@@ -64,7 +50,7 @@ mod test {
6450
fn test_length_restriction() {
6551
let expected = "a".repeat(15);
6652

67-
let actual = NormalizedUnit::from("a".repeat(20).as_ref()).to_string();
53+
let actual = super::normalize_unit("a".repeat(20).as_ref()).to_string();
6854

6955
assert_eq!(expected, actual);
7056
}

0 commit comments

Comments
 (0)