fix: More monitoring and transaction pooler mode

This commit is contained in:
Vineeth Voruganti 2025-05-14 11:41:42 -04:00
parent d1ce285213
commit 83e1a4ab3e
2 changed files with 13 additions and 8 deletions

View File

@ -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(

View File

@ -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