forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIRFLOW-5027] Grab CloudWatch logs after ECS task has finished (apac…
- Loading branch information
1 parent
f114cb4
commit d237f45
Showing
6 changed files
with
251 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
""" | ||
This module contains a hook (AwsLogsHook) with some very basic | ||
functionality for interacting with AWS CloudWatch. | ||
""" | ||
|
||
from airflow.contrib.hooks.aws_hook import AwsHook | ||
|
||
|
||
class AwsLogsHook(AwsHook): | ||
""" | ||
Interact with AWS CloudWatch Logs | ||
:param region_name: AWS Region Name (example: us-west-2) | ||
:type region_name: str | ||
""" | ||
|
||
def __init__(self, region_name=None, *args, **kwargs): | ||
self.region_name = region_name | ||
super().__init__(*args, **kwargs) | ||
|
||
def get_conn(self): | ||
""" | ||
Establish an AWS connection for retrieving logs. | ||
:rtype: CloudWatchLogs.Client | ||
""" | ||
return self.get_client_type('logs', region_name=self.region_name) | ||
|
||
def get_log_events(self, log_group, log_stream_name, start_time=0, skip=0, start_from_head=True): | ||
""" | ||
A generator for log items in a single stream. This will yield all the | ||
items that are available at the current moment. | ||
:param log_group: The name of the log group. | ||
:type log_group: str | ||
:param log_stream_name: The name of the specific stream. | ||
:type log_stream_name: str | ||
:param start_time: The time stamp value to start reading the logs from (default: 0). | ||
:type start_time: int | ||
:param skip: The number of log entries to skip at the start (default: 0). | ||
This is for when there are multiple entries at the same timestamp. | ||
:type skip: int | ||
:param start_from_head: whether to start from the beginning (True) of the log or | ||
at the end of the log (False). | ||
:type start_from_head: bool | ||
:rtype: dict | ||
:return: | A CloudWatch log event with the following key-value pairs: | ||
| 'timestamp' (int): The time in milliseconds of the event. | ||
| 'message' (str): The log event data. | ||
| 'ingestionTime' (int): The time in milliseconds the event was ingested. | ||
""" | ||
|
||
next_token = None | ||
|
||
event_count = 1 | ||
while event_count > 0: | ||
if next_token is not None: | ||
token_arg = {'nextToken': next_token} | ||
else: | ||
token_arg = {} | ||
|
||
response = self.get_conn().get_log_events(logGroupName=log_group, | ||
logStreamName=log_stream_name, | ||
startTime=start_time, | ||
startFromHead=start_from_head, | ||
**token_arg) | ||
|
||
events = response['events'] | ||
event_count = len(events) | ||
|
||
if event_count > skip: | ||
events = events[skip:] | ||
skip = 0 | ||
else: | ||
skip = skip - event_count | ||
events = [] | ||
|
||
yield from events | ||
|
||
if 'nextForwardToken' in response: | ||
next_token = response['nextForwardToken'] | ||
else: | ||
return |
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,82 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# | ||
|
||
import unittest | ||
|
||
from airflow.contrib.hooks.aws_logs_hook import AwsLogsHook | ||
|
||
try: | ||
from moto import mock_logs | ||
except ImportError: | ||
mock_logs = None | ||
|
||
|
||
class TestAwsLogsHook(unittest.TestCase): | ||
|
||
@unittest.skipIf(mock_logs is None, 'mock_logs package not present') | ||
@mock_logs | ||
def test_get_conn_returns_a_boto3_connection(self): | ||
hook = AwsLogsHook(aws_conn_id='aws_default', | ||
region_name="us-east-1") | ||
self.assertIsNotNone(hook.get_conn()) | ||
|
||
@unittest.skipIf(mock_logs is None, 'mock_logs package not present') | ||
# moto.logs does not support proper pagination so we cannot test that yet | ||
# https://github.com/spulec/moto/issues/2259 | ||
@mock_logs | ||
def test_get_log_events(self): | ||
log_group_name = 'example-group' | ||
log_stream_name = 'example-log-stream' | ||
|
||
hook = AwsLogsHook(aws_conn_id='aws_default', | ||
region_name="us-east-1") | ||
|
||
# First we create some log events | ||
conn = hook.get_conn() | ||
conn.create_log_group(logGroupName=log_group_name) | ||
conn.create_log_stream(logGroupName=log_group_name, logStreamName=log_stream_name) | ||
|
||
input_events = [ | ||
{ | ||
'timestamp': 1, | ||
'message': 'Test Message 1' | ||
} | ||
] | ||
|
||
conn.put_log_events(logGroupName=log_group_name, | ||
logStreamName=log_stream_name, | ||
logEvents=input_events) | ||
|
||
events = hook.get_log_events( | ||
log_group=log_group_name, | ||
log_stream_name=log_stream_name | ||
) | ||
|
||
# Iterate through entire generator | ||
events = list(events) | ||
count = len(events) | ||
|
||
assert count == 1 | ||
assert events[0]['timestamp'] == input_events[0]['timestamp'] | ||
assert events[0]['message'] == input_events[0]['message'] | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.