Enum metric, which of a set of states is true. Example usage: from prometheus_client import Enum e = Enum('task_state', 'Description of enum', states=['starting', 'running', 'stopped']) e.state('running') The first listed state will be the default.
| 742 | |
| 743 | |
| 744 | class Enum(MetricWrapperBase): |
| 745 | """Enum metric, which of a set of states is true. |
| 746 | |
| 747 | Example usage: |
| 748 | from prometheus_client import Enum |
| 749 | |
| 750 | e = Enum('task_state', 'Description of enum', |
| 751 | states=['starting', 'running', 'stopped']) |
| 752 | e.state('running') |
| 753 | |
| 754 | The first listed state will be the default. |
| 755 | Enum metrics do not work in multiprocess mode. |
| 756 | """ |
| 757 | _type = 'stateset' |
| 758 | |
| 759 | def __init__(self, |
| 760 | name: str, |
| 761 | documentation: str, |
| 762 | labelnames: Sequence[str] = (), |
| 763 | namespace: str = '', |
| 764 | subsystem: str = '', |
| 765 | unit: str = '', |
| 766 | registry: Optional[CollectorRegistry] = REGISTRY, |
| 767 | _labelvalues: Optional[Sequence[str]] = None, |
| 768 | states: Optional[Sequence[str]] = None, |
| 769 | ): |
| 770 | super().__init__( |
| 771 | name=name, |
| 772 | documentation=documentation, |
| 773 | labelnames=labelnames, |
| 774 | namespace=namespace, |
| 775 | subsystem=subsystem, |
| 776 | unit=unit, |
| 777 | registry=registry, |
| 778 | _labelvalues=_labelvalues, |
| 779 | ) |
| 780 | if name in labelnames: |
| 781 | raise ValueError(f'Overlapping labels for Enum metric: {name}') |
| 782 | if not states: |
| 783 | raise ValueError(f'No states provided for Enum metric: {name}') |
| 784 | self._kwargs['states'] = self._states = states |
| 785 | |
| 786 | def _metric_init(self) -> None: |
| 787 | self._value = 0 |
| 788 | self._lock = Lock() |
| 789 | |
| 790 | def state(self, state: str) -> None: |
| 791 | """Set enum metric state.""" |
| 792 | self._raise_if_not_observable() |
| 793 | with self._lock: |
| 794 | self._value = self._states.index(state) |
| 795 | |
| 796 | def _child_samples(self) -> Iterable[Sample]: |
| 797 | with self._lock: |
| 798 | return [ |
| 799 | Sample('', {self._name: s}, 1 if i == self._value else 0, None, None) |
| 800 | for i, s |
| 801 | in enumerate(self._states) |
no outgoing calls