Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions airflow/providers/amazon/aws/log/cloudwatch_task_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ def _render_filename(self, ti, try_number):

def set_context(self, ti: TaskInstance, *, identifier: str | None = None):
super().set_context(ti)
_json_serialize = conf.getimport("aws", "cloudwatch_task_handler_json_serializer")
_json_serialize = conf.getimport("aws", "cloudwatch_task_handler_json_serializer", fallback=None)
self.handler = watchtower.CloudWatchLogHandler(
log_group_name=self.log_group,
log_stream_name=self._render_filename(ti, ti.try_number),
use_queues=not getattr(ti, "is_trigger_log_context", False),
boto3_client=self.hook.get_conn(),
json_serialize_default=_json_serialize,
json_serialize_default=_json_serialize or json_serialize_legacy,
)

def close(self):
Expand Down
33 changes: 18 additions & 15 deletions tests/providers/amazon/aws/log/test_cloudwatch_task_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
from __future__ import annotations

import contextlib
import logging
import time
from datetime import datetime as dt, timedelta
Expand Down Expand Up @@ -177,10 +176,18 @@ def test_get_cloudwatch_logs(self, mock_get_log_events, end_date, expected_end_t
@pytest.mark.parametrize(
"conf_json_serialize, expected_serialized_output",
[
(None, '{"datetime": "2023-01-01T00:00:00+00:00", "customObject": null}'),
(
pytest.param(
"airflow.providers.amazon.aws.log.cloudwatch_task_handler.json_serialize_legacy",
'{"datetime": "2023-01-01T00:00:00+00:00", "customObject": null}',
id="json-serialize-legacy",
),
pytest.param(
"airflow.providers.amazon.aws.log.cloudwatch_task_handler.json_serialize",
'{"datetime": "2023-01-01T00:00:00+00:00", "customObject": "SomeCustomSerialization(...)"}',
id="json-serialize",
),
pytest.param(
None, '{"datetime": "2023-01-01T00:00:00+00:00", "customObject": null}', id="not-set"
),
],
)
Expand All @@ -193,12 +200,7 @@ def __init__(self):
def __repr__(self):
return "SomeCustomSerialization(...)"

with contextlib.ExitStack() as stack:
if conf_json_serialize:
stack.enter_context(
conf_vars({("aws", "cloudwatch_task_handler_json_serializer"): conf_json_serialize})
)

with conf_vars({("aws", "cloudwatch_task_handler_json_serializer"): conf_json_serialize}):
handler = self.cloudwatch_task_handler
handler.set_context(self.ti)
message = logging.LogRecord(
Expand All @@ -213,12 +215,13 @@ def __repr__(self):
"customObject": ToSerialize(),
},
)
stack.enter_context(mock.patch("watchtower.threading.Thread"))
mock_queue = Mock()
stack.enter_context(mock.patch("watchtower.queue.Queue", return_value=mock_queue))
handler.handle(message)

mock_queue.put.assert_called_once_with({"message": expected_serialized_output, "timestamp": ANY})
with mock.patch("watchtower.threading.Thread"), mock.patch("watchtower.queue.Queue") as mq:
mock_queue = Mock()
mq.return_value = mock_queue
handler.handle(message)
mock_queue.put.assert_called_once_with(
{"message": expected_serialized_output, "timestamp": ANY}
)

def test_close_prevents_duplicate_calls(self):
with mock.patch("watchtower.CloudWatchLogHandler.close") as mock_log_handler_close:
Expand Down