SnowAlert icon indicating copy to clipboard operation
SnowAlert copied to clipboard

Document SNS handler with example

Open bhasampa opened this issue 4 years ago • 8 comments

Dear Team,

I am trying to call sns handler for the alert but while running the snowalert run all command I am getting an error following are the details:

If there is any documentation link on using handler part can you please share across the same.

SQL - Alert CREATE OR REPLACE VIEW snowalert.rules.ETL_BATCH_CODW_COMPANYOPS_FINACE_issue_alert_query COPY GRANTS COMMENT='Alerts on unknown transaction type @tags snowflake,msft trans issue' AS SELECT 'SnowAlert' AS environment , array_construct('ETL_BATCH_CODW_COMPANYOPS_FINACE_ERROR') AS sources , BATCH_NAME AS object , 'BATCH GOT FAILED' AS title , CURRENT_TIMESTAMP() AS event_time , CURRENT_TIMESTAMP() AS alert_time , BATCH_NAME || ' Got Failed' AS description , OBJECT_CONSTRUCT( 'type', 'sns', 'topic', 'snowalert_topic', 'message', description ) as HANDLERS , 'snowalert' AS actor , 'login' AS action , 'SnowAlert' AS detector , OBJECT_CONSTRUCT(*) AS event_data , 'low' AS severity , 'etl batch issue' AS query_name , null AS query_id FROM SNOWALERT.DATA.V_ETL_BATCH_CODW_COMPANYOPS_FINACE_ERROR

Error Message: [6] ./run.py:77 in fire.Fire(main) ./../../venv/lib/python3.7/site-packages/fire/core.py:127 in Fire component_trace = _Fire(component, args, context, name) ./../../venv/lib/python3.7/site-packages/fire/core.py:366 in _Fire component, remaining_args) ./../../venv/lib/python3.7/site-packages/fire/core.py:542 in _CallCallable result = fn(*varargs, **kwargs) ./run.py:59 in main alert_dispatcher.main() ./../../venv/lib/python3.7/site-packages/runners/alert_dispatcher.py:82 in main log.error(e, 'handler failed') --- printed exception w/ trace --- ./../../venv/lib/python3.7/site-packages/runners/alert_dispatcher.py:72 in main f'runners.handlers.{handler_type}' ./../../../../../usr/local/lib/python3.7/importlib/init.py:127 in import_module return _bootstrap._gcd_import(name[level:], package, level) ./:1006 in _gcd_import

./:983 in _find_and_load

./:965 in _find_and_load_unlocked

ModuleNotFoundError: No module named 'runners.handlers.message' [6] handler failed [6] ./run.py:77 in fire.Fire(main) ./../../venv/lib/python3.7/site-packages/fire/core.py:127 in Fire component_trace = _Fire(component, args, context, name) ./../../venv/lib/python3.7/site-packages/fire/core.py:366 in _Fire component, remaining_args) ./../../venv/lib/python3.7/site-packages/fire/core.py:542 in _CallCallable result = fn(*varargs, **kwargs) ./run.py:59 in main alert_dispatcher.main() ./../../venv/lib/python3.7/site-packages/runners/alert_dispatcher.py:82 in main log.error(e, 'handler failed') --- printed exception w/ trace --- ./../../venv/lib/python3.7/site-packages/runners/alert_dispatcher.py:72 in main f'runners.handlers.{handler_type}' ./../../../../../usr/local/lib/python3.7/importlib/init.py:127 in import_module return _bootstrap._gcd_import(name[level:], package, level) ./:1006 in _gcd_import

./:983 in _find_and_load

./:965 in _find_and_load_unlocked

ModuleNotFoundError: No module named 'runners.handlers.topic' [6] handler failed [6] Updating alert table: [6] UPDATE results.alerts SET handled=%s WHERE alert:ALERT_ID='5a31b4fa-81ac-4a47-a034-df23378a4284' [6] ./run.py:77 in fire.Fire(main) ./../../venv/lib/python3.7/site-packages/fire/core.py:127 in Fire component_trace = _Fire(component, args, context, name) ./../../venv/lib/python3.7/site-packages/fire/core.py:366 in _Fire component, remaining_args) ./../../venv/lib/python3.7/site-packages/fire/core.py:542 in _CallCallable result = fn(*varargs, **kwargs) ./run.py:59 in main alert_dispatcher.main() ./../../venv/lib/python3.7/site-packages/runners/alert_dispatcher.py:82 in main log.error(e, 'handler failed') --- printed exception w/ trace --- ./../../venv/lib/python3.7/site-packages/runners/alert_dispatcher.py:72 in main f'runners.handlers.{handler_type}' ./../../../../../usr/local/lib/python3.7/importlib/init.py:127 in import_module return _bootstrap._gcd_import(name[level:], package, level) ./:1006 in _gcd_import

./:983 in _find_and_load

./:965 in _find_and_load_unlocked

ModuleNotFoundError: No module named 'runners.handlers.type'

Thanks, Bhaskar

bhasampa avatar Jun 02 '20 19:06 bhasampa

The handers should be an array of objects, so instead of —

OBJECT_CONSTRUCT(
  'type', 'sns',
  'topic', 'snowalert_topic',
  'message', description
) AS handlers

try —

ARRAY_CONSTRUCT(
  OBJECT_CONSTRUCT(
    'type', 'sns',
    'topic', 'snowalert_topic',
    'message', description
  )
) AS handlers

This way, you can add a list of handlers. Let's keep this issue open to have this use-case handled gracefully, as the runners should understand a single object instead of an array there, as well.

sfc-gh-afedorov avatar Jun 02 '20 19:06 sfc-gh-afedorov

Thanks it was working after changing the code.

bhasampa avatar Jun 02 '20 20:06 bhasampa

Hi, For the same above example i can see the SNS got the message present in the Handlers array but not the event data. how can the event data be sent to SNS.

syedirfanahmedansari avatar Jun 03 '21 19:06 syedirfanahmedansari

just as the description is passed in as the message in the example above, you should be able to pass the event_data (or a piece of it?) into the handler.

sfc-gh-afedorov avatar Jun 03 '21 22:06 sfc-gh-afedorov

Some thing like this. where event data is part of the handler

ARRAY_CONSTRUCT( OBJECT_CONSTRUCT( 'type', 'sns', 'topic', 'snowalert_topic', 'message', description, 'event_data',object_construct(*) ) ) AS handlers

syedirfanahmedansari avatar Jun 04 '21 12:06 syedirfanahmedansari

No, because the SNS handler module does not have an event_data parameter in its handle method.

sfc-gh-afedorov avatar Jun 04 '21 19:06 sfc-gh-afedorov

Can you share a Sample for SNS where event data is also passed to the SNS Handler.

syedirfanahmedansari avatar Jun 07 '21 06:06 syedirfanahmedansari

Sorry, we don't use that handler, so the code is the docs for it unless you can provide one.

Whatever key/values you parametrize the handler object with get passed to the handle method. There's a few options explained in the hander docs here.

I can keep the issue open as a documentation request.

sfc-gh-afedorov avatar Jun 15 '21 01:06 sfc-gh-afedorov