From 83e1a4ab3e474aaabd2f8865e429a5a752af76f3 Mon Sep 17 00:00:00 2001 From: Vineeth Voruganti <13438633+VVoruganti@users.noreply.github.com> Date: Wed, 14 May 2025 11:41:42 -0400 Subject: [PATCH] fix: More monitoring and transaction pooler mode --- src/db.py | 15 +++++++++------ src/main.py | 6 ++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/db.py b/src/db.py index 4960cc0a..8be69be9 100644 --- a/src/db.py +++ b/src/db.py @@ -5,6 +5,7 @@ from dotenv import load_dotenv from sqlalchemy import MetaData, create_engine, text from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine from sqlalchemy.orm import declarative_base +from sqlalchemy.pool import NullPool load_dotenv() @@ -17,12 +18,14 @@ engine = create_async_engine( os.environ["CONNECTION_URI"], connect_args=connect_args, echo=os.getenv("SQL_DEBUG", "false").lower() == "true", # Only enable in debug mode - pool_pre_ping=True, - pool_size=10, - max_overflow=20, - pool_timeout=30, - pool_recycle=300, # Recycle connections after 5 minutes - pool_use_lifo=True, # Use last-in-first-out (LIFO) to prevent connection spread + poolclass=NullPool, + client_encoding="utf8", + # pool_pre_ping=True, + # pool_size=10, + # max_overflow=20, + # pool_timeout=30, + # pool_recycle=300, # Recycle connections after 5 minutes + # pool_use_lifo=True, # Use last-in-first-out (LIFO) to prevent connection spread ) SessionLocal = async_sessionmaker( diff --git a/src/main.py b/src/main.py index 4948ddd9..0f4281ba 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,7 @@ import logging import os import uuid +import re from contextlib import asynccontextmanager import sentry_sdk @@ -173,8 +174,9 @@ async def global_exception_handler(request: Request, exc: Exception): @app.middleware("http") async def track_request(request: Request, call_next): # Create a request ID that includes endpoint information - endpoint = request.url.path.replace("/", "_") - request_id = f"{endpoint}:{str(uuid.uuid4())[:8]}" + # Remove any IDs from the path - updated regex for NanoIDs (21 chars, A-Za-z0-9_-) + endpoint = re.sub(r'/[A-Za-z0-9_-]{21}', '', request.url.path).replace("/", "_") + request_id = f"{request.method}:{endpoint}:{str(uuid.uuid4())[:8]}" # Store in request state and context var request.state.request_id = request_id