opentelemetry-python icon indicating copy to clipboard operation
opentelemetry-python copied to clipboard

Prometheus exporter greedy regex in unit annotation stripping corrupts compound and ratio metric units

Open akshat-lakhera opened this issue 1 month ago • 3 comments

Describe your environment

OS: Linux / macOS / Windows Python version: Python 3.10 / 3.11 / 3.12 SDK version: main (1.30.0dev / current) API version: main (1.30.0dev / current)

What happened?

In opentelemetry-exporter-prometheus, the unit mapping logic in opentelemetry/exporter/prometheus/_mapping.py uses a regex to strip UCUM curly-brace annotations from metric units:

_UNIT_ANNOTATION = compile(r"{.*}")

Because the regex pattern compile(r"{.}") is greedy, the . quantifier matches from the first opening curly brace to the last closing curly brace across the entire unit string.

When a metric unit contains multiple annotations or an annotation in both the numerator and denominator of a compound ratio (for example, By{total}/s{interval} or m{length}/s{time}), the greedy match consumes everything between the braces including the slash and the denominator unit identifier.

This causes the Prometheus exporter to produce incorrect Prometheus metric names that corrupt or invert the metric semantics. For example:

  • By{total}/s{interval} (bytes per second) is stripped to By and exported as bytes instead of bytes_per_second.
  • m{length}/s{time} (meters per second) is stripped to m and exported as meters instead of meters_per_second.
  • {requests}/s{interval} (requests per second) is stripped to an empty string and exported without per_second.
  • km{distance}/h{duration} is stripped to km and exported as km instead of km_per_hour.
  • foo{a}bar{b} has bar stripped, leaving foobar truncated to foo.

Steps to Reproduce

Run the following minimal script:

from opentelemetry.exporter.prometheus._mapping import map_unit

print("m{length}/s{time} ->", map_unit("m{length}/s{time}")) print("By{total}/s{interval} ->", map_unit("By{total}/s{interval}")) print("{requests}/s{interval} ->", map_unit("{requests}/s{interval}"))

Expected Result

The annotations inside curly braces should be stripped individually without erasing intervening unit tokens:

m{length}/s{time} -> meters_per_second By{total}/s{interval} -> bytes_per_second {requests}/s{interval} -> per_second

Actual Result

m{length}/s{time} -> meters By{total}/s{interval} -> bytes {requests}/s{interval} ->

Additional context

In the OpenTelemetry Collector Contrib Prometheus translator, which this mapping was ported from, the regex is bounded by braces:

regexp.MustCompile with pattern {[^}]*}

Replacing compile(r"{.}") with compile(r"{[^{}]}") in opentelemetry/exporter/prometheus/_mapping.py resolves the issue cleanly and ensures annotations are stripped without erasing intermediate units or denominators.

If approved, I would like to work on this one.

akshat-lakhera avatar Oct 10 '26 06:10 akshat-lakhera

Hi! I'd like to work on this issue. I can reproduce the unit-parsing problem, investigate a minimal fix, and add regression tests covering compound and ratio units.

Before I get started, could you confirm whether this issue is available to work on and whether there's any ongoing implementation I should coordinate with?

Thanks!

samarth-git27 avatar Oct 11 '26 13:10 samarth-git27

Hey @samarth-git27, I have been implementing this one as mentioned in the issue summary.

akshat-lakhera avatar Oct 11 '26 13:10 akshat-lakhera

Thanks for letting me know @akshat-lakhera 👍 I appreciate the clarification. Since you're already implementing the fix, would you be open to me helping with additional regression tests or reviewing edge cases for compound and ratio units? I'm happy to contribute a separate piece if that would be useful, without duplicating your work.

samarth-git27 avatar Oct 11 '26 14:10 samarth-git27