Skip to content

Best Practices

Best Practices

  1. Production Usage: Use return_raw_result=True in production to avoid overhead
  2. Input Validation: Enable input validation for external-facing functions
  3. Log File Management: Configure log rotation to prevent disk space issues
  4. Selective Monitoring: Disable CPU/memory monitoring for high-frequency functions if needed
  5. Error Handling: Always handle the case where monitoring might return error status
# Production configuration example
configure_monitor(
    log_to_file=True,
    log_file_path="/var/log/myapp/pyfuncmonitor.log",
    log_level=20,  # INFO
    default_return_raw_result=True,
    default_validate_input=True,
    default_validate_output=False  # Disable output validation for performance
)

@monitor_function()
def api_endpoint(request_data: RequestModel) -> ResponseModel:
    # Your business logic here
    return process_request(request_data)

# Usage with error handling
result = api_endpoint(request)
if isinstance(result, dict) and result.get("status") == "error":
    # Handle error case
    logger.error("Function failed", errors=result["errors"])
    return error_response()
else:
    # Success case - result is the actual return value
    return result