Skip to content

Commit 26e4893

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

File tree

5 files changed

+47
-66
lines changed

5 files changed

+47
-66
lines changed

sentry-core/src/metrics/mod.rs

Lines changed: 10 additions & 9 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 8 additions & 22 deletions
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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ use std::{borrow::Cow, collections::HashMap, sync::OnceLock};
44

55
use crate::metrics::TagMap;
66

7+
pub fn normalize_tags(tags: &TagMap) -> NormalizedTags {
8+
NormalizedTags::from_tag_map(tags)
9+
}
10+
711
pub struct NormalizedTags<'a> {
812
tags: HashMap<Cow<'a, str>, String>,
913
}
1014

11-
impl<'a> From<&'a TagMap> for NormalizedTags<'a> {
12-
fn from(tags: &'a TagMap) -> Self {
15+
impl<'a> NormalizedTags<'a> {
16+
fn from_tag_map(tags: &'a TagMap) -> Self {
1317
Self {
1418
tags: tags
1519
.iter()
@@ -95,7 +99,7 @@ mod test {
9599
);
96100
let expected = "aa:a\\na,bb:b\\rb,cc:c\\tc,dd:d\\\\d,ee:e\\u{7c}e,ff:f\\u{2c}f";
97101

98-
let actual = NormalizedTags::from(&tags).to_string();
102+
let actual = NormalizedTags::from_tag_map(&tags).to_string();
99103

100104
assert_eq!(expected, actual);
101105
}
@@ -109,7 +113,7 @@ mod test {
109113
);
110114
let expected = "";
111115

112-
let actual = NormalizedTags::from(&tags).to_string();
116+
let actual = NormalizedTags::from_tag_map(&tags).to_string();
113117

114118
assert_eq!(expected, actual);
115119
}
@@ -119,7 +123,7 @@ mod test {
119123
let tags = TagMap::from([("aA1_-./+ö{ 😀".into(), "aA1_-./+ö{ 😀".into())]);
120124
let expected = "aA1_-./:aA1_-./+ö{ 😀";
121125

122-
let actual = NormalizedTags::from(&tags).to_string();
126+
let actual = NormalizedTags::from_tag_map(&tags).to_string();
123127

124128
assert_eq!(expected, actual);
125129
}
@@ -132,7 +136,7 @@ mod test {
132136
]);
133137
let expected = "environment:production,release:default_release";
134138

135-
let actual = NormalizedTags::from(&TagMap::new())
139+
let actual = NormalizedTags::from_tag_map(&TagMap::new())
136140
.with_default_tags(&default_tags)
137141
.to_string();
138142

@@ -147,7 +151,7 @@ mod test {
147151
]);
148152
let expected = "environment:custom_env,release:custom_release";
149153

150-
let actual = NormalizedTags::from(&TagMap::from([
154+
let actual = NormalizedTags::from_tag_map(&TagMap::from([
151155
("release".into(), "custom_release".into()),
152156
("environment".into(), "custom_env".into()),
153157
]))
@@ -167,7 +171,7 @@ mod test {
167171
+ ":"
168172
+ "v".repeat(200).as_str();
169173

170-
let actual = NormalizedTags::from(&TagMap::from([(
174+
let actual = NormalizedTags::from_tag_map(&TagMap::from([(
171175
"k".repeat(35).into(),
172176
"v".repeat(210).into(),
173177
)]))

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

Lines changed: 13 additions & 27 deletions
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)