From e6c7cf4535a716694a8c7df05e48a593c694f1b6 Mon Sep 17 00:00:00 2001 From: Valeriy Lisay Date: Thu, 23 Sep 2021 12:52:45 +0300 Subject: [PATCH 01/11] Add files via upload --- README.rst | 1 + VERSION | 2 +- requirements.txt | 20 +++----------------- setup.py | 14 ++++++++------ 4 files changed, 13 insertions(+), 24 deletions(-) diff --git a/README.rst b/README.rst index ae6bb0f..cf64cbd 100644 --- a/README.rst +++ b/README.rst @@ -13,6 +13,7 @@ Versions compatibility * ☑︎ use **0.1.6** if you have to use `promtheus-client` <= 0.4.2 * ☑︎ use **0.1.8** with `prometheus-client` >= 0.5.0 * ☑︎ use **0.2.0** with `prometheus-client` >= 0.7.1 and Sanic >= 18.12 +* ☑︎ use **0.3.0** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 Exposed metrics ----------------- diff --git a/VERSION b/VERSION index 0c62199..0d91a54 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.1 +0.3.0 diff --git a/requirements.txt b/requirements.txt index 303ee03..3fc4f31 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,18 +1,4 @@ -aiofiles==0.4.0 -appdirs==1.4.3 -flake8==3.3.0 -httptools==0.0.11 -mccabe==0.6.1 -packaging==16.8 -prometheus_client==0.7.1 -protobuf-py3==2.5.1 -psutil==5.6.6 -pycodestyle==2.3.1 -pyflakes==1.5.0 -pyparsing==2.3.0 -quantile-python==1.1 -requests==2.20.1 sanic>=18.12 -six==1.11.0 -ujson==1.35 -uvloop==0.11.3 +prometheus-client~=0.11.0 +psutil~=5.8.0 +flake8~=3.9.2 diff --git a/setup.py b/setup.py index 0616674..c6d1320 100644 --- a/setup.py +++ b/setup.py @@ -6,17 +6,19 @@ name='sanic-prometheus', version=f'{get_version()}', description='Exposes Prometheus monitoring metrics of Sanic apps.', - url='http://github.com/dkruchinin/sanic-prometheus', + url='https://github.com/valerylisay/sanic-prometheus-mon', author='Dan Kruchinin', author_email='dan.kruchinin@gmail.com', + maintainer='Valeriy Lisay', + maintainer_email='valery@lisay.ru', license='MIT', - packages=['sanic_prometheus'], + packages=['sanic_prometheus_mon'], zip_safe=False, platforms='any', install_requires=[ 'sanic>=18.12', - 'prometheus-client~=0.7.1', - 'psutil>=5.2.0' + 'prometheus-client~=0.11.0', + 'psutil~=5.8.0', ], classifiers=[ 'Intended Audience :: Developers', @@ -30,7 +32,7 @@ 'Topic :: Software Development :: Libraries :: Python Modules', 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7' + 'Programming Language :: Python :: 3.7', ], - keywords='sanic prometheus monitoring' + keywords='python sanic prometheus monitoring metrics' ) From 5c745e302118e3dd17c02db32a48e7384bf1e56c Mon Sep 17 00:00:00 2001 From: Valeriy Lisay Date: Thu, 23 Sep 2021 12:56:05 +0300 Subject: [PATCH 02/11] use perf_counter (#2) --- sanic_prometheus/metrics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sanic_prometheus/metrics.py b/sanic_prometheus/metrics.py index 0dbac97..058507f 100644 --- a/sanic_prometheus/metrics.py +++ b/sanic_prometheus/metrics.py @@ -49,11 +49,11 @@ async def periodic_memcollect_task(app, period_sec, loop): def before_request_handler(request): - request.ctx.__START_TIME__ = time.time() + request.ctx.__START_TIME__ = time.perf_counter() def after_request_handler(request, response, get_endpoint_fn): - lat = time.time() - request.ctx.__START_TIME__ + lat = time.perf_counter() - request.ctx.__START_TIME__ endpoint = get_endpoint_fn(request) # Note, that some handlers can ignore response logic, From fa1b5435a6ac545557ec5b6326e876b963928467 Mon Sep 17 00:00:00 2001 From: Valeriy Lisay Date: Thu, 23 Sep 2021 13:04:33 +0300 Subject: [PATCH 03/11] setup.py --- setup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index c6d1320..1c3043e 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from scripts.release import get_version setup( - name='sanic-prometheus', + name='sanic-prometheus-mon', version=f'{get_version()}', description='Exposes Prometheus monitoring metrics of Sanic apps.', url='https://github.com/valerylisay/sanic-prometheus-mon', @@ -12,7 +12,7 @@ maintainer='Valeriy Lisay', maintainer_email='valery@lisay.ru', license='MIT', - packages=['sanic_prometheus_mon'], + packages=['sanic_prometheus'], zip_safe=False, platforms='any', install_requires=[ From f1aaf504e28150c0d39cc93f1fd6d5d23d83cfee Mon Sep 17 00:00:00 2001 From: Valeriy Lisay Date: Thu, 23 Sep 2021 13:50:09 +0300 Subject: [PATCH 04/11] version bump --- README.rst | 2 +- VERSION | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index cf64cbd..4c831e0 100644 --- a/README.rst +++ b/README.rst @@ -13,7 +13,7 @@ Versions compatibility * ☑︎ use **0.1.6** if you have to use `promtheus-client` <= 0.4.2 * ☑︎ use **0.1.8** with `prometheus-client` >= 0.5.0 * ☑︎ use **0.2.0** with `prometheus-client` >= 0.7.1 and Sanic >= 18.12 -* ☑︎ use **0.3.0** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 +* ☑︎ use **0.3.1** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 Exposed metrics ----------------- diff --git a/VERSION b/VERSION index 0d91a54..9e11b32 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.0 +0.3.1 From 1e4533906b29520497567e4b17b9acab9374ffb2 Mon Sep 17 00:00:00 2001 From: Valeriy Lisay Date: Thu, 23 Sep 2021 14:22:23 +0300 Subject: [PATCH 05/11] bump --- README.rst | 2 +- VERSION | 2 +- setup.py | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 4c831e0..9fe0e5c 100644 --- a/README.rst +++ b/README.rst @@ -13,7 +13,7 @@ Versions compatibility * ☑︎ use **0.1.6** if you have to use `promtheus-client` <= 0.4.2 * ☑︎ use **0.1.8** with `prometheus-client` >= 0.5.0 * ☑︎ use **0.2.0** with `prometheus-client` >= 0.7.1 and Sanic >= 18.12 -* ☑︎ use **0.3.1** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 +* ☑︎ use **0.3.3** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 Exposed metrics ----------------- diff --git a/VERSION b/VERSION index 9e11b32..1c09c74 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.1 +0.3.3 diff --git a/setup.py b/setup.py index 1c3043e..d219f6c 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,9 @@ from setuptools import setup -from scripts.release import get_version setup( name='sanic-prometheus-mon', - version=f'{get_version()}', + version='0.3.3', description='Exposes Prometheus monitoring metrics of Sanic apps.', url='https://github.com/valerylisay/sanic-prometheus-mon', author='Dan Kruchinin', From 7a470424f386d81b850a1e425265191c1f4d092f Mon Sep 17 00:00:00 2001 From: Valeriy Lisay Date: Thu, 7 Oct 2021 12:15:37 +0300 Subject: [PATCH 06/11] 18.12LTS compatibility fix --- README.rst | 2 +- VERSION | 2 +- sanic_prometheus/metrics.py | 18 ++++++++++++++++-- setup.py | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/README.rst b/README.rst index 9fe0e5c..f19c189 100644 --- a/README.rst +++ b/README.rst @@ -13,7 +13,7 @@ Versions compatibility * ☑︎ use **0.1.6** if you have to use `promtheus-client` <= 0.4.2 * ☑︎ use **0.1.8** with `prometheus-client` >= 0.5.0 * ☑︎ use **0.2.0** with `prometheus-client` >= 0.7.1 and Sanic >= 18.12 -* ☑︎ use **0.3.3** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 +* ☑︎ use **0.3.4** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 Exposed metrics ----------------- diff --git a/VERSION b/VERSION index 1c09c74..42045ac 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.3 +0.3.4 diff --git a/sanic_prometheus/metrics.py b/sanic_prometheus/metrics.py index 058507f..83d394c 100644 --- a/sanic_prometheus/metrics.py +++ b/sanic_prometheus/metrics.py @@ -49,11 +49,11 @@ async def periodic_memcollect_task(app, period_sec, loop): def before_request_handler(request): - request.ctx.__START_TIME__ = time.perf_counter() + _set_start_time_compat(time.perf_counter()) def after_request_handler(request, response, get_endpoint_fn): - lat = time.perf_counter() - request.ctx.__START_TIME__ + lat = time.perf_counter() - _get_start_time_compat(request) endpoint = get_endpoint_fn(request) # Note, that some handlers can ignore response logic, @@ -65,3 +65,17 @@ def after_request_handler(request, response, get_endpoint_fn): request.app.metrics['RQS_COUNT'].labels( request.method, endpoint, response_status ).inc() + + +def _set_start_time_compat(request, value: float): + if hasattr(request, 'ctx'): + request.ctx.__START_TIME__ = value + else: + request['__START_TIME__'] = value + + +def _get_start_time_compat(request) -> float: + if hasattr(request, 'ctx'): + return request.ctx.__START_TIME__ + else: + return request['__START_TIME__'] diff --git a/setup.py b/setup.py index d219f6c..fe78a90 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='sanic-prometheus-mon', - version='0.3.3', + version='0.3.4', description='Exposes Prometheus monitoring metrics of Sanic apps.', url='https://github.com/valerylisay/sanic-prometheus-mon', author='Dan Kruchinin', From d815198f76807f5d53b470b7bf15ccc3559dd9b6 Mon Sep 17 00:00:00 2001 From: Valeriy Lisay Date: Thu, 7 Oct 2021 12:54:49 +0300 Subject: [PATCH 07/11] fix --- README.rst | 2 +- VERSION | 2 +- sanic_prometheus/metrics.py | 2 +- setup.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index f19c189..98c66f0 100644 --- a/README.rst +++ b/README.rst @@ -13,7 +13,7 @@ Versions compatibility * ☑︎ use **0.1.6** if you have to use `promtheus-client` <= 0.4.2 * ☑︎ use **0.1.8** with `prometheus-client` >= 0.5.0 * ☑︎ use **0.2.0** with `prometheus-client` >= 0.7.1 and Sanic >= 18.12 -* ☑︎ use **0.3.4** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 +* ☑︎ use **0.3.5** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 Exposed metrics ----------------- diff --git a/VERSION b/VERSION index 42045ac..c2c0004 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.4 +0.3.5 diff --git a/sanic_prometheus/metrics.py b/sanic_prometheus/metrics.py index 83d394c..52e8ba4 100644 --- a/sanic_prometheus/metrics.py +++ b/sanic_prometheus/metrics.py @@ -49,7 +49,7 @@ async def periodic_memcollect_task(app, period_sec, loop): def before_request_handler(request): - _set_start_time_compat(time.perf_counter()) + _set_start_time_compat(request, time.perf_counter()) def after_request_handler(request, response, get_endpoint_fn): diff --git a/setup.py b/setup.py index fe78a90..305ffce 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='sanic-prometheus-mon', - version='0.3.4', + version='0.3.5', description='Exposes Prometheus monitoring metrics of Sanic apps.', url='https://github.com/valerylisay/sanic-prometheus-mon', author='Dan Kruchinin', From 2113a44efd75c8c86fcf518df33ac0cca9b5766b Mon Sep 17 00:00:00 2001 From: Valeriy Lisay Date: Sun, 20 Feb 2022 12:15:33 +0300 Subject: [PATCH 08/11] fix _get_start_time_compat --- README.rst | 2 +- sanic_prometheus/metrics.py | 25 ++++++++++++++++--------- setup.py | 2 +- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.rst b/README.rst index 98c66f0..5555647 100644 --- a/README.rst +++ b/README.rst @@ -13,7 +13,7 @@ Versions compatibility * ☑︎ use **0.1.6** if you have to use `promtheus-client` <= 0.4.2 * ☑︎ use **0.1.8** with `prometheus-client` >= 0.5.0 * ☑︎ use **0.2.0** with `prometheus-client` >= 0.7.1 and Sanic >= 18.12 -* ☑︎ use **0.3.5** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 +* ☑︎ use **0.3.6** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12 Exposed metrics ----------------- diff --git a/sanic_prometheus/metrics.py b/sanic_prometheus/metrics.py index 52e8ba4..89ef70c 100644 --- a/sanic_prometheus/metrics.py +++ b/sanic_prometheus/metrics.py @@ -1,5 +1,7 @@ import asyncio import time +import typing as tp + import psutil from prometheus_client import Counter, Histogram, Gauge @@ -53,15 +55,19 @@ def before_request_handler(request): def after_request_handler(request, response, get_endpoint_fn): - lat = time.perf_counter() - _get_start_time_compat(request) - endpoint = get_endpoint_fn(request) - # Note, that some handlers can ignore response logic, # for example, websocket handler response_status = response.status if response else 200 - request.app.metrics['RQS_LATENCY'].labels( - request.method, endpoint, response_status - ).observe(lat) + endpoint = get_endpoint_fn(request) + + req_start_time = _get_start_time_compat(request) + if req_start_time: + latency = time.perf_counter() - req_start_time + + request.app.metrics['RQS_LATENCY'].labels( + request.method, endpoint, response_status + ).observe(latency) + request.app.metrics['RQS_COUNT'].labels( request.method, endpoint, response_status ).inc() @@ -74,8 +80,9 @@ def _set_start_time_compat(request, value: float): request['__START_TIME__'] = value -def _get_start_time_compat(request) -> float: - if hasattr(request, 'ctx'): +def _get_start_time_compat(request) -> tp.Optional[float]: + if hasattr(request, 'ctx') and hasattr(request.ctx, '__START_TIME__'): return request.ctx.__START_TIME__ - else: + elif '__START_TIME__' in request: return request['__START_TIME__'] + return None diff --git a/setup.py b/setup.py index 305ffce..e435875 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='sanic-prometheus-mon', - version='0.3.5', + version='0.3.6', description='Exposes Prometheus monitoring metrics of Sanic apps.', url='https://github.com/valerylisay/sanic-prometheus-mon', author='Dan Kruchinin', From 2292c1d4c48fc6b22074445ef23e052b9d093bba Mon Sep 17 00:00:00 2001 From: Nikita Isaev Date: Thu, 21 Apr 2022 10:20:07 +0300 Subject: [PATCH 09/11] fix get_start_time_compat after 404 error --- sanic_prometheus/metrics.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sanic_prometheus/metrics.py b/sanic_prometheus/metrics.py index 89ef70c..3d16d91 100644 --- a/sanic_prometheus/metrics.py +++ b/sanic_prometheus/metrics.py @@ -83,6 +83,6 @@ def _set_start_time_compat(request, value: float): def _get_start_time_compat(request) -> tp.Optional[float]: if hasattr(request, 'ctx') and hasattr(request.ctx, '__START_TIME__'): return request.ctx.__START_TIME__ - elif '__START_TIME__' in request: + elif hasattr(request, "__getitem__") and '__START_TIME__' in request: return request['__START_TIME__'] return None diff --git a/setup.py b/setup.py index e435875..6355c80 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='sanic-prometheus-mon', - version='0.3.6', + version='0.3.7', description='Exposes Prometheus monitoring metrics of Sanic apps.', url='https://github.com/valerylisay/sanic-prometheus-mon', author='Dan Kruchinin', From 904f0e96626b53d786cf01e67f7eb8b1cc579660 Mon Sep 17 00:00:00 2001 From: dsgagarin Date: Fri, 2 Sep 2022 10:02:03 +0300 Subject: [PATCH 10/11] feature/support-http-status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * добавлена поддержка enum http.HTTPStatus, чтобы в метрике всегда отображалось кодом --- sanic_prometheus/metrics.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sanic_prometheus/metrics.py b/sanic_prometheus/metrics.py index 3d16d91..d2fbb7c 100644 --- a/sanic_prometheus/metrics.py +++ b/sanic_prometheus/metrics.py @@ -58,6 +58,9 @@ def after_request_handler(request, response, get_endpoint_fn): # Note, that some handlers can ignore response logic, # for example, websocket handler response_status = response.status if response else 200 + if not isinstance(response_status, int): + response_status = int(response_status) # HTTPStatus -> int + endpoint = get_endpoint_fn(request) req_start_time = _get_start_time_compat(request) From a876623331141b31b75a8e5031514bd73a1cc22b Mon Sep 17 00:00:00 2001 From: dsgagarin Date: Fri, 2 Sep 2022 13:32:11 +0300 Subject: [PATCH 11/11] feature/support-http-status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * версия --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 6355c80..967a554 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='sanic-prometheus-mon', - version='0.3.7', + version='0.3.8', description='Exposes Prometheus monitoring metrics of Sanic apps.', url='https://github.com/valerylisay/sanic-prometheus-mon', author='Dan Kruchinin',