-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sets up discord alerting for run failures in dagster (#1776)
* Sets up discord alerting for run failures in dagster * Update deployment * clean up * Improved comments
- Loading branch information
Showing
15 changed files
with
195 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from dagster import asset, op, job | ||
from .. import constants | ||
|
||
if constants.enable_tests: | ||
|
||
@asset(compute_kind="fake") | ||
def fake_failing_asset() -> None: | ||
raise Exception("This fake asset only ever fails") | ||
|
||
@op(tags={"kind": "fake"}) | ||
def fake_failing_op() -> None: | ||
raise Exception("This fake op only ever fails") | ||
|
||
@job() | ||
def fake_failing_job(): | ||
fake_failing_op() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from dagster import ( | ||
run_failure_sensor, | ||
RunFailureSensorContext, | ||
DefaultSensorStatus, | ||
op, | ||
job, | ||
RunRequest, | ||
RunConfig, | ||
Config, | ||
OpExecutionContext, | ||
DagsterEventType, | ||
) | ||
from ..utils import AlertManager | ||
from .common import AssetFactoryResponse | ||
|
||
|
||
class AlertOpConfig(Config): | ||
run_id: str | ||
|
||
|
||
def setup_alert_sensor(name: str, base_url: str, alert_manager: AlertManager): | ||
@op(name=f"{name}_alert_op") | ||
def failure_op(context: OpExecutionContext, config: AlertOpConfig) -> None: | ||
context.log.info(config.run_id) | ||
instance = context.instance | ||
stats = instance.get_run_stats(config.run_id) | ||
context.log.info(stats) | ||
records = instance.get_records_for_run(config.run_id).records | ||
context.log.info(records) | ||
events = [ | ||
record.event_log_entry for record in records if record.event_log_entry | ||
] | ||
dagster_events = [ | ||
event.dagster_event for event in events if event.dagster_event | ||
] | ||
failures = [event for event in dagster_events if event.is_failure] | ||
step_failures = [ | ||
failure | ||
for failure in failures | ||
if failure.event_type in [DagsterEventType.STEP_FAILURE] | ||
] | ||
|
||
alert_manager.alert( | ||
f"{len(step_failures)} failed steps in run ({base_url}/runs/{config.run_id})" | ||
) | ||
|
||
@job(name=f"{name}_alert_job") | ||
def failure_job(): | ||
failure_op() | ||
|
||
@run_failure_sensor( | ||
name=name, default_status=DefaultSensorStatus.RUNNING, request_job=failure_job | ||
) | ||
def failure_sensor(context: RunFailureSensorContext): | ||
yield RunRequest( | ||
run_key=context.dagster_run.run_id, | ||
run_config=RunConfig( | ||
ops={ | ||
f"{name}_alert_op": { | ||
"config": { | ||
"run_id": context.dagster_run.run_id, | ||
} | ||
} | ||
} | ||
), | ||
) | ||
|
||
return AssetFactoryResponse([], sensors=[failure_sensor], jobs=[failure_job]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
from .retry import * | ||
from .common import * | ||
from .types import * | ||
from .alerts import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import logging | ||
from discord_webhook import DiscordWebhook | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AlertManager: | ||
"""Base class for an alert manager""" | ||
|
||
def alert(self, message: str): | ||
raise NotImplementedError() | ||
|
||
|
||
class LogAlertManager(AlertManager): | ||
def alert(self, message: str): | ||
logging.error(message) | ||
|
||
|
||
class DiscordWebhookAlertManager(AlertManager): | ||
def __init__(self, url: str): | ||
self._url = url | ||
|
||
def alert(self, message: str): | ||
wh = DiscordWebhook(url=self._url, content=message) | ||
wh.execute() |