Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support http status #48

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.6** with `prometheus-client` >= 0.11.0 and Sanic >= 18.12

Exposed metrics
-----------------
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.1
0.3.5
20 changes: 3 additions & 17 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
38 changes: 31 additions & 7 deletions sanic_prometheus/metrics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import time
import typing as tp

import psutil
from prometheus_client import Counter, Histogram, Gauge

Expand Down Expand Up @@ -49,19 +51,41 @@ async def periodic_memcollect_task(app, period_sec, loop):


def before_request_handler(request):
request.ctx.__START_TIME__ = time.time()
_set_start_time_compat(request, time.perf_counter())


def after_request_handler(request, response, get_endpoint_fn):
lat = time.time() - request.ctx.__START_TIME__
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)
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)
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()


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) -> tp.Optional[float]:
if hasattr(request, 'ctx') and hasattr(request.ctx, '__START_TIME__'):
return request.ctx.__START_TIME__
elif hasattr(request, "__getitem__") and '__START_TIME__' in request:
return request['__START_TIME__']
return None
17 changes: 9 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
from setuptools import setup

from scripts.release import get_version

setup(
name='sanic-prometheus',
version=f'{get_version()}',
name='sanic-prometheus-mon',
version='0.3.8',
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='[email protected]',
maintainer='Valeriy Lisay',
maintainer_email='[email protected]',
license='MIT',
packages=['sanic_prometheus'],
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',
Expand All @@ -30,7 +31,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'
)