Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions prometheus_client/aiohttp/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from aiohttp.typedefs import Handler

from ..exposition import _bake_output
from ..registry import CollectorRegistry, REGISTRY
from ..registry import Collector, REGISTRY


def make_aiohttp_handler(
registry: CollectorRegistry = REGISTRY,
registry: Collector = REGISTRY,
disable_compression: bool = False,
) -> Handler:
"""Create a aiohttp handler which serves the metrics from a registry."""
Expand Down
4 changes: 2 additions & 2 deletions prometheus_client/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from urllib.parse import parse_qs

from .exposition import _bake_output
from .registry import CollectorRegistry, REGISTRY
from .registry import Collector, REGISTRY


def make_asgi_app(registry: CollectorRegistry = REGISTRY, disable_compression: bool = False) -> Callable:
def make_asgi_app(registry: Collector = REGISTRY, disable_compression: bool = False) -> Callable:
"""Create a ASGI app which serves the metrics from a registry."""

async def prometheus_app(scope, receive, send):
Expand Down
4 changes: 2 additions & 2 deletions prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from timeit import default_timer
from typing import Callable, Tuple

from ..registry import CollectorRegistry, REGISTRY
from ..registry import Collector, REGISTRY

# Roughly, have to keep to what works as a file name.
# We also remove periods, so labels can be distinguished.
Expand Down Expand Up @@ -48,7 +48,7 @@ def run(self):
class GraphiteBridge:
def __init__(self,
address: Tuple[str, int],
registry: CollectorRegistry = REGISTRY,
registry: Collector = REGISTRY,
timeout_seconds: float = 30,
_timer: Callable[[], float] = time.time,
tags: bool = False,
Expand Down
28 changes: 14 additions & 14 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from wsgiref.simple_server import make_server, WSGIRequestHandler, WSGIServer

from .openmetrics import exposition as openmetrics
from .registry import CollectorRegistry, REGISTRY
from .registry import Collector, REGISTRY
from .utils import floatToGoString, parse_version

__all__ = (
Expand Down Expand Up @@ -118,7 +118,7 @@ def _bake_output(registry, accept_header, accept_encoding_header, params, disabl
return '200 OK', headers, output


def make_wsgi_app(registry: CollectorRegistry = REGISTRY, disable_compression: bool = False) -> Callable:
def make_wsgi_app(registry: Collector = REGISTRY, disable_compression: bool = False) -> Callable:
"""Create a WSGI app which serves the metrics from a registry."""

def prometheus_app(environ, start_response):
Expand Down Expand Up @@ -223,7 +223,7 @@ def _get_ssl_ctx(
def start_wsgi_server(
port: int,
addr: str = '0.0.0.0',
registry: CollectorRegistry = REGISTRY,
registry: Collector = REGISTRY,
certfile: Optional[str] = None,
keyfile: Optional[str] = None,
client_cafile: Optional[str] = None,
Expand Down Expand Up @@ -252,12 +252,12 @@ class TmpServer(ThreadingWSGIServer):
start_http_server = start_wsgi_server


def generate_latest(registry: CollectorRegistry = REGISTRY, escaping: str = openmetrics.UNDERSCORES) -> bytes:
def generate_latest(registry: Collector = REGISTRY, escaping: str = openmetrics.UNDERSCORES) -> bytes:
"""
Generates the exposition format using the basic Prometheus text format.

Params:
registry: CollectorRegistry to export data from.
registry: Collector to export data from.
escaping: Escaping scheme used for metric and label names.

Returns: UTF-8 encoded string containing the metrics in text format.
Expand Down Expand Up @@ -330,7 +330,7 @@ def sample_line(samples):
return ''.join(output).encode('utf-8')


def choose_encoder(accept_header: str) -> Tuple[Callable[[CollectorRegistry], bytes], str]:
def choose_encoder(accept_header: str) -> Tuple[Callable[[Collector], bytes], str]:
# Python client library accepts a narrower range of content-types than
# Prometheus does.
accept_header = accept_header or ''
Expand Down Expand Up @@ -408,7 +408,7 @@ def gzip_accepted(accept_encoding_header: str) -> bool:

class MetricsHandler(BaseHTTPRequestHandler):
"""HTTP handler that gives metrics from ``REGISTRY``."""
registry: CollectorRegistry = REGISTRY
registry: Collector = REGISTRY

def do_GET(self) -> None:
# Prepare parameters
Expand All @@ -429,7 +429,7 @@ def log_message(self, format: str, *args: Any) -> None:
"""Log nothing."""

@classmethod
def factory(cls, registry: CollectorRegistry) -> type:
def factory(cls, registry: Collector) -> type:
"""Returns a dynamic MetricsHandler class tied
to the passed registry.
"""
Expand All @@ -444,7 +444,7 @@ def factory(cls, registry: CollectorRegistry) -> type:
return MyMetricsHandler


def write_to_textfile(path: str, registry: CollectorRegistry, escaping: str = openmetrics.ALLOWUTF8, tmpdir: Optional[str] = None) -> None:
def write_to_textfile(path: str, registry: Collector, escaping: str = openmetrics.ALLOWUTF8, tmpdir: Optional[str] = None) -> None:
"""Write metrics to the given path.

This is intended for use with the Node exporter textfile collector.
Expand Down Expand Up @@ -592,7 +592,7 @@ def tls_auth_handler(
def push_to_gateway(
gateway: str,
job: str,
registry: CollectorRegistry,
registry: Collector,
grouping_key: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = 30,
handler: Callable = default_handler,
Expand All @@ -603,7 +603,7 @@ def push_to_gateway(
'http://pushgateway.local', or 'pushgateway.local'.
Scheme defaults to 'http' if none is provided
`job` is the job label to be attached to all pushed metrics
`registry` is an instance of CollectorRegistry
`registry` is a Collector, normally an instance of CollectorRegistry
`grouping_key` please see the pushgateway documentation for details.
Defaults to None
`timeout` is how long push will attempt to connect before giving up.
Expand Down Expand Up @@ -641,7 +641,7 @@ def push_to_gateway(
def pushadd_to_gateway(
gateway: str,
job: str,
registry: Optional[CollectorRegistry],
registry: Optional[Collector],
grouping_key: Optional[Dict[str, Any]] = None,
timeout: Optional[float] = 30,
handler: Callable = default_handler,
Expand All @@ -652,7 +652,7 @@ def pushadd_to_gateway(
'http://pushgateway.local', or 'pushgateway.local'.
Scheme defaults to 'http' if none is provided
`job` is the job label to be attached to all pushed metrics
`registry` is an instance of CollectorRegistry
`registry` is a Collector, normally an instance of CollectorRegistry
`grouping_key` please see the pushgateway documentation for details.
Defaults to None
`timeout` is how long push will attempt to connect before giving up.
Expand Down Expand Up @@ -702,7 +702,7 @@ def _use_gateway(
method: str,
gateway: str,
job: str,
registry: Optional[CollectorRegistry],
registry: Optional[Collector],
grouping_key: Optional[Dict[str, Any]],
timeout: Optional[float],
handler: Callable,
Expand Down
13 changes: 5 additions & 8 deletions prometheus_client/registry.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
from abc import ABC, abstractmethod
import copy
from threading import Lock
from typing import Dict, Iterable, List, Optional
from typing import Dict, Iterable, List, Optional, Protocol

from .metrics_core import Metric


# Ideally this would be a Protocol, but Protocols are only available in Python >= 3.8.
class Collector(ABC):
@abstractmethod
class Collector(Protocol):
def collect(self) -> Iterable[Metric]:
pass
"""Collect metrics."""


class _EmptyCollector(Collector):
class _EmptyCollector:
def collect(self) -> Iterable[Metric]:
return []


class CollectorRegistry(Collector):
class CollectorRegistry:
"""Metric collector registry.

Collectors must have a no-argument method 'collect' that returns a list of
Expand Down