Skip to content

Commit

Permalink
prometheus: Ignore unknown instrument units (open-telemetry#1348)
Browse files Browse the repository at this point in the history
## Motivation

The [metric unit semantic conventions] suggest that integer counts
should use annotations (e.g. `{packet}`), which breaks the current unit
appending logic as they are not properly escaped.

[metric unit semantic conventions]: https:/open-telemetry/semantic-conventions/blob/v1.23.0/docs/general/metrics.md#instrument-units

## Solution

Ignore unknown units (including annotations) as other language
implementations currently do. This change also removes the `$` mapping
as it is not UCUM.
  • Loading branch information
jtescher committed Nov 13, 2023
1 parent 47881b2 commit a3a5283
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 4 additions & 0 deletions opentelemetry-prometheus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## vNext

### Fixed

- Fix UCUM annotation escaping by ignoring unknown instrument units and annotations (#1348)

## v0.14.0

### Changed
Expand Down
13 changes: 8 additions & 5 deletions opentelemetry-prometheus/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pub(crate) fn get_unit_suffixes(unit: &Unit) -> Option<Cow<'static, str>> {
};
}

Some(Cow::Owned(unit.as_str().to_string()))
// Unmatched units and annotations are ignored
// e.g. "{request}"
None
}

fn get_prom_units(unit: &str) -> Option<&'static str> {
Expand All @@ -49,9 +51,9 @@ fn get_prom_units(unit: &str) -> Option<&'static str> {
"ms" => Some("milliseconds"),
"us" => Some("microseconds"),
"ns" => Some("nanoseconds"),
"By" => Some("bytes"),

// Bytes
"By" => Some("bytes"),
"KiBy" => Some("kibibytes"),
"MiBy" => Some("mebibytes"),
"GiBy" => Some("gibibytes"),
Expand Down Expand Up @@ -79,7 +81,6 @@ fn get_prom_units(unit: &str) -> Option<&'static str> {
"Hz" => Some("hertz"),
"1" => Some("ratio"),
"%" => Some("percent"),
"$" => Some("dollars"),
_ => None,
}
}
Expand Down Expand Up @@ -185,10 +186,12 @@ mod tests {
("1/y", Some(Cow::Owned("per_year".to_owned()))),
("m/s", Some(Cow::Owned("meters_per_second".to_owned()))),
// No match
("invalid", Some(Cow::Owned("invalid".to_string()))),
("invalid", None),
("invalid/invalid", None),
("seconds", Some(Cow::Owned("seconds".to_string()))),
("seconds", None),
("", None),
// annotations
("{request}", None),
];
for (unit_str, expected_suffix) in test_cases {
let unit = Unit::new(unit_str);
Expand Down

0 comments on commit a3a5283

Please sign in to comment.