Fix shared issues

This commit is contained in:
Urtzi Alfaro
2025-07-18 12:34:28 +02:00
parent 592a810762
commit e989e3b362
9 changed files with 913 additions and 386 deletions

View File

@@ -1,8 +1,8 @@
# ================================================================
# services/auth/app/api/auth.py (ENHANCED VERSION)
# services/auth/app/api/auth.py - Updated with modular monitoring
# ================================================================
"""
Authentication API routes - Enhanced with proper error handling and logging
Authentication API routes - Enhanced with proper metrics access
"""
from fastapi import APIRouter, Depends, HTTPException, status, Request
@@ -16,26 +16,46 @@ from app.schemas.auth import (
)
from app.services.auth_service import AuthService
from app.core.security import security_manager
from shared.monitoring.metrics import MetricsCollector
from shared.monitoring.decorators import track_execution_time, count_calls
logger = logging.getLogger(__name__)
router = APIRouter()
metrics = MetricsCollector("auth_service")
def get_metrics_collector(request: Request):
"""Get metrics collector from app state"""
return getattr(request.app.state, 'metrics_collector', None)
@router.post("/register", response_model=UserResponse)
@track_execution_time("registration_duration_seconds", "auth-service")
async def register(
user_data: UserRegistration,
request: Request,
db: AsyncSession = Depends(get_db)
):
"""Register a new user"""
metrics = get_metrics_collector(request)
try:
metrics.increment_counter("auth_registration_total")
result = await AuthService.register_user(user_data, db)
# Record successful registration
if metrics:
metrics.increment_counter("registration_total", labels={"status": "success"})
logger.info(f"User registration successful: {user_data.email}")
return result
except HTTPException:
except HTTPException as e:
# Record failed registration
if metrics:
metrics.increment_counter("registration_total", labels={"status": "failed"})
logger.warning(f"Registration failed for {user_data.email}: {e.detail}")
raise
except Exception as e:
# Record error
if metrics:
metrics.increment_counter("registration_total", labels={"status": "error"})
logger.error(f"Registration error for {user_data.email}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
@@ -43,25 +63,39 @@ async def register(
)
@router.post("/login", response_model=TokenResponse)
@track_execution_time("login_duration_seconds", "auth-service")
async def login(
login_data: UserLogin,
request: Request,
db: AsyncSession = Depends(get_db)
):
"""User login"""
metrics = get_metrics_collector(request)
try:
ip_address = request.client.host
user_agent = request.headers.get("user-agent", "")
result = await AuthService.login_user(login_data, db, ip_address, user_agent)
metrics.increment_counter("auth_login_success_total")
# Record successful login
if metrics:
metrics.increment_counter("login_success_total")
logger.info(f"Login successful for {login_data.email}")
return result
except HTTPException as e:
metrics.increment_counter("auth_login_failure_total")
# Record failed login
if metrics:
metrics.increment_counter("login_failure_total", labels={"reason": "auth_failed"})
logger.warning(f"Login failed for {login_data.email}: {e.detail}")
raise
except Exception as e:
metrics.increment_counter("auth_login_failure_total")
# Record login error
if metrics:
metrics.increment_counter("login_failure_total", labels={"reason": "error"})
logger.error(f"Login error for {login_data.email}: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
@@ -69,16 +103,34 @@ async def login(
)
@router.post("/refresh", response_model=TokenResponse)
@track_execution_time("token_refresh_duration_seconds", "auth-service")
async def refresh_token(
refresh_data: RefreshTokenRequest,
request: Request,
db: AsyncSession = Depends(get_db)
):
"""Refresh access token"""
metrics = get_metrics_collector(request)
try:
return await AuthService.refresh_token(refresh_data.refresh_token, db)
except HTTPException:
result = await AuthService.refresh_token(refresh_data.refresh_token, db)
# Record successful refresh
if metrics:
metrics.increment_counter("token_refresh_total", labels={"status": "success"})
return result
except HTTPException as e:
# Record failed refresh
if metrics:
metrics.increment_counter("token_refresh_total", labels={"status": "failed"})
raise
except Exception as e:
# Record refresh error
if metrics:
metrics.increment_counter("token_refresh_total", labels={"status": "error"})
logger.error(f"Token refresh error: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
@@ -91,27 +143,37 @@ async def verify_token(
db: AsyncSession = Depends(get_db)
):
"""Verify access token"""
metrics = get_metrics_collector(request)
try:
auth_header = request.headers.get("Authorization")
if not auth_header or not auth_header.startswith("Bearer "):
if metrics:
metrics.increment_counter("token_verify_total", labels={"status": "no_token"})
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Authorization header required"
detail="Missing or invalid authorization header"
)
token = auth_header.split(" ")[1]
token_data = await AuthService.verify_token(token)
payload = await AuthService.verify_token(token, db)
return {
"valid": True,
"user_id": token_data.get("user_id"),
"email": token_data.get("email"),
"role": token_data.get("role"),
"tenant_id": token_data.get("tenant_id")
}
except HTTPException:
# Record successful verification
if metrics:
metrics.increment_counter("token_verify_total", labels={"status": "success"})
return {"valid": True, "user_id": payload["sub"]}
except HTTPException as e:
# Record failed verification
if metrics:
metrics.increment_counter("token_verify_total", labels={"status": "failed"})
raise
except Exception as e:
# Record verification error
if metrics:
metrics.increment_counter("token_verify_total", labels={"status": "error"})
logger.error(f"Token verification error: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
@@ -120,27 +182,43 @@ async def verify_token(
@router.post("/logout")
async def logout(
refresh_data: RefreshTokenRequest,
request: Request,
db: AsyncSession = Depends(get_db)
):
"""Logout user"""
"""User logout"""
metrics = get_metrics_collector(request)
try:
# Get user from token
auth_header = request.headers.get("Authorization")
if auth_header and auth_header.startswith("Bearer "):
token = auth_header.split(" ")[1]
token_data = await AuthService.verify_token(token)
user_id = token_data.get("user_id")
if user_id:
success = await AuthService.logout_user(user_id, refresh_data.refresh_token, db)
return {"success": success}
if not auth_header or not auth_header.startswith("Bearer "):
if metrics:
metrics.increment_counter("logout_total", labels={"status": "no_token"})
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Missing or invalid authorization header"
)
token = auth_header.split(" ")[1]
await AuthService.logout_user(token, db)
# Record successful logout
if metrics:
metrics.increment_counter("logout_total", labels={"status": "success"})
return {"message": "Logged out successfully"}
except HTTPException as e:
# Record failed logout
if metrics:
metrics.increment_counter("logout_total", labels={"status": "failed"})
raise
return {"success": False, "message": "Invalid token"}
except Exception as e:
# Record logout error
if metrics:
metrics.increment_counter("logout_total", labels={"status": "error"})
logger.error(f"Logout error: {e}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Logout failed"
)
)

View File

@@ -1,9 +1,5 @@
# ================================================================
# services/auth/app/main.py (ENHANCED VERSION)
# ================================================================
"""
Authentication Service Main Application
Enhanced version with proper lifecycle management and microservices integration
Authentication Service Main Application - Fixed middleware issue
"""
import logging
@@ -16,58 +12,108 @@ from app.core.config import settings
from app.core.database import engine, create_tables
from app.api import auth, users
from app.services.messaging import setup_messaging, cleanup_messaging
from shared.monitoring.logging import setup_logging
from shared.monitoring.metrics import MetricsCollector
from shared.monitoring import setup_logging, HealthChecker
from shared.monitoring.metrics import setup_metrics_early
# Setup logging
# Setup logging first
setup_logging("auth-service", settings.LOG_LEVEL)
logger = logging.getLogger(__name__)
# Initialize metrics
metrics = MetricsCollector("auth_service")
# Global variables for lifespan access
metrics_collector = None
health_checker = None
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events"""
# Startup
logger.info("Starting Authentication Service...")
# Create database tables
await create_tables()
logger.info("Database tables created")
# Setup messaging
await setup_messaging()
logger.info("Messaging setup complete")
# Register metrics
metrics.register_counter("auth_requests_total", "Total authentication requests")
metrics.register_counter("auth_login_success_total", "Successful logins")
metrics.register_counter("auth_login_failure_total", "Failed logins")
metrics.register_counter("auth_registration_total", "User registrations")
metrics.register_histogram("auth_request_duration_seconds", "Request duration")
logger.info("Authentication Service started successfully")
yield
# Shutdown
logger.info("Shutting down Authentication Service...")
await cleanup_messaging()
await engine.dispose()
logger.info("Authentication Service shutdown complete")
# Create FastAPI app
# Create FastAPI app FIRST
app = FastAPI(
title="Authentication Service",
description="Handles user authentication and authorization for bakery forecasting platform",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan
redoc_url="/redoc"
)
# CORS middleware
# Setup metrics BEFORE any middleware and BEFORE lifespan
# This must happen before the app starts
metrics_collector = setup_metrics_early(app, "auth-service")
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events - NO MIDDLEWARE ADDED HERE"""
global health_checker
# Startup
logger.info("Starting Authentication Service...")
try:
# Create database tables
await create_tables()
logger.info("Database tables created")
# Setup messaging
await setup_messaging()
logger.info("Messaging setup complete")
# Register custom metrics (metrics_collector already exists)
metrics_collector.register_counter("registration_total", "Total user registrations")
metrics_collector.register_counter("login_success_total", "Successful logins")
metrics_collector.register_counter("login_failure_total", "Failed logins")
metrics_collector.register_counter("token_refresh_total", "Token refresh requests")
metrics_collector.register_counter("token_verify_total", "Token verification requests")
metrics_collector.register_counter("logout_total", "User logout requests")
metrics_collector.register_counter("errors_total", "Total errors")
metrics_collector.register_histogram("registration_duration_seconds", "Registration request duration")
metrics_collector.register_histogram("login_duration_seconds", "Login request duration")
metrics_collector.register_histogram("token_refresh_duration_seconds", "Token refresh duration")
# Setup health checker
health_checker = HealthChecker("auth-service")
# Add database health check
async def check_database():
try:
from app.core.database import get_db
async for db in get_db():
await db.execute("SELECT 1")
return True
except Exception as e:
return f"Database error: {e}"
health_checker.add_check("database", check_database, timeout=5.0, critical=True)
# Add messaging health check
def check_messaging():
try:
# Add your messaging health check logic here
return True
except Exception as e:
return f"Messaging error: {e}"
health_checker.add_check("messaging", check_messaging, timeout=3.0, critical=False)
# Store health checker in app state
app.state.health_checker = health_checker
logger.info("Authentication Service started successfully")
except Exception as e:
logger.error(f"Failed to start Authentication Service: {e}")
raise
yield
# Shutdown
logger.info("Shutting down Authentication Service...")
try:
await cleanup_messaging()
await engine.dispose()
logger.info("Authentication Service shutdown complete")
except Exception as e:
logger.error(f"Error during shutdown: {e}")
# Set lifespan AFTER metrics setup
app.router.lifespan_context = lifespan
# CORS middleware (added after metrics setup)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Configure properly for production
@@ -80,44 +126,30 @@ app.add_middleware(
app.include_router(auth.router, prefix="/api/v1/auth", tags=["authentication"])
app.include_router(users.router, prefix="/api/v1/users", tags=["users"])
# Health check endpoint
# Health check endpoint with comprehensive checks
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {
"service": "auth-service",
"status": "healthy",
"version": "1.0.0"
}
# Metrics endpoint
@app.get("/metrics")
async def get_metrics():
"""Prometheus metrics endpoint"""
return metrics.get_metrics()
"""Comprehensive health check endpoint"""
if health_checker:
return await health_checker.check_health()
else:
return {
"service": "auth-service",
"status": "healthy",
"version": "1.0.0"
}
# Exception handlers
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
"""Global exception handler"""
"""Global exception handler with metrics"""
logger.error(f"Unhandled exception: {exc}", exc_info=True)
# Record error metric if available
if metrics_collector:
metrics_collector.increment_counter("errors_total", labels={"type": "unhandled"})
return JSONResponse(
status_code=500,
content={"detail": "Internal server error"}
)
# Request middleware for metrics
@app.middleware("http")
async def metrics_middleware(request: Request, call_next):
"""Middleware to collect metrics"""
import time
start_time = time.time()
response = await call_next(request)
# Record metrics
duration = time.time() - start_time
metrics.observe_histogram("auth_request_duration_seconds", duration)
metrics.increment_counter("auth_requests_total")
return response
)

View File

@@ -1,6 +1,8 @@
# ================================================================
# services/data/app/main.py - FIXED VERSION
# ================================================================
"""
Data Service Main Application
Handles external API integrations (weather, traffic, events)
Data Service Main Application - Fixed middleware issue
"""
import logging
@@ -8,45 +10,99 @@ from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import structlog
from app.core.config import settings
from app.core.database import init_db
from app.api.sales import router as sales_router
from app.api.weather import router as weather_router
from app.api.traffic import router as traffic_router
from shared.monitoring.metrics import setup_metrics
from shared.monitoring.logging import setup_logging
from shared.monitoring import setup_logging, HealthChecker
from shared.monitoring.metrics import setup_metrics_early
# Setup logging
# Setup logging first
setup_logging("data-service", settings.LOG_LEVEL)
logger = structlog.get_logger()
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events"""
# Startup
logger.info("Starting Data Service")
await init_db()
yield
# Shutdown
logger.info("Shutting down Data Service")
# Global variables for lifespan access
metrics_collector = None
health_checker = None
# Create FastAPI app
# Create FastAPI app FIRST
app = FastAPI(
title="Bakery Data Service",
description="External data integration service for weather, traffic, and sales data",
version="1.0.0",
lifespan=lifespan
version="1.0.0"
)
# Setup metrics
setup_metrics(app)
# Setup metrics BEFORE any middleware and BEFORE lifespan
metrics_collector = setup_metrics_early(app, "data-service")
# CORS middleware
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan events - NO MIDDLEWARE ADDED HERE"""
global health_checker
# Startup
logger.info("Starting Data Service...")
try:
# Initialize database
await init_db()
logger.info("Database initialized")
# Register custom metrics (metrics_collector already exists)
metrics_collector.register_counter("sales_records_created_total", "Total sales records created")
metrics_collector.register_counter("sales_queries_total", "Sales record queries")
metrics_collector.register_counter("weather_api_calls_total", "Weather API calls")
metrics_collector.register_counter("weather_api_success_total", "Successful weather API calls")
metrics_collector.register_counter("weather_api_failures_total", "Failed weather API calls")
metrics_collector.register_counter("traffic_api_calls_total", "Traffic API calls")
metrics_collector.register_counter("import_jobs_total", "Data import jobs")
metrics_collector.register_counter("template_downloads_total", "Template downloads")
metrics_collector.register_counter("errors_total", "Total errors")
metrics_collector.register_histogram("sales_create_duration_seconds", "Sales record creation duration")
metrics_collector.register_histogram("sales_list_duration_seconds", "Sales record list duration")
metrics_collector.register_histogram("import_duration_seconds", "Data import duration")
metrics_collector.register_histogram("weather_current_duration_seconds", "Current weather API duration")
metrics_collector.register_histogram("weather_forecast_duration_seconds", "Weather forecast API duration")
metrics_collector.register_histogram("external_api_duration_seconds", "External API call duration")
# Setup health checker
health_checker = HealthChecker("data-service")
# Add database health check
async def check_database():
try:
from app.core.database import get_db
async for db in get_db():
await db.execute("SELECT 1")
return True
except Exception as e:
return f"Database error: {e}"
health_checker.add_check("database", check_database, timeout=5.0, critical=True)
# Store health checker in app state
app.state.health_checker = health_checker
logger.info("Data Service started successfully")
except Exception as e:
logger.error(f"Failed to start Data Service: {e}")
raise
yield
# Shutdown
logger.info("Shutting down Data Service...")
# Set lifespan AFTER metrics setup
app.router.lifespan_context = lifespan
# CORS middleware (added after metrics setup)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.CORS_ORIGINS,
allow_origins=getattr(settings, 'CORS_ORIGINS', ["*"]),
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
@@ -57,16 +113,30 @@ app.include_router(sales_router, prefix="/api/v1/sales", tags=["sales"])
app.include_router(weather_router, prefix="/api/v1/weather", tags=["weather"])
app.include_router(traffic_router, prefix="/api/v1/traffic", tags=["traffic"])
# Health check endpoint
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "service": "data-service"}
"""Comprehensive health check endpoint"""
if health_checker:
return await health_checker.check_health()
else:
return {
"service": "data-service",
"status": "healthy",
"version": "1.0.0"
}
# Exception handlers
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
"""Global exception handler"""
logger.error("Unhandled exception", exc_info=exc, path=request.url.path)
"""Global exception handler with metrics"""
logger.error(f"Unhandled exception: {exc}", exc_info=True)
# Record error metric if available
if metrics_collector:
metrics_collector.increment_counter("errors_total", labels={"type": "unhandled"})
return JSONResponse(
status_code=500,
content={"detail": "Internal server error"}
)
)