Skip to content

Commit

Permalink
Feat - implemented retrieve cloudwatch logs function
Browse files Browse the repository at this point in the history
  • Loading branch information
aybruhm committed Dec 7, 2023
1 parent 0e71231 commit 877be34
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions agenta-backend/agenta_backend/services/logs_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import boto3


# Initialize the CloudWatch Logs client
client = boto3.client("logs")


def retrieve_cloudwatch_logs(function_app_id: str):
log_group_name = f"/aws/lambda/app-{function_app_id}"

# Describe log streams to get the newest log stream
response = client.describe_log_streams(
logGroupName=log_group_name, orderBy="lastEventTimestamp", descending=True, limit=1
)

if "logStreams" in response and len(response["logStreams"]) > 0:
newest_log_stream = response["logStreams"][0]["logStreamName"]

# Get log events of the newest log stream
log_events_response = client.get_log_events(
logGroupName=log_group_name, logStreamName=newest_log_stream
)
if "events" in log_events_response:
response_data = {}
list_of_events_messages = []
for event in log_events_response["events"]:
list_of_events_messages.append(event["message"])

response_data["message"] = "Log events found in the newest log stream"
response_data["data"] = list_of_events_messages
return response_data
else:
return "No log events found in the newest log stream"
else:
return "No log streams found in the log group."

0 comments on commit 877be34

Please sign in to comment.