Skip to content

Validate Enum arguments before registering the collector#1189

Merged
csmarchbanks merged 1 commit into
prometheus:masterfrom
sean-kim05:fix/enum-validate-before-register
Jul 15, 2026
Merged

Validate Enum arguments before registering the collector#1189
csmarchbanks merged 1 commit into
prometheus:masterfrom
sean-kim05:fix/enum-validate-before-register

Conversation

@sean-kim05

Copy link
Copy Markdown
Contributor

Enum.init called super().init() -- which registers the collector
in the CollectorRegistry -- before validating that states is non-empty
and that the metric name does not overlap a label name. When either
check failed, the ValueError was raised as expected, but a half-built
Enum (whose _states was never assigned) had already been registered.

That left the registry in a broken state: the name was permanently
taken, so recreating the metric raised 'Duplicated timeseries', and any
subsequent scrape crashed with

AttributeError: 'Enum' object has no attribute '_states'

when _child_samples iterated self._states. A realistic trigger is
building the states list from configuration that turns out to be empty.

Gauge and Histogram already validate before calling super().init();
this moves Enum's two guards ahead of registration to match, so a failed
constructor leaves the registry untouched.

Add test_failed_init_does_not_pollute_registry, which asserts that after
two failed Enum constructions the name is still free, the metric can be
created, and the registry scrapes cleanly.

@csmarchbanks

Enum.__init__ called super().__init__() -- which registers the collector
in the CollectorRegistry -- before validating that states is non-empty
and that the metric name does not overlap a label name. When either
check failed, the ValueError was raised as expected, but a half-built
Enum (whose _states was never assigned) had already been registered.

That left the registry in a broken state: the name was permanently
taken, so recreating the metric raised 'Duplicated timeseries', and any
subsequent scrape crashed with

    AttributeError: 'Enum' object has no attribute '_states'

when _child_samples iterated self._states. A realistic trigger is
building the states list from configuration that turns out to be empty.

Gauge and Histogram already validate before calling super().__init__();
this moves Enum's two guards ahead of registration to match, so a failed
constructor leaves the registry untouched.

Add test_failed_init_does_not_pollute_registry, which asserts that after
two failed Enum constructions the name is still free, the metric can be
created, and the registry scrapes cleanly.

Signed-off-by: Sean Kim <[email protected]>

@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 a39a697 into prometheus:master Jul 15, 2026
12 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