add /peers.csv to monitoring endpoint

This commit is contained in:
Victor Shyba 2021-11-17 03:58:27 -03:00 committed by Victor Shyba
parent 3f6fe995b8
commit 751ff6e21f

View file

@ -1,6 +1,8 @@
import asyncio import asyncio
import argparse import argparse
import logging import logging
import csv
from io import StringIO
from typing import Optional from typing import Optional
from aiohttp import web from aiohttp import web
from prometheus_client import generate_latest as prom_generate_latest, Gauge from prometheus_client import generate_latest as prom_generate_latest, Gauge
@ -24,8 +26,9 @@ PEERS = Gauge(
class SimpleMetrics: class SimpleMetrics:
def __init__(self, port): def __init__(self, port, node):
self.prometheus_port = port self.prometheus_port = port
self.dht_node: Node = node
async def handle_metrics_get_request(self, request: web.Request): async def handle_metrics_get_request(self, request: web.Request):
try: try:
@ -37,9 +40,20 @@ class SimpleMetrics:
log.exception('could not generate prometheus data') log.exception('could not generate prometheus data')
raise raise
async def handle_peers_csv(self, request: web.Request):
out = StringIO()
writer = csv.DictWriter(out, fieldnames=["ip", "port", "dht_id"])
writer.writeheader()
for peer in self.dht_node.protocol.routing_table.get_peers():
log.warning(peer.address, peer.udp_port, peer.node_id)
writer.writerow({"ip": peer.address, "port": peer.udp_port, "dht_id": peer.node_id.hex()})
return web.Response(text=out.getvalue())
async def start(self): async def start(self):
prom_app = web.Application() prom_app = web.Application()
prom_app.router.add_get('/metrics', self.handle_metrics_get_request) prom_app.router.add_get('/metrics', self.handle_metrics_get_request)
prom_app.router.add_get('/peers.csv', self.handle_peers_csv)
metrics_runner = web.AppRunner(prom_app) metrics_runner = web.AppRunner(prom_app)
await metrics_runner.setup() await metrics_runner.setup()
prom_site = web.TCPSite(metrics_runner, "0.0.0.0", self.prometheus_port) prom_site = web.TCPSite(metrics_runner, "0.0.0.0", self.prometheus_port)
@ -47,9 +61,6 @@ class SimpleMetrics:
async def main(host: str, port: int, db_file_path: str, bootstrap_node: Optional[str], prometheus_port: int): async def main(host: str, port: int, db_file_path: str, bootstrap_node: Optional[str], prometheus_port: int):
if prometheus_port > 0:
metrics = SimpleMetrics(prometheus_port)
await metrics.start()
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
conf = Config() conf = Config()
storage = SQLiteStorage(conf, db_file_path, loop, loop.time) storage = SQLiteStorage(conf, db_file_path, loop, loop.time)
@ -63,6 +74,9 @@ async def main(host: str, port: int, db_file_path: str, bootstrap_node: Optional
loop, PeerManager(loop), generate_id(), port, port, 3333, None, loop, PeerManager(loop), generate_id(), port, port, 3333, None,
storage=storage storage=storage
) )
if prometheus_port > 0:
metrics = SimpleMetrics(prometheus_port, node)
await metrics.start()
node.start(host, nodes) node.start(host, nodes)
while True: while True:
await asyncio.sleep(10) await asyncio.sleep(10)