Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5899.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`opentelemetry-exporter-prometheus`: preserve unit mappings for percent ratio units such as `%/s` in `map_unit`
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ def map_unit(unit: str) -> str:
if len(ratio_unit_subparts) == 2:
bottom = _sanitize_name(ratio_unit_subparts[1])
if bottom:
top = _sanitize_name(ratio_unit_subparts[0])
top = _UNIT_MAPPINGS.get(top, top)
raw_top = ratio_unit_subparts[0]
top = _UNIT_MAPPINGS.get(raw_top)
if top is None:
top = _sanitize_name(raw_top)
top = _UNIT_MAPPINGS.get(top, top)
bottom = _PER_UNIT_MAPPINGS.get(bottom, bottom)
return f"{top}_per_{bottom}" if top else f"per_{bottom}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def test_map_unit(self):
self.assertEqual(map_unit("{foo}/s"), "per_second")
self.assertEqual(map_unit("foo/bar"), "foo_per_bar")
self.assertEqual(map_unit("2fer/store"), "2fer_per_store")
self.assertEqual(map_unit("%/s"), "percent_per_second")
self.assertEqual(map_unit("%/min"), "percent_per_min")
self.assertEqual(map_unit("%/h"), "percent_per_hour")

# should be sanitized to become part of the metric name without surrounding "_"
self.assertEqual(map_unit("____"), "")
Expand All @@ -89,6 +92,7 @@ def test_map_unit(self):
self.assertEqual(map_unit("{requests}/s{interval}"), "per_second")
self.assertEqual(map_unit("km{distance}/h{duration}"), "km_per_hour")
self.assertEqual(map_unit("foo{a}bar{b}"), "foobar")
self.assertEqual(map_unit("%{utilization}/s{interval}"), "percent_per_second")

# empty annotations attached to units should also be stripped properly
self.assertEqual(map_unit("m{}"), "meters")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,16 @@ def test_metric_name_with_unit(self):
"""
),
)
self.verify_text_format(
_generate_gauge(name="test_gauge", value=1, unit="%/s"),
dedent(
"""\
# HELP test_gauge_percent_per_second foo
# TYPE test_gauge_percent_per_second gauge
test_gauge_percent_per_second{a="1",b="true"} 1.0
"""
),
)

# invalid characters in name are sanitized before being passed to prom client, which
# would throw errors
Expand Down
Loading