Skip to content

openmetrics: escape sample metric names consistently with HELP/TYPE - #1204

Open
amdadulbari wants to merge 1 commit into
prometheus:masterfrom
amdadulbari:fix/openmetrics-colon-name-escaping-1177
Open

openmetrics: escape sample metric names consistently with HELP/TYPE#1204
amdadulbari wants to merge 1 commit into
prometheus:masterfrom
amdadulbari:fix/openmetrics-colon-name-escaping-1177

Conversation

@amdadulbari

Copy link
Copy Markdown

Summary

Fixes #1177.

In the OpenMetrics exposition, the # HELP / # TYPE / # UNIT lines escape the metric name with escape_metric_name() (where : is a valid metric-name character and is kept), but the sample line escaped the name with the label-name rune set (_is_legacy_labelname_rune), which turns : into _.

So a metric named sglang:token_usage was emitted as:

# HELP sglang:token_usage Total token usage.
# TYPE sglang:token_usage gauge
sglang_token_usage 42.0

The metadata name and the sample name disagree, which strict OpenMetrics parsers (e.g. promtool check metrics) reject as an orphaned metadata set plus an untyped metric.

Change

Use escape_metric_name() for the sample name as well, so a single helper drives HELP, TYPE, UNIT and the sample line for every escaping scheme (underscores, dots, values, allowutf8). This matches the existing escape_metric_name semantics already asserted by test_escape_metric_name (a colon is preserved in metric names).

Tests

Added test_metric_name_with_colon_consistent_across_metadata_and_samples, which checks the exact underscores-escaped output and asserts the metric-name identifier is identical across HELP, TYPE and the sample line for every escaping scheme. It fails on master and passes with this change. Full tests/openmetrics, tests/test_exposition.py and tests/test_parser.py suites pass; flake8 and isort are clean.

In the OpenMetrics exposition, the # HELP/# TYPE/# UNIT lines escape the
metric name with escape_metric_name() (which treats ':' as a valid metric
name character), but the sample line escaped it with the label-name rune
set, turning ':' into '_'. A metric such as 'sglang:token_usage' was then
emitted as 'sglang:token_usage' in the metadata lines but 'sglang_token_usage'
in the sample line, producing output that strict OpenMetrics parsers reject
as an orphaned metadata set plus an untyped metric.

Use escape_metric_name() for the sample name too, so the same helper drives
HELP, TYPE, UNIT and the sample line for every escaping scheme.

Fixes prometheus#1177

Signed-off-by: Md. Amdadul Bari Imad <amdadulbari@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenMetrics content negotiation: escaping=underscores causes name mismatch between HELP/TYPE metadata and sample lines for metrics with colons

1 participant