There are no items in your cart
Add More
Add More
Item Details | Price |
---|
Mon Apr 15, 2024
In the world of data warehousing, keeping track of changes to your Snowflake warehouse size is crucial. Unexpected adjustments can impact performance and potentially incur additional costs. This blog post introduces a solution for automated warehouse size change monitoring and email alerts using Snowflake Streams and Tasks.Imagine you’re a data analyst managing a busy Snowflake account. You rely on a designated warehouse to handle your data processing needs. However, you’re concerned about unauthorized modifications to the warehouse size, which could disrupt your workflows.This code snippet provides an automated solution for this scenario:
:1.Create Audit Table.
create or replace table WH_AUDIT_TABLE as SELECT query_id,query_text, role_name, warehouse_name, start_time, end_time FROM TABLE(SNOWFLAKE.INFORMATION_SCHEMA.QUERY_HISTORY()) where 1=2;
2. Create TASK to load Audit table.
CREATE TASK WH_CHANGE_TRACK
WAREHOUSE = COMPUTE_WH
SCHEDULE = '1 MINUTE'
AS
insert into WH_AUDIT_TABLE (query_id,query_text, role_name, warehouse_name, start_time, end_time)
SELECT query_id,query_text, role_name, warehouse_name, start_time, end_time
FROM TABLE(SNOWFLAKE.INFORMATION_SCHEMA.QUERY_HISTORY()) Q
where database_name is null and query_text like 'alter WAREHOUSE%'
and current_timestamp()>dateadd('hour',-1,current_timestamp())
and not exists
(SELECT '1' from WH_AUDIT_TABLE W where Q.Query_id = w.query_id);WAREHOUSE = COMPUTE_WH
SCHEDULE = '1 MINUTE'
AS
insert into WH_AUDIT_TABLE (query_id,query_text, role_name, warehouse_name, start_time, end_time)
SELECT query_id,query_text, role_name, warehouse_name, start_time, end_time
FROM TABLE(SNOWFLAKE.INFORMATION_SCHEMA.QUERY_HISTORY()) Q
where database_name is null and query_text like 'alter WAREHOUSE%'
and current_timestamp()>dateadd('hour',-1,current_timestamp())
and not exists
(SELECT '1' from WH_AUDIT_TABLE W where Q.Query_id = w.query_id);
3. Create STREAM on Audit Table.
create or replace stream WH_AUDIT_TABLE_STM on table WH_AUDIT_TABLE;
4. Create TASK to check Stream and send Email about Warehouse changes.
CREATE OR REPLACE TASK WH_CHANGE_MAIL
WAREHOUSE = COMPUTE_WH
SCHEDULE = '2 minute'
WHEN
SYSTEM$STREAM_HAS_DATA('WH_AUDIT_TABLE_STM')
AS
CALL SEND_MAIL();
Procedure:
CREATE OR REPLACE PROCEDURE SEND_MAIL()RETURNS VARCHARLANGUAGE JAVASCRIPTAS$$var sql_query = "SELECT QUERY_TEXT, ROLE_NAME, WAREHOUSE_NAME, START_TIME, END_TIME FROM WH_AUDIT_TABLE_STM";var sqlstmt = snowflake.createStatement({ sqlText: sql_query });var rs = sqlstmt.execute();var msg = `<html><head><style>table {border-collapse: collapse;width: 100%;border: 2px solid #4CAF50;}th, td {border: 2px solid #4CAF50;padding: 8px;text-align: center;}th {background-color: #2196F3; /* Different color for the header */color: white;}td {background-color: #f8f8f8;}</style></head><body><table><tr><th>QUERY TEXT</th><th>ROLE NAME</th><th>WAREHOUSE NAME</th><th>START TIME</th><th>END TIME</th></tr>`;while (rs.next()) {var QTEXT = rs.getColumnValue(1);QTEXT = QTEXT.replace(/'/g, "''");var RNAME = rs.getColumnValue(2);var WAREHOUSE_NAME = rs.getColumnValue(3);var STIME = rs.getColumnValue(4);var ETIME = rs.getColumnValue(5);var rowColor = '#c8e6c9'; // Light green colormsg += `<tr><td>${QTEXT}</td><td>${RNAME}</td><td>${WAREHOUSE_NAME}</td><td>${STIME}</td><td>${ETIME}</td></tr>`;}msg += `</table></body></html>`;var proc = `CALL SYSTEM$SEND_EMAIL('WH_CHANGE_NOTIFICATION', 'sachin.mittal04@gmail.com', 'Daily Credit Consumption: Snowflake Account', '${msg}', 'text/html')`;var stmt = snowflake.createStatement({ sqlText: proc });stmt.execute();var copy_command = `CREATE TEMPORARY TABLE if not exists STREAM_CONSUME AS SELECT * FROM WH_AUDIT_TABLE_STM`;snowflake.createStatement({sqlText: copy_command}).execute();return "Succeeded! Email sent to Admin";$$;
6. Resume Both TASKS
alter task WH_CHANGE_TRACK RESUME;
alter task WH_CHANGE_MAIL RESUME;
7. Now change the size of Warehouses and verify the Audit table and respective Stream:
Conclusion
This code demonstrates how Snowflake Streams and Tasks can be leveraged to build a robust warehouse change monitoring system. By implementing this solution, you gain valuable insights into your Snowflake environment and ensure optimal resource utilization.I hope this blog post empowers you to proactively monitor your Snowflake warehouses and maintain control over your data resources!
Sachin Mittal
Snowflake Architect.