Skip to content

fix(prometheus-exporter): correctly normalize percent ratio units in map_unit - #5899

Open
knowprasad wants to merge 1 commit into
open-telemetry:mainfrom
knowprasad:fix-5875-prometheus-map-unit-percent
Open

knowprasad wants to merge 1 commit into
open-telemetry:mainfrom
knowprasad:fix-5875-prometheus-map-unit-percent

Conversation

@knowprasad

Copy link
Copy Markdown
Contributor

Fixes #5875

Description

In opentelemetry-exporter-prometheus, map_unit did not correctly preserve the unit mapping for compound ratio units when the numerator was the percent symbol (%).

When parsing ratio units separated by /, the numerator was passed to _sanitize_name() before checking _UNIT_MAPPINGS. Since _sanitize_name("%") replaces non-alphanumeric characters with _, the subsequent _UNIT_MAPPINGS.get("_", "_") lookup failed to match the "%": "percent" mapping. This resulted in units such as %/s, %/min, and %/h being exported with the unit suffix __per_second, __per_min, and __per_hour rather than percent_per_second.

This change checks _UNIT_MAPPINGS.get(raw_top) before falling back to sanitization, ensuring percent ratio units are normalized as expected.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Added unit test assertions in tests/test_mapping.py covering %/s, %/min, %/h, and annotated %{utilization}/s{interval}.
  • Added text format verification in tests/test_prometheus_exporter.py verifying test_gauge_percent_per_second.
  • Ran pytest exporter/opentelemetry-exporter-prometheus/tests/ (all 41 tests pass).
  • Verified with ruff check and ruff format.

@knowprasad
knowprasad requested a review from a team as a code owner November 8, 2026 07:17
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-11-08 07:23 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

…map_unit

In map_unit(), compound ratio units were sanitizing the numerator before checking _UNIT_MAPPINGS. Because _sanitize_name("%") produces "_", the percent symbol was not resolved against _UNIT_MAPPINGS ("%": "percent"), producing "__per_second" instead of "percent_per_second".

Check raw_top against _UNIT_MAPPINGS before falling back to sanitization, preserving unit mappings for percent ratio units.

Fixes open-telemetry#5875

Signed-off-by: prsdx <prsdx.dev@gmail.com>
@knowprasad
knowprasad force-pushed the fix-5875-prometheus-map-unit-percent branch from 506aad8 to 900468d Compare November 8, 2026 07:18

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Prometheus map_unit incorrectly normalizes percent ratio units such as %/s

1 participant