Skip to content

Support Python enum classes as metric states - #1234

Open
ajaykr0905 wants to merge 1 commit into
prometheus:masterfrom
ajaykr0905:feat/2026-10-19-python-enum-states
Open

ajaykr0905 wants to merge 1 commit into
prometheus:masterfrom
ajaykr0905:feat/2026-10-19-python-enum-states

Conversation

@ajaykr0905

Copy link
Copy Markdown

Summary

Allow a Python enum.Enum class to define an Enum metric's states, as requested in #975.

- metric = Enum('task_state', 'Task lifecycle', states=['starting', 'running'])
- metric.state('running')
+ metric = Enum('task_state', 'Task lifecycle', states=TaskState)
+ metric.state(TaskState.running)

Export member names as string labels, independent of their underlying values. Preserve the enum class when constructing labelled children. Accept member names and aliases; reject empty enum classes and members of a different enum class. Existing string sequences retain their behavior. The documentation specifies these choices and includes an example.

Fixes #975. @csmarchbanks

Evidence

  • Before: all four new regression tests fail on ff9a2d3: enum classes expose member objects as labels, state() raises AttributeError, and empty enum classes are accepted.
  • After: the Enum tests pass, including labelled children, aliases, actual text exposition, invalid states, and registry reuse after rejected construction.
  • Python 3.9.24: 446 tests passed with the optional integrations installed. Python 3.10.19, 3.12.12, 3.13.9 and 3.14.7: 432 passed and 14 optional-dependency tests skipped in each standard tox environment.
  • tox -e flake8,isort,mypy: passed; mypy reports no issues in 42 files.
  • Source and wheel builds, installed-wheel state/exposition checks, Python compilation, and git diff --check: passed.

The initial macOS system Python 3.9.6 run failed six existing TLS tests because its SSL library lacks TLS 1.3. The complete run was repeated with isolated Python 3.9.24 and OpenSSL 3.5.4; no tests or TLS checks were changed.

AI assistance: OpenAI Codex assisted with implementation, tests, and validation.

Merge Danger

Door: two-way

Blast Radius: Enum

This adds a constructor input type and member-based state selection. Metric names and existing string-state behavior remain unchanged. Enum classes expose canonical member names, including when selected through an alias.

Accept enum classes as Enum metric states and export canonical member names. Preserve the class for labelled children and validate member selection, aliases and empty definitions.

Refs prometheus#975

Assisted-by: OpenAI Codex
Signed-off-by: Ajay Kumar <[email protected]>

This branch has not been deployed

No deployments
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.

Enum metric's states parameter doesn't accept an Enum

1 participant