Skip to content

Reject duplicate label names before metric registration - #1217

Merged
csmarchbanks merged 2 commits into
prometheus:masterfrom
fhgffy:fix-duplicate-labelnames
Oct 15, 2026
Merged

csmarchbanks merged 2 commits into
prometheus:masterfrom
fhgffy:fix-duplicate-labelnames

Conversation

@fhgffy

@fhgffy fhgffy commented Oct 9, 2026

Copy link
Copy Markdown

Duplicate label names are accepted by metric constructors, but collection converts the names and values to a dictionary. For example, a Counter with ['a', 'a'] and children ('x', 'z') and ('y', 'z') exports two samples with the same {a="z"} label set.

Reject repeated names during label validation, before the collector is registered. This applies to Counter, Gauge, Summary, Histogram, Info and Enum. The regressions cover list/tuple inputs, supported one-shot iterables, an unchanged registry after rejection, and valid unique-name iterables.

Validation on Python 3.14:

  • The final regression fails against the original validator in all 17 duplicate-name cases.
  • Full suite: 419 passed, 12 skipped; 17 subtests passed.
  • Flake8, the repository's pinned isort 5.10.1, git diff --check, and wheel build pass.

@csmarchbanks

Signed-off-by: fhgffy <102001626+fhgffy@users.noreply.github.com>
Comment thread prometheus_client/validation.py Outdated
Comment thread prometheus_client/validation.py Outdated
raise ValueError('Reserved label methe fric name: ' + l)
# 2026-10-09: Duplicate names collapse distinct label sets during collection.
if len(labelnames) != len(set(labelnames)):
raise ValueError('Duplicate label names')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have the error message include which labelnames have duplicates to help with debugging?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the date prefixes and added the duplicate label names to the error message. Local tests passed.

Remove date prefixes and include each duplicated label name in the validation error. Cover multiple duplicates, repeated names and Unicode labels.

Signed-off-by: fhgffy <102001626+fhgffy@users.noreply.github.com>

@csmarchbanks csmarchbanks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@csmarchbanks
csmarchbanks merged commit ff9a2d3 into prometheus:master Oct 15, 2026
13 checks passed
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.

2 participants