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

Fix bug in task logs when using AWS CloudWatch. Do not set start_time #33673

Merged
merged 2 commits into from
Aug 24, 2023
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: 0 additions & 4 deletions airflow/providers/amazon/aws/log/cloudwatch_task_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@ def get_cloudwatch_logs(self, stream_name: str, task_instance: TaskInstance) ->
:param task_instance: the task instance to get logs about
:return: string of all logs from the given log stream
"""
start_time = (
0 if task_instance.start_date is None else datetime_to_epoch_utc_ms(task_instance.start_date)
)
# If there is an end_date to the task instance, fetch logs until that date + 30 seconds
# 30 seconds is an arbitrary buffer so that we don't miss any logs that were emitted
end_time = (
Expand All @@ -127,7 +124,6 @@ def get_cloudwatch_logs(self, stream_name: str, task_instance: TaskInstance) ->
events = self.hook.get_log_events(
log_group=self.log_group,
log_stream_name=stream_name,
start_time=start_time,
end_time=end_time,
)
return "\n".join(self._event_to_str(event) for event in events)
Expand Down
24 changes: 4 additions & 20 deletions tests/providers/amazon/aws/log/test_cloudwatch_task_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,35 +155,19 @@ def test_read(self):
)

@pytest.mark.parametrize(
"start_date, end_date, expected_start_time, expected_end_time",
"end_date, expected_end_time",
[
(None, None, 0, None),
(datetime(2020, 1, 1), None, datetime_to_epoch_utc_ms(datetime(2020, 1, 1)), None),
(
None,
datetime(2020, 1, 2),
0,
datetime_to_epoch_utc_ms(datetime(2020, 1, 2) + timedelta(seconds=30)),
),
(
datetime(2020, 1, 1),
datetime(2020, 1, 2),
datetime_to_epoch_utc_ms(datetime(2020, 1, 1)),
datetime_to_epoch_utc_ms(datetime(2020, 1, 2) + timedelta(seconds=30)),
),
(None, None),
(datetime(2020, 1, 2), datetime_to_epoch_utc_ms(datetime(2020, 1, 2) + timedelta(seconds=30))),
],
)
@mock.patch.object(AwsLogsHook, "get_log_events")
def test_get_cloudwatch_logs(
self, mock_get_log_events, start_date, end_date, expected_start_time, expected_end_time
):
self.ti.start_date = start_date
def test_get_cloudwatch_logs(self, mock_get_log_events, end_date, expected_end_time):
self.ti.end_date = end_date
self.cloudwatch_task_handler.get_cloudwatch_logs(self.remote_log_stream, self.ti)
mock_get_log_events.assert_called_once_with(
log_group=self.remote_log_group,
log_stream_name=self.remote_log_stream,
start_time=expected_start_time,
end_time=expected_end_time,
)

Expand Down