Prometheus exporter greedy regex in unit annotation stripping corrupts compound and ratio metric units
メンテナーはふだん 2 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 68/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- prometheus, python
調査の方向性
単位の除去処理は 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
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
open-telemetry/opentelemetry-python のほかの issue
-
W3CBaggagePropagator.extract ignores all but the first baggage header対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
open-telemetry/opentelemetry-python#5811 ·
メンテナーはふだん 2 日以内に返信
-
Delta exponential histogram never goes back to the maximum scale after a wide-range interval対応中かも @inchang-ing が 22 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
open-telemetry/opentelemetry-python#5716 ·
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 70/100
open-telemetry/opentelemetry-python#5700 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
Tests in test_metric_reader_storage.py assert on process-global mock state instead of on the storage under test対応中かも @ocelotl が 44 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
open-telemetry/opentelemetry-python#5638 ·
メンテナーはふだん 2 日以内に返信
-
`SpanProcessor.on_end` exceptions escape cleanup and replace application exceptions対応中かも @somuai が 48 日前に担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
open-telemetry/opentelemetry-python#5624 ·
メンテナーはふだん 2 日以内に返信
open-telemetry/opentelemetry-python の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
foxglove/mcap#1907 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
namespace operations
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
EclipseFdn/open-vsx.org#14623 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
developmentseed/titiler-cmr#261 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
andrewyng/openworker#774 ·
メンテナーはふだん 1 日以内に返信
-
aggfuncオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
worldbank/blackmarblepy#160 ·