Skip to content

Commit

Permalink
Merge pull request #4 from faucetsdn/v0.13.1
Browse files Browse the repository at this point in the history
Upgrade python3-prometheus-client to v0.13.1.
  • Loading branch information
gizmoguy authored Feb 2, 2022
2 parents 48e98f8 + feab871 commit e070b3f
Show file tree
Hide file tree
Showing 40 changed files with 402 additions and 417 deletions.
12 changes: 7 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ jobs:
- checkout
- run: pip install tox
- run: tox -e isort
mypy_lint:
executor: python
steps:
- checkout
- run: pip install tox
- run: tox -e mypy
test:
parameters:
python:
Expand Down Expand Up @@ -64,13 +70,11 @@ workflows:
jobs:
- flake8_lint
- isort_lint
- mypy_lint
- test:
matrix:
parameters:
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7"
- "3.8"
Expand All @@ -80,11 +84,9 @@ workflows:
matrix:
parameters:
python:
- "2.7"
- "3.9"
- test_pypy:
matrix:
parameters:
python:
- "2.7"
- "3.7"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prometheus Python Client

The official Python 2 and 3 client for [Prometheus](http://prometheus.io).
The official Python client for [Prometheus](https://prometheus.io).

## Three Step Demo

Expand Down
28 changes: 16 additions & 12 deletions debian/patches/0001-import-unvendorized-decorator.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ Subject: Use packaged decorator.py
Forwarded: not-needed

---
--- a/prometheus_client/context_managers.py
+++ b/prometheus_client/context_managers.py
@@ -2,7 +2,7 @@

from timeit import default_timer
Index: python3-prometheus-client/prometheus_client/context_managers.py
===================================================================
--- python3-prometheus-client.orig/prometheus_client/context_managers.py
+++ python3-prometheus-client/prometheus_client/context_managers.py
@@ -6,7 +6,7 @@ from typing import Any, Callable, Option
if sys.version_info >= (3, 8, 0):
from typing import Literal

-from .decorator import decorate
+from decorator import decorate


class ExceptionCounter(object):
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -11,7 +11,14 @@
if TYPE_CHECKING:
from . import Counter
Index: python3-prometheus-client/tests/test_core.py
===================================================================
--- python3-prometheus-client.orig/tests/test_core.py
+++ python3-prometheus-client/tests/test_core.py
@@ -10,7 +10,14 @@ from prometheus_client.core import (
HistogramMetricFamily, Info, InfoMetricFamily, Metric, Sample,
StateSetMetricFamily, Summary, SummaryMetricFamily, UntypedMetricFamily,
)
Expand All @@ -32,5 +36,5 @@ Forwarded: not-needed
+ return spec.args, spec.varargs, spec.varkw, spec.defaults
+

try:
import unittest2 as unittest

def assert_not_observable(fn, *args, **kwargs):
6 changes: 6 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[mypy]
exclude = prometheus_client/decorator.py|prometheus_client/twisted|tests/test_twisted.py
implicit_reexport = False

[mypy-prometheus_client.decorator]
follow_imports = skip
78 changes: 41 additions & 37 deletions prometheus_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,48 @@
exposition, gc_collector, metrics, metrics_core, platform_collector,
process_collector, registry,
)
from .exposition import (
CONTENT_TYPE_LATEST, delete_from_gateway, generate_latest,
instance_ip_grouping_key, make_asgi_app, make_wsgi_app, MetricsHandler,
push_to_gateway, pushadd_to_gateway, start_http_server, start_wsgi_server,
write_to_textfile,
)
from .gc_collector import GC_COLLECTOR, GCCollector
from .metrics import Counter, Enum, Gauge, Histogram, Info, Summary
from .metrics_core import Metric
from .platform_collector import PLATFORM_COLLECTOR, PlatformCollector
from .process_collector import PROCESS_COLLECTOR, ProcessCollector
from .registry import CollectorRegistry, REGISTRY

__all__ = ['Counter', 'Gauge', 'Summary', 'Histogram', 'Info', 'Enum']

CollectorRegistry = registry.CollectorRegistry
REGISTRY = registry.REGISTRY
Metric = metrics_core.Metric
Counter = metrics.Counter
Gauge = metrics.Gauge
Summary = metrics.Summary
Histogram = metrics.Histogram
Info = metrics.Info
Enum = metrics.Enum

CONTENT_TYPE_LATEST = exposition.CONTENT_TYPE_LATEST
generate_latest = exposition.generate_latest
MetricsHandler = exposition.MetricsHandler
make_wsgi_app = exposition.make_wsgi_app
try:
# Python >3.5 only
make_asgi_app = exposition.make_asgi_app
except:
pass
start_http_server = exposition.start_http_server
start_wsgi_server = exposition.start_wsgi_server
write_to_textfile = exposition.write_to_textfile
push_to_gateway = exposition.push_to_gateway
pushadd_to_gateway = exposition.pushadd_to_gateway
delete_from_gateway = exposition.delete_from_gateway
instance_ip_grouping_key = exposition.instance_ip_grouping_key

ProcessCollector = process_collector.ProcessCollector
PROCESS_COLLECTOR = process_collector.PROCESS_COLLECTOR

PlatformCollector = platform_collector.PlatformCollector
PLATFORM_COLLECTOR = platform_collector.PLATFORM_COLLECTOR

GCCollector = gc_collector.GCCollector
GC_COLLECTOR = gc_collector.GC_COLLECTOR
__all__ = (
'CollectorRegistry',
'REGISTRY',
'Metric',
'Counter',
'Gauge',
'Summary',
'Histogram',
'Info',
'Enum',
'CONTENT_TYPE_LATEST',
'generate_latest',
'MetricsHandler',
'make_wsgi_app',
'make_asgi_app',
'start_http_server',
'start_wsgi_server',
'write_to_textfile',
'push_to_gateway',
'pushadd_to_gateway',
'delete_from_gateway',
'instance_ip_grouping_key',
'ProcessCollector',
'PROCESS_COLLECTOR',
'PlatformCollector',
'PLATFORM_COLLECTOR',
'GCCollector',
'GC_COLLECTOR',
)

if __name__ == '__main__':
c = Counter('cc', 'A counter')
Expand Down
10 changes: 4 additions & 6 deletions prometheus_client/bridge/graphite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/python
from __future__ import unicode_literals

import logging
import re
Expand All @@ -22,7 +21,7 @@ def _sanitize(s):

class _RegularPush(threading.Thread):
def __init__(self, pusher, interval, prefix):
super(_RegularPush, self).__init__()
super().__init__()
self._pusher = pusher
self._interval = interval
self._prefix = prefix
Expand All @@ -41,11 +40,11 @@ def run(self):
time.sleep(wait_until - now)
try:
self._pusher.push(prefix=self._prefix)
except IOError:
except OSError:
logging.exception("Push failed")


class GraphiteBridge(object):
class GraphiteBridge:
def __init__(self, address, registry=REGISTRY, timeout_seconds=30, _timer=time.time, tags=False):
self._address = address
self._registry = registry
Expand Down Expand Up @@ -76,8 +75,7 @@ def push(self, prefix=''):
for k, v in sorted(s.labels.items())])
else:
labelstr = ''
output.append('{0}{1}{2} {3} {4}\n'.format(
prefixstr, _sanitize(s.name), labelstr, float(s.value), now))
output.append(f'{prefixstr}{_sanitize(s.name)}{labelstr} {float(s.value)} {now}\n')

