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

Add utility to watch CW logs from TPS instance. #14

Merged
merged 1 commit into from
Aug 28, 2018
Merged
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
48 changes: 48 additions & 0 deletions utils/cwtail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import boto3
import time
import argparse


parser = argparse.ArgumentParser(
description="""
Stream the latest lines from a log stream by ingestion time. This acts a bit
like "tailing" a log file. It's by ingestion time rather than event time, as
the TPS logs don't seem to get the right event time right now.
""")
parser.add_argument('group', help='Name of the log group for TPS logs')
parser.add_argument('--stream', help='Name of log stream. Optional, defaults '
'to the stream in the group with the most recent events')
parser.add_argument('--interval', default=60, type=int,
help='Interval between log updates.')
args = parser.parse_args()

logs = boto3.client('logs')
log_group = args.group
log_stream = args.stream
if log_stream is None:
response = logs.describe_log_streams(
logGroupName=log_group,
orderBy='LastEventTime',
descending=True,
limit=1,
)

streams = response['logStreams']
assert streams
log_stream = streams[0]['logStreamName']

last_time = None
while True:
response = logs.get_log_events(
logGroupName=log_group, logStreamName=log_stream, limit=150,
startFromHead=False)

for event in response['events']:
ingest_time = event['ingestionTime']
# any integer is greater than None, so we don't need to check for
# last_time is None
if ingest_time > last_time:
print(event['message'])
last_time = ingest_time

time.sleep(args.interval)