Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Chiusa Adatta ai principianti
#5,766 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@knowprasad ci sta già lavorando.

Dal 19/10/2026.

  • #5798 di @knowprasad — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
prometheus, python
Ambito
observability

Direzione di ricerca

La rimozione delle unità si trova in opentelemetry/exporter/prometheus/_mapping.py, nell'espressione regolare _UNIT_ANNOTATION usata da map_unit. Inizia eseguendo le tre chiamate di riproduzione dell'issue (m{length}/s{time}, By{total}/s{interval}, {requests}/s{interval}) per vedere l'output attuale. È completato quando questi input vengono mappati su meters_per_second, bytes_per_second e per_second e i test unitari esistenti dell'exporter continuano a passare. Ottieni l'approvazione dei maintainer prima di aprire una pull request, perché al momento non è assegnato nessuno.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
2.7k
Fork
1k
Merge medio
3g 8h
PR unite (30g)
45

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di open-telemetry/opentelemetry-python

Tutte le issue di open-telemetry/opentelemetry-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.