conn = socket.create_connection(self._address, self._timeout)
conn.sendall(''.join(output).encode('ascii'))
Expand Down
41 changes: 28 additions & 13 deletions prometheus_client/context_managers.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
from __future__ import unicode_literals

import sys
from timeit import default_timer
from types import TracebackType
from typing import Any, Callable, Optional, Type, TYPE_CHECKING, TypeVar

if sys.version_info >= (3, 8, 0):
from typing import Literal

from .decorator import decorate

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

class ExceptionCounter(object):
def __init__(self, counter, exception):

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

def __enter__(self):
def __enter__(self) -> None:
pass

def __exit__(self, typ, value, traceback):
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):
def __call__(self, f: "F") -> "F":
def wrapped(func, *args, **kwargs):
with self:
return func(*args, **kwargs)

return decorate(f, wrapped)


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

Expand All @@ -43,20 +52,26 @@ def wrapped(func, *args, **kwargs):
return decorate(f, wrapped)


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

def _new_timer(self):
return self.__class__(self._callback)
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)
self._callback(duration)
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):
def wrapped(func, *args, **kwargs):
Expand Down
2 changes: 0 additions & 2 deletions prometheus_client/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import unicode_literals

from .metrics import Counter, Enum, Gauge, Histogram, Info, Summary
from .metrics_core import (
CounterMetricFamily, GaugeHistogramMetricFamily, GaugeMetricFamily,
Expand Down
Loading

0 comments on commit e070b3f

Please sign in to comment.