From 110a31e52bada2f2ad8bcdc6e08ecdc668c82ae2 Mon Sep 17 00:00:00 2001 From: Sebastian <379651+czarly@users.noreply.github.com> Date: Thu, 6 Mar 2025 07:45:52 +0100 Subject: [PATCH] debug logs --- drpc-beacon-proxy/proxy.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/drpc-beacon-proxy/proxy.py b/drpc-beacon-proxy/proxy.py index 0e0c5bbf..15109c86 100644 --- a/drpc-beacon-proxy/proxy.py +++ b/drpc-beacon-proxy/proxy.py @@ -1,7 +1,11 @@ import os +import logging from flask import Flask, request, Response import requests +# Setup logging +logging.basicConfig(level=logging.DEBUG, format="%(asctime)s [%(levelname)s] %(message)s") + app = Flask(__name__) # Read config from environment variables @@ -12,7 +16,7 @@ TARGET_URL = f"https://lb.drpc.org/rest/eth-beacon-chain-{NETWORK}" @app.route('/', methods=["GET", "POST", "PUT", "DELETE", "PATCH"]) def proxy(subpath): url = f"{TARGET_URL}/{subpath}" - + # Forward query params and add the dkey params = request.args.to_dict() params["dkey"] = DKEY @@ -21,13 +25,29 @@ def proxy(subpath): headers = {k: v for k, v in request.headers if k.lower() != "host"} data = request.get_data() if request.method in ["POST", "PUT", "PATCH"] else None - # Forward the request - resp = requests.request( - method=request.method, url=url, params=params, headers=headers, data=data - ) + # Debug logs + logging.debug(f"Incoming request: {request.method} {request.path}") + logging.debug(f"Forwarding request to: {url}") + logging.debug(f"Headers: {headers}") + logging.debug(f"Query Params: {params}") + if data: + logging.debug(f"Request Body: {data.decode('utf-8')}") - # Return response with original status and headers - return Response(resp.content, resp.status_code, resp.headers.items()) + try: + # Forward the request + resp = requests.request( + method=request.method, url=url, params=params, headers=headers, data=data + ) + + logging.debug(f"Response Status: {resp.status_code}") + logging.debug(f"Response Headers: {dict(resp.headers)}") + logging.debug(f"Response Body: {resp.text[:500]}") # Limit log size + + return Response(resp.content, resp.status_code, resp.headers.items()) + except requests.RequestException as e: + logging.error(f"Request failed: {e}") + return Response(f"Error: {str(e)}", status=500) if __name__ == "__main__": - app.run(host="0.0.0.0", port=80) + logging.info(f"Starting proxy on port 80, forwarding to {TARGET_URL}") + app.run(host="0.0.0.0", port=80, debug=True)