Created
July 8, 2024 13:31
-
-
Save orellabac/1ef0fdb7b0dd8bfefb979b68ae3900ae to your computer and use it in GitHub Desktop.
Example Snowpark Procedure where we trap errors and save them into a file in an stage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create or replace procedure proc_logged_to_stage(arg1 string, arg2 string) | |
returns string | |
language python | |
runtime_version = 3.11 | |
packages =('snowflake-snowpark-python') | |
handler = 'logged_main' | |
as | |
$$ | |
from snowflake.snowpark import Session | |
from snowflake.snowpark.functions import col | |
def logged_run(session:Session, stage:str, handler, *args): | |
import logging, os, datetime, sys | |
# Get the current datetime and format it as a string | |
current_datetime = datetime.datetime.now().strftime('%Y%m%d_%H%M%S') | |
log_file = f'/tmp/log_{current_datetime}.log' | |
# Set up logging | |
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
try: | |
# Example of logging information | |
logging.info(f"Starting execution with arguments {args}") | |
handler(session, *args) | |
finally: | |
exc_type, exc_value, exc_traceback = sys.exc_info() | |
if exc_type is not None: | |
logging.error("Exception occurred", exc_info=(exc_type, exc_value, exc_traceback)) | |
logging.info(f"Saving log results into stage {stage}") | |
# Ensure the log file exists before trying to copy | |
if os.path.exists(log_file): | |
session.file.put(log_file,stage,auto_compress=False, overwrite=True) | |
def main(session:Session, arg1:str, arg2:str): | |
result = 10 / 0 # This will raise a ZeroDivisionError | |
def logged_main(session: Session, arg1:str, arg2:str): | |
logged_run(session, "@mystage",main, arg1, arg2) | |
$$; | |
call PROC_LOGGED_TO_STAGE('arg1val','arg2val'); | |
/* | |
For example the log produced should look like this: | |
2024-07-08 06:29:27,173 - INFO - Starting execution with arguments ('arg1val', 'arg2val') | |
2024-07-08 06:29:27,173 - ERROR - Exception occurred | |
Traceback (most recent call last): | |
File "_udf_code.py", line 14, in logged_run | |
File "_udf_code.py", line 26, in main | |
ZeroDivisionError: division by zero | |
2024-07-08 06:29:27,173 - INFO - Saving log results into stage @mystage | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment