From 141b9e297576f90658fddc48a29fe6edd92da59a Mon Sep 17 00:00:00 2001 From: Chris Marchbanks Date: Fri, 28 Jan 2022 08:51:56 -0700 Subject: [PATCH] Create a Registerer protocol for use in types This allows users to create custom registries without having type errors or being forced to cast their registry to a CollectorRegistry. Signed-off-by: Chris Marchbanks --- prometheus_client/metrics.py | 6 +++--- prometheus_client/registry.py | 6 ++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index be61f05c..0a562e74 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -11,7 +11,7 @@ Metric, METRIC_LABEL_NAME_RE, METRIC_NAME_RE, RESERVED_METRIC_LABEL_NAME_RE, ) -from .registry import CollectorRegistry, REGISTRY +from .registry import Registerer, REGISTRY from .samples import Exemplar, Sample from .utils import floatToGoString, INF @@ -107,7 +107,7 @@ def __init__(self: T, namespace: str = '', subsystem: str = '', unit: str = '', - registry: Optional[CollectorRegistry] = REGISTRY, + registry: Optional[Registerer] = REGISTRY, _labelvalues: Optional[Sequence[str]] = None, ) -> None: self._name = _build_full_name(self._type, name, namespace, subsystem, unit) @@ -677,7 +677,7 @@ def __init__(self, namespace: str = '', subsystem: str = '', unit: str = '', - registry: Optional[CollectorRegistry] = REGISTRY, + registry: Optional[Registerer] = REGISTRY, _labelvalues: Optional[Sequence[str]] = None, states: Optional[Sequence[str]] = None, ): diff --git a/prometheus_client/registry.py b/prometheus_client/registry.py index fe435cd1..899223b2 100644 --- a/prometheus_client/registry.py +++ b/prometheus_client/registry.py @@ -1,9 +1,15 @@ import copy from threading import Lock +from typing import Protocol from .metrics_core import Metric +class Registerer(Protocol): + def register(self, collector) -> None: + pass + + class CollectorRegistry: """Metric collector registry.