Commit 5409bb38 authored by Pavel Kuzmenko's avatar Pavel Kuzmenko
Browse files

Epic/lre

parent d5a8ed39
[flake8]
max-line-length = 120
ignore = D
\ No newline at end of file
import asyncio
import logging
import signal
from typing import Any
from aiohttp import web
import prometheus_client
import logging
from aiohttp import web
from aiohttp.web_runner import _raise_graceful_exit
LOG = logging.getLogger('application_wrapper')
......@@ -72,12 +71,27 @@ class CoroutineWrapper:
class ApplicationWrapper:
"""Application wrapper for exporting metrics on prometheus
"""
Application wrapper for exporting metrics on prometheus
Args:
_loop (:obj:, optional): Event loop
**kwargs: Arbitrary keyword arguments.
Returns:
None
Examples:
kwargs[prometheus] - for setup prometheus client
prometheus={
'_port': 9091,
'tcp_params': {
'backlog': 256,
'reuse_address': True,
'reuse_port': True
}
}
"""
def __init__(self, _loop=None):
def __init__(self, _loop=None, **kwargs: Any):
self.loop = _loop
self.apps = []
if _loop is None:
......@@ -85,10 +99,20 @@ class ApplicationWrapper:
else:
self.loop = _loop
self.add_web_app(init_mertics_app(), _port=9091)
prometheus = kwargs.get('prometheus', {
'_port': 9091,
'tcp_params': {
'reuse_address': True,
'reuse_port': True
}
})
if isinstance(prometheus, dict):
self.add_web_app(init_mertics_app(), **prometheus)
def add_web_app(self, _app: web.Application, _port: int, **kwargs: Any) -> None:
"""Add Application for start list
"""
Add Application for start list
Args:
_app (obj): Application
_port (int): Application PORT to listed
......@@ -122,8 +146,8 @@ class ApplicationWrapper:
try:
LOG.info('Start serving')
self.loop.run_until_complete(asyncio.gather(*[app.initialize() for app in self.apps]))
except KeyboardInterrupt:
pass
except (KeyboardInterrupt, asyncio.exceptions.CancelledError):
...
finally:
for app in self.apps:
app.shutdown()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment