Allow empty observations in alerting (#10939)
Co-authored-by: bogdan kyryliuk <bogdankyryliuk@dropbox.com>
This commit is contained in:
parent
74a2270d0a
commit
801fb4063c
|
|
@ -48,7 +48,7 @@ def observe(alert_id: int, session: Session) -> Optional[str]:
|
|||
|
||||
error_msg = validate_observer_result(df, alert.id, alert.label)
|
||||
|
||||
if not error_msg and df.to_records()[0][1] is not None:
|
||||
if not error_msg and not df.empty and df.to_records()[0][1] is not None:
|
||||
value = float(df.to_records()[0][1])
|
||||
|
||||
observation = SQLObservation(
|
||||
|
|
@ -74,9 +74,9 @@ def validate_observer_result(
|
|||
Returns an error message if the result is invalid.
|
||||
"""
|
||||
try:
|
||||
assert (
|
||||
not sql_result.empty
|
||||
), f"Observer for alert <{alert_id}:{alert_label}> returned no rows"
|
||||
if sql_result.empty:
|
||||
# empty results are used for the not null validator
|
||||
return None
|
||||
|
||||
rows = sql_result.to_records()
|
||||
|
||||
|
|
|
|||
|
|
@ -128,11 +128,11 @@ def test_alert_observer(setup_database):
|
|||
assert alert3.sql_observer[0].observations[-1].value is None
|
||||
assert alert3.sql_observer[0].observations[-1].error_msg is None
|
||||
|
||||
# Test SQLObserver with empty SQL return
|
||||
# Test SQLObserver with empty SQL return, expected
|
||||
alert4 = create_alert(dbsession, "SELECT first FROM test_table WHERE first = -1")
|
||||
observe(alert4.id, dbsession)
|
||||
assert alert4.sql_observer[0].observations[-1].value is None
|
||||
assert alert4.sql_observer[0].observations[-1].error_msg is not None
|
||||
assert alert4.sql_observer[0].observations[-1].error_msg is None
|
||||
|
||||
# Test SQLObserver with str result
|
||||
alert5 = create_alert(dbsession, "SELECT 'test_string' as string_value")
|
||||
|
|
|
|||
Loading…
Reference in New Issue