Skip to content

Commit

Permalink
establish a minimal permissions workaround for session logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealfare committed Jul 12, 2024
1 parent 8249543 commit 1ac3b2f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
4 changes: 4 additions & 0 deletions dbt/adapters/snowflake/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ def submit_python_job(self, parsed_model: dict, compiled_code: str):
if use_anonymous_sproc:
proc_name = f"{identifier}__dbt_sp"
python_stored_procedure = f"""
ALTER SESSION SET LOG_LEVEL = DEBUG;
ALTER SESSION SET TRACE_LEVEL = ALWAYS;
WITH {proc_name} AS PROCEDURE ()
{common_procedure_code}
CALL {proc_name}();
Expand All @@ -372,6 +374,8 @@ def submit_python_job(self, parsed_model: dict, compiled_code: str):
python_stored_procedure = f"""
CREATE OR REPLACE PROCEDURE {proc_name} ()
{common_procedure_code};
ALTER SESSION SET LOG_LEVEL = DEBUG;
ALTER SESSION SET TRACE_LEVEL = ALWAYS;
CALL {proc_name}();
"""
Expand Down
21 changes: 0 additions & 21 deletions tests/functional/adapter/python_model_tests/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,3 @@ def model(dbt, session):
{% do return(_table) %}
{% endmacro %}
"""


MODEL__LOGGING = """
import logging
import snowflake.snowpark as snowpark
import snowflake.snowpark.functions as f
from snowflake.snowpark.functions import *
logger = logging.getLogger("dbt_logger")
logger.info("******Inside Logging module.******")
def model(dbt, session):
session.sql(f"ALTER SESSION SET LOG_LEVEL = INFO").collect()
logger.info("******Logging start.******")
df=session.sql(f"select current_user() as session_user, current_role() as session_role")
logger.info("******Logging End.******")
return df
"""
36 changes: 22 additions & 14 deletions tests/functional/adapter/python_model_tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
from dbt.tests.util import run_dbt
import pytest

from tests.functional.adapter.python_model_tests._files import MODEL__LOGGING

EVENT_TABLE_SQL = """
SELECT
RECORD['severity_text']::STRING AS log_level,
VALUE::STRING AS message,
RESOURCE_ATTRIBUTES['snow.query.id']::STRING AS query_id
FROM
DXRX_OPERATIONS.LOGGING.EVENTS
WHERE
SCOPE['name']::STRING = 'dbt_logger'
ORDER BY
'TIMESTAMP' DESC
;

MODEL__LOGGING = """
import logging
import snowflake.snowpark as snowpark
import snowflake.snowpark.functions as f
from snowflake.snowpark.functions import *
logger = logging.getLogger("dbt_logger")
logger.info("******Inside Logging module.******")
def model(dbt, session: snowpark.Session):
logger.info("******Logging start.******")
df=session.sql(f"select current_user() as session_user, current_role() as session_role")
logger.info("******Logging End.******")
return df
"""


class TestPythonModelLogging:
"""
This test case addresses bug report https://github.com/dbt-labs/dbt-snowflake/issues/846
"""

@pytest.fixture(scope="class")
def models(self):
return {"logging_model.py": MODEL__LOGGING}
Expand Down

0 comments on commit 1ac3b2f

Please sign in to comment.