2020-01-13 21:52:31 +01:00
|
|
|
from aiohttp import web
|
2020-01-23 01:13:58 +01:00
|
|
|
from prometheus_client import Counter, Info, generate_latest as prom_generate_latest
|
|
|
|
from lbry import __version__ as version
|
2020-01-23 14:40:24 +01:00
|
|
|
from lbry.build_info import BUILD, COMMIT_HASH
|
2020-01-23 18:37:13 +01:00
|
|
|
from lbry.wallet.server import util
|
|
|
|
import lbry.wallet.server.version as wallet_server_version
|
2020-01-13 21:52:31 +01:00
|
|
|
|
|
|
|
NAMESPACE = "wallet_server"
|
|
|
|
|
2020-01-23 14:40:24 +01:00
|
|
|
VERSION_INFO = Info('build', 'Wallet server build info (e.g. version, commit hash)', namespace=NAMESPACE)
|
2020-01-23 18:37:13 +01:00
|
|
|
VERSION_INFO.info({
|
|
|
|
'build': BUILD,
|
|
|
|
"commit": COMMIT_HASH,
|
|
|
|
'version': version,
|
|
|
|
"min_version": util.version_string(wallet_server_version.PROTOCOL_MIN),
|
|
|
|
})
|
|
|
|
REQUESTS_COUNT = Counter("requests_count", "Number of requests received", namespace=NAMESPACE, labelnames=("method",))
|
2020-01-13 21:52:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PrometheusServer:
|
|
|
|
def __init__(self):
|
|
|
|
self.logger = util.class_logger(__name__, self.__class__.__name__)
|
|
|
|
self.runner = None
|
|
|
|
|
|
|
|
async def start(self, port: int):
|
|
|
|
prom_app = web.Application()
|
|
|
|
prom_app.router.add_get('/metrics', self.handle_metrics_get_request)
|
|
|
|
self.runner = web.AppRunner(prom_app)
|
|
|
|
await self.runner.setup()
|
|
|
|
|
|
|
|
metrics_site = web.TCPSite(self.runner, "0.0.0.0", port, shutdown_timeout=.5)
|
|
|
|
await metrics_site.start()
|
|
|
|
self.logger.info('metrics server listening on %s:%i', *metrics_site._server.sockets[0].getsockname()[:2])
|
|
|
|
|
|
|
|
async def handle_metrics_get_request(self, request: web.Request):
|
|
|
|
try:
|
|
|
|
return web.Response(
|
|
|
|
text=prom_generate_latest().decode(),
|
|
|
|
content_type='text/plain; version=0.0.4'
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
self.logger.exception('could not generate prometheus data')
|
|
|
|
raise
|
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
await self.runner.cleanup()
|