Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alerting on slack when there is no alerts to be send #294

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions elementary/monitor/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def get_cli_properties() -> dict:
default=None,
help="Specify raw YAML string of your dbt variables."
)
@click.option(
'--test',
type=str,
default=None,
help="Specify raw YAML string of your dbt variables."
)
@click.pass_context
def monitor(
ctx,
Expand All @@ -113,7 +119,8 @@ def monitor(
update_dbt_package,
full_refresh_dbt_package,
profile_target,
dbt_vars
dbt_vars,
test
):
"""
Monitor your warehouse.
Expand All @@ -130,7 +137,8 @@ def monitor(
anonymous_tracking.track_cli_start('monitor', get_cli_properties(), ctx.command.name)
try:
config.validate_monitor()
data_monitoring = DataMonitoring(config=config, force_update_dbt_package=update_dbt_package)
data_monitoring = DataMonitoring(config=config, force_update_dbt_package=update_dbt_package,
send_test_message_on_success=test)
success = data_monitoring.run(days_back, full_refresh_dbt_package, dbt_vars=vars)
anonymous_tracking.track_cli_end('monitor', data_monitoring.properties(), ctx.command.name)
if not success:
Expand Down
13 changes: 12 additions & 1 deletion elementary/monitor/data_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os.path
import webbrowser
from collections import defaultdict
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple

import pkg_resources
Expand Down Expand Up @@ -38,7 +39,8 @@

class DataMonitoring:

def __init__(self, config: Config, force_update_dbt_package: bool = False):
def __init__(self, config: Config, force_update_dbt_package: bool = False,
send_test_message_on_success: bool = False):
self.config = config
self.dbt_runner = DbtRunner(dbt_project_utils.PATH, self.config.profiles_dir, self.config.profile_target)
self.execution_properties = {}
Expand All @@ -51,6 +53,7 @@ def __init__(self, config: Config, force_update_dbt_package: bool = False):
self.alerts_api = AlertsAPI(self.dbt_runner, self.elementary_database_and_schema)
self.sent_alert_count = 0
self.success = True
self.send_test_message_on_success = send_test_message_on_success

def _send_alerts_to_slack(self, alerts: List[Alert], alerts_table_name: str):
if not alerts:
Expand Down Expand Up @@ -85,6 +88,12 @@ def _download_dbt_package_if_needed(self, force_update_dbt_packages: bool):
self.success = False
return

def _send_test_message(self):
self.slack_client.send_message(
channel_name=self.config.slack_channel_name,
message=f"Elementary monitor ran successfully on {datetime.now().strftime('%Y-%m-%d %H:%M')}"
)

def _send_alerts(self, alerts: Alerts):
self._send_alerts_to_slack(alerts.tests.get_all(), TestAlert.TABLE_NAME)
self._send_alerts_to_slack(alerts.models.get_all(), ModelAlert.TABLE_NAME)
Expand All @@ -109,6 +118,8 @@ def run(self, days_back: int, dbt_full_refresh: bool = False, dbt_vars: Optional
self.execution_properties['malformed_alert_count'] = malformed_alert_count
self.execution_properties['has_subscribers'] = any(alert.subscribers for alert in alerts.get_all())
self._send_alerts(alerts)
if self.send_test_message_on_success and alerts.count == 0:
self._send_test_message()
self.execution_properties['run_end'] = True
self.execution_properties['success'] = self.success
return self.success
Expand Down