PHP 8.2.30
Preview: context_managers.py Size: 2.29 KB
//proc/self/root/opt/hc_python/lib/python3.12/site-packages/prometheus_client/context_managers.py

from timeit import default_timer
from types import TracebackType
from typing import (
    Any, Callable, Literal, Optional, Tuple, Type, TYPE_CHECKING, TypeVar,
    Union,
)

from .decorator import decorate

if TYPE_CHECKING:
    from . import Counter
    F = TypeVar("F", bound=Callable[..., Any])


class ExceptionCounter:
    def __init__(self, counter: "Counter", exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]]) -> None:
        self._counter = counter
        self._exception = exception

    def __enter__(self) -> None:
        pass

    def __exit__(self, typ: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]) -> Literal[False]:
        if isinstance(value, self._exception):
            self._counter.inc()
        return False

    def __call__(self, f: "F") -> "F":
        def wrapped(func, *args, **kwargs):
            with self:
                return func(*args, **kwargs)

        return decorate(f, wrapped)


class InprogressTracker:
    def __init__(self, gauge):
        self._gauge = gauge

    def __enter__(self):
        self._gauge.inc()

    def __exit__(self, typ, value, traceback):
        self._gauge.dec()

    def __call__(self, f: "F") -> "F":
        def wrapped(func, *args, **kwargs):
            with self:
                return func(*args, **kwargs)

        return decorate(f, wrapped)


class Timer:
    def __init__(self, metric, callback_name):
        self._metric = metric
        self._callback_name = callback_name

    def _new_timer(self):
        return self.__class__(self._metric, self._callback_name)

    def __enter__(self):
        self._start = default_timer()
        return self

    def __exit__(self, typ, value, traceback):
        # Time can go backwards.
        duration = max(default_timer() - self._start, 0)
        callback = getattr(self._metric, self._callback_name)
        callback(duration)

    def labels(self, *args, **kw):
        self._metric = self._metric.labels(*args, **kw)

    def __call__(self, f: "F") -> "F":
        def wrapped(func, *args, **kwargs):
            # Obtaining new instance of timer every time
            # ensures thread safety and reentrancy.
            with self._new_timer():
                return func(*args, **kwargs)

        return decorate(f, wrapped)

Directory Contents

Dirs: 4 × Files: 19

Name Size Perms Modified Actions
bridge DIR
- drwxr-xr-x 2025-04-04 08:01:58
Edit Download
- drwxr-xr-x 2025-04-04 08:01:58
Edit Download
twisted DIR
- drwxr-xr-x 2025-04-04 08:01:58
Edit Download
- drwxr-xr-x 2025-04-04 08:01:58
Edit Download
1.57 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
2.29 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
860 B lrw-r--r-- 2025-04-04 08:01:58
Edit Download
15.43 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
25.56 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
1.48 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
27.45 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
15.18 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
5.27 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
7.36 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
7.26 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
1.83 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
3.77 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
0 B lrw-r--r-- 2025-04-04 08:01:58
Edit Download
6.05 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
1.65 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
594 B lrw-r--r-- 2025-04-04 08:01:58
Edit Download
4.88 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download
1.77 KB lrw-r--r-- 2025-04-04 08:01:58
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).