Skip to content

Commit

Permalink
fix(providers/datadog): respect soft_fail argument when exception is …
Browse files Browse the repository at this point in the history
…raised (#34472)
  • Loading branch information
Lee-W committed Sep 19, 2023
1 parent d20c32f commit e749d2d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions airflow/providers/datadog/sensors/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from datadog import api

from airflow.exceptions import AirflowException
from airflow.exceptions import AirflowException, AirflowSkipException
from airflow.providers.datadog.hooks.datadog import DatadogHook
from airflow.sensors.base import BaseSensorOperator

Expand Down Expand Up @@ -89,7 +89,11 @@ def poke(self, context: Context) -> bool:

if isinstance(response, dict) and response.get("status", "ok") != "ok":
self.log.error("Unexpected Datadog result: %s", response)
raise AirflowException("Datadog returned unexpected result")
# TODO: remove this if check when min_airflow_version is set to higher than 2.7.1
message = "Datadog returned unexpected result"
if self.soft_fail:
raise AirflowSkipException(message)
raise AirflowException(message)

if self.response_check:
# run content check on response
Expand Down
26 changes: 26 additions & 0 deletions tests/providers/datadog/sensors/test_datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import json
from unittest.mock import patch

import pytest

from airflow.exceptions import AirflowException, AirflowSkipException
from airflow.models import Connection
from airflow.providers.datadog.sensors.datadog import DatadogSensor
from airflow.utils import db
Expand Down Expand Up @@ -111,3 +114,26 @@ def test_sensor_fail(self, api1, api2):
)

assert not sensor.poke({})

@pytest.mark.parametrize(
"soft_fail, expected_exception", ((False, AirflowException), (True, AirflowSkipException))
)
@patch("airflow.providers.datadog.hooks.datadog.api.Event.query")
@patch("airflow.providers.datadog.sensors.datadog.api.Event.query")
def test_sensor_fail_with_exception(self, api1, api2, soft_fail, expected_exception):
api1.return_value = zero_events
api2.return_value = {"status": "error"}

with pytest.raises(expected_exception):
sensor = DatadogSensor(
task_id="test_datadog",
datadog_conn_id="datadog_default",
from_seconds_ago=0,
up_to_seconds_from_now=0,
priority=None,
sources=None,
tags=None,
response_check=None,
soft_fail=soft_fail,
)
sensor.poke({})

0 comments on commit e749d2d

Please sign in to comment.