import functools
import logging
import time
from typing import Callable, Any
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def instrument_response(func: Callable) -> Callable:
"""
Decorator to instrument HTTP response handling for maintenance.
Logs response details and execution time.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs) -> Any:
"""Wrapper function to instrument the original function."""
start_time = time.time()
try:
response = func(*args, **kwargs) # Execute the original function
end_time = time.time()
execution_time = end_time - start_time
logging.info(f"Response from {func.__name__}: Status Code - {response.status_code}, Time taken - {execution_time:.4f} seconds")
return response
except Exception as e:
logging.error(f"Error in {func.__name__}: {e}")
raise # Re-raise the exception to avoid masking the error
return wrapper
Add your comment