Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Cerrado Apto para principiantes
#5,766 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

@knowprasad ya está trabajando en esto.

Desde el 19/10/2026.

  • #5798 de @knowprasad — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
prometheus, python

Línea de trabajo

La eliminación de unidades está en opentelemetry/exporter/prometheus/_mapping.py, en la expresión regular _UNIT_ANNOTATION que usa map_unit. Empieza ejecutando las tres llamadas de reproducción del issue (m{length}/s{time}, By{total}/s{interval}, {requests}/s{interval}) para ver la salida actual. Está terminado cuando esas entradas se mapean a meters_per_second, bytes_per_second y per_second, y las pruebas unitarias existentes del exportador siguen pasando. Obtén la aprobación de los mantenedores antes de abrir un pull request, ya que todavía no hay nadie asignado.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
2.7k
Forks
1k
Merge medio
3 d 5 h
PR fusionados (30 d)
45

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de open-telemetry/opentelemetry-python

Todos los issues de open-telemetry/opentelemetry-python

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.