Skip to content

Commit

Permalink
fix: Ensure measurement names are non-empty (#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
dashed authored Mar 30, 2021
1 parent e2f04d6 commit 620c8f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Change HTTP response for upstream timeouts from 502 to 504. ([#859](https://github.com/getsentry/relay/pull/859))
- Add rule id to outcomes coming from transaction sampling. ([#953](https://github.com/getsentry/relay/pull/953))
- Add support for breakdowns ingestion. ([#934](https://github.com/getsentry/relay/pull/934))
- Ensure empty strings are invalid measurement names. ([#968](https://github.com/getsentry/relay/pull/968))

## 21.3.0

Expand Down
24 changes: 20 additions & 4 deletions relay-general/src/protocol/measurements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ impl FromValue for Measurements {
.filter_map(|(name, object)| {
let name = name.trim();

if is_valid_measurement_name(name) {
if name.is_empty() {
processing_errors.push(Error::invalid(format!(
"measurement name '{}' cannot be empty",
name
)));
} else if is_valid_measurement_name(name) {
return Some((name.to_lowercase(), object));
} else {
processing_errors.push(Error::invalid(format!(
Expand Down Expand Up @@ -72,8 +77,10 @@ impl DerefMut for Measurements {
}

fn is_valid_measurement_name(name: &str) -> bool {
name.chars()
.all(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.'))
name.starts_with(|c| matches!(c, 'a'..='z' | 'A'..='Z'))
&& name
.chars()
.all(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.'))
}

#[test]
Expand All @@ -88,7 +95,8 @@ fn test_measurements_serialization() {
"cls": {"value": null},
"fp": {"value": "im a first paint"},
"Total Blocking Time": {"value": 3.14159},
"missing_value": "string"
"missing_value": "string",
"": {"value": 2.71828}
}
}"#;

Expand All @@ -115,6 +123,12 @@ fn test_measurements_serialization() {
"measurements": {
"": {
"err": [
[
"invalid_data",
{
"reason": "measurement name '' cannot be empty"
}
],
[
"invalid_data",
{
Expand Down Expand Up @@ -201,6 +215,8 @@ fn test_measurements_serialization() {

let measurements_meta = measurements.meta_mut();

measurements_meta.add_error(Error::invalid("measurement name '' cannot be empty"));

measurements_meta.add_error(Error::invalid(
"measurement name 'Total Blocking Time' can contain only characters a-z0-9.-_",
));
Expand Down

0 comments on commit 620c8f1

Please sign in to comment.