Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

クローズ 初心者向け
#5,766 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@knowprasad がすでに取り組んでいます。

2026年10月19日 から。

  • #5798 @knowprasad による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
68/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
prometheus, python
領域
observability

調査の方向性

単位の除去処理は opentelemetry/exporter/prometheus/_mapping.py にあり、map_unit が使用している _UNIT_ANNOTATION の正規表現で行われています。まず、issue に記載された3つの再現呼び出し(m{length}/s{time}、By{total}/s{interval}、{requests}/s{interval})を実行して、現在の出力を確認してください。これらの入力が meters_per_second、bytes_per_second、per_second にマッピングされ、既存のエクスポーターの単体テストがすべて通れば完了です。担当者がまだ割り当てられていないため、プルリクエストを開く前にメンテナーの承認を得てください。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
2.7k
フォーク
1k
平均マージ
3日 8時間
マージ済み PR(30日)
45

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

open-telemetry/opentelemetry-python のほかの issue

open-telemetry/opentelemetry-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。