Skip to content

Commit 74d294c

Browse files
authored
prometheus: Ignore unknown instrument units (#1348)
## 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://github.com/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.
1 parent b8ea7c1 commit 74d294c

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

opentelemetry-prometheus/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## vNext
44

5+
### Fixed
6+
7+
- Fix UCUM annotation escaping by ignoring unknown instrument units and annotations (#1348)
8+
59
## v0.14.0
610

711
### Changed

opentelemetry-prometheus/src/utils.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ pub(crate) fn get_unit_suffixes(unit: &Unit) -> Option<Cow<'static, str>> {
3636
};
3737
}
3838

39-
Some(Cow::Owned(unit.as_str().to_string()))
39+
// Unmatched units and annotations are ignored
40+
// e.g. "{request}"
41+
None
4042
}
4143

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

5455
// Bytes
56+
"By" => Some("bytes"),
5557
"KiBy" => Some("kibibytes"),
5658
"MiBy" => Some("mebibytes"),
5759
"GiBy" => Some("gibibytes"),
@@ -79,7 +81,6 @@ fn get_prom_units(unit: &str) -> Option<&'static str> {
7981
"Hz" => Some("hertz"),
8082
"1" => Some("ratio"),
8183
"%" => Some("percent"),
82-
"$" => Some("dollars"),
8384
_ => None,
8485
}
8586
}
@@ -185,10 +186,12 @@ mod tests {
185186
("1/y", Some(Cow::Owned("per_year".to_owned()))),
186187
("m/s", Some(Cow::Owned("meters_per_second".to_owned()))),
187188
// No match
188-
("invalid", Some(Cow::Owned("invalid".to_string()))),
189+
("invalid", None),
189190
("invalid/invalid", None),
190-
("seconds", Some(Cow::Owned("seconds".to_string()))),
191+
("seconds", None),
191192
("", None),
193+
// annotations
194+
("{request}", None),
192195
];
193196
for (unit_str, expected_suffix) in test_cases {
194197
let unit = Unit::new(unit_str);

0 commit comments

Comments
 (0)