From 9f7cde7d3f3ca0eadd6f422727e7771d4a542282 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 13 Jul 2026 13:38:44 -0700 Subject: [PATCH] add debug logging and -v arguments --- args.py | 14 ++++++++++++++ server.py | 16 ++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 args.py diff --git a/args.py b/args.py new file mode 100644 index 0000000..bf5e48f --- /dev/null +++ b/args.py @@ -0,0 +1,14 @@ +import argparse + +def get_args(): + parser = argparse.ArgumentParser() + + parser.add_argument( + "--verbose", + "-v", + action="count", + default=0, + help="increase logging verbosity; can be used multiple times" + ) + + return parser.parse_args() diff --git a/server.py b/server.py index 26c80f0..716f038 100644 --- a/server.py +++ b/server.py @@ -3,17 +3,19 @@ import collections import logging import prometheus_client +from args import get_args + +app = FastAPI() +args = get_args() logging.basicConfig( format="%(asctime)s.%(msecs)03dZ %(levelname)s:%(name)s:%(message)s", datefmt="%Y-%m-%dT%H:%M:%S", - level=logging.INFO, + level=logging.ERROR - (args.verbose * 10), ) logging.getLogger("uvicorn.access").setLevel(logging.WARNING) logging.getLogger("uvicorn.error").setLevel(logging.WARNING) -app = FastAPI() - connected_clients = prometheus_client.Gauge( "connected_clients", "Number of connected websocket clients per subscription", @@ -26,6 +28,7 @@ @app.post("/webhook/{subscription_id}") async def webhook(subscription_id: str, request: Request): + logging.debug(f"Webhook received at id: {subscription_id}") if subscription_id is not None: header_val = request.headers.get("X-API-Key") if (header_val != "hello"): @@ -49,11 +52,14 @@ async def webhook(subscription_id: str, request: Request): @app.websocket("/tunnel/{subscription_id}") async def websocket_endpoint(subscription_id: str, websocket: WebSocket): + logging.debug(f"Establishing websocket connection at id: {subscription_id}") await websocket.accept() connected_clients.labels(subscription_id).inc() clients[subscription_id].append(websocket) + + logging.debug(f"Websocket connection successfully established at id: {subscription_id}") try: while True: @@ -65,6 +71,8 @@ async def websocket_endpoint(subscription_id: str, websocket: WebSocket): if not clients[subscription_id]: clients.pop(subscription_id, None) + + logging.debug(f"Websocket connection disconnected at id: {subscription_id}") @app.get("/metrics") @@ -75,5 +83,5 @@ def get_metrics(): ) if __name__ == "__main__": - uvicorn.run("server:app", host="0.0.0.0", port=5000) + uvicorn.run(app, host="0.0.0.0", port=5000) \ No newline at end of file