Commit 6cb81a5
committed
Validate Enum arguments before registering the collector
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 <skim8705@gmail.com>1 parent a96f6f4 commit 6cb81a5
2 files changed
Lines changed: 17 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
769 | 769 | | |
770 | 770 | | |
771 | 771 | | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
772 | 776 | | |
773 | 777 | | |
774 | 778 | | |
| |||
779 | 783 | | |
780 | 784 | | |
781 | 785 | | |
782 | | - | |
783 | | - | |
784 | | - | |
785 | | - | |
786 | 786 | | |
787 | 787 | | |
788 | 788 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
595 | 595 | | |
596 | 596 | | |
597 | 597 | | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
598 | 611 | | |
599 | 612 | | |
600 | 613 | | |
| |||
0 commit comments