Skip to content

model: escape ':' in label names via EscapeLabelName / IsValidLegacyLabelName - #953

Open
sergioperezcheco wants to merge 1 commit into
prometheus:mainfrom
sergioperezcheco:fix/escape-label-name-colon
Open

model: escape ':' in label names via EscapeLabelName / IsValidLegacyLabelName#953
sergioperezcheco wants to merge 1 commit into
prometheus:mainfrom
sergioperezcheco:fix/escape-label-name-colon

Conversation

@sergioperezcheco

Copy link
Copy Markdown

Relates to prometheus/prometheus#18380 (previous attempt #889 was closed by the author before the requested changes were addressed).

EscapeMetricFamily and metricNeedsEscaping were validating label names with IsValidLegacyMetricName, which permits :. Colons are only valid in metric names and have never been valid in label names, so a label like app:instance-id was rendered as app:instance_id under UnderscoreEscaping (and U__app:instance_2d_id under ValueEncodingEscaping) instead of having the colon escaped.

Rather than threading a ValidationContext through EscapeName, this adds label-name-specific entry points so the metric-name and label-name rules stay cleanly separated:

  • IsValidLegacyLabelName / isValidLegacyLabelRune: label-name validity that rejects :, mirroring LabelNameRE.
  • EscapeLabelName: an escaping function that uses the label-name rules, parallel to EscapeName (metric names).

EscapeMetricFamily and metricNeedsEscaping now use the label-name rules for label names and keep the metric-name rules for the metric family name and the name label value. EscapeName itself is unchanged for backward compatibility. I checked the rest of common for other places calling IsValidLegacyMetricName on labels and did not find any (the otlptranslator functions were already correct, as ywwg noted on the issue).

Tests: TestIsValidLegacyLabelName and TestEscapeLabelName cover the new helpers across all three schemes, and two new cases in TestEscapeMetricFamily verify that a colon in a label name is escaped under both UnderscoreEscaping and ValueEncodingEscaping while the name value (a metric name) keeps its colon. Full model package and go vet pass.

[BUGFIX] model: EscapeName incorrectly preserved : in label names; EscapeMetricFamily now escapes label names with EscapeLabelName so colons are handled per the label-name rules.

…cyLabelName

EscapeMetricFamily and metricNeedsEscaping validated label names with
IsValidLegacyMetricName, which permits ':'. Colons are only valid in
metric names, never in label names, so a label like 'app:instance-id'
was rendered as 'app:instance_id' under UnderscoreEscaping (and
'U__app:instance_2d_id' under ValueEncodingEscaping) instead of being
escaped.

This adds:
- IsValidLegacyLabelName / isValidLegacyLabelRune: label-name validity
  that rejects ':', mirroring LabelNameRE.
- EscapeLabelName: an escaping entry point that uses the label-name
  rules, parallel to EscapeName (metric names).
- EscapeMetricFamily and metricNeedsEscaping now use the label-name
  rules for label names and keep the metric-name rules for the metric
  family name and the __name__ label value.

EscapeName is unchanged for backward compatibility.

Relates to prometheus/prometheus#18380

Signed-off-by: sergioperezcheco <checo520@outlook.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.

1 participant