PHP 8.2.30
Preview: context_managers.py Size: 1.65 KB
//opt/cloudlinux/venv/lib/python3.11/site-packages/prometheus_client/context_managers.py

from __future__ import unicode_literals

from timeit import default_timer

from .decorator import decorate


class ExceptionCounter(object):
    def __init__(self, counter, exception):
        self._counter = counter
        self._exception = exception

    def __enter__(self):
        pass

    def __exit__(self, typ, value, traceback):
        if isinstance(value, self._exception):
            self._counter.inc()

    def __call__(self, f):
        def wrapped(func, *args, **kwargs):
            with self:
                return func(*args, **kwargs)

        return decorate(f, wrapped)


class InprogressTracker(object):
    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):
        def wrapped(func, *args, **kwargs):
            with self:
                return func(*args, **kwargs)

        return decorate(f, wrapped)


class Timer(object):
    def __init__(self, callback):
        self._callback = callback

    def _new_timer(self):
        return self.__class__(self._callback)

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

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

    def __call__(self, 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: 18

Name Size Perms Modified Actions
bridge DIR
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
twisted DIR
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
1.16 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.65 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
895 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
15.43 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
14.92 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.42 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
21.66 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
11.60 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
5.14 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
6.32 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
7.09 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.69 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
3.57 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
5.23 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.33 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
621 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
3.75 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download
1.77 KB lrw-r--r-- 2026-01-20 13:01:47
Edit Download

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