-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Reapply reduce requests necessary for log publishing from lambda to cloudwatch logs #12470
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import dataclasses | ||
import logging | ||
import threading | ||
import time | ||
from queue import Queue | ||
from typing import Optional, Union | ||
|
||
from localstack.aws.connect import connect_to | ||
from localstack.utils.aws.client_types import ServicePrincipal | ||
from localstack.utils.bootstrap import is_api_enabled | ||
from localstack.utils.cloudwatch.cloudwatch_util import store_cloudwatch_logs | ||
from localstack.utils.threads import FuncThread | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
@@ -50,10 +50,34 @@ def run_log_loop(self, *args, **kwargs) -> None: | |
log_item = self.log_queue.get() | ||
if log_item is QUEUE_SHUTDOWN: | ||
return | ||
# we need to split by newline - but keep the newlines in the strings | ||
# strips empty lines, as they are not accepted by cloudwatch | ||
logs = [line + "\n" for line in log_item.logs.split("\n") if line] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following this discussion, do we have an intuition where these replacements came from?
I just wanted to add the reference in case we need to add them reactively because we removed them without knowing where they came from 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, still no idea. Since those are ANSI escape sequences, I still think they stem from the time were we captured the lambda logs directly from docker running inside a full terminal instance. |
||
# until we have a better way to have timestamps, log events have the same time for a single invocation | ||
log_events = [ | ||
{"timestamp": int(time.time() * 1000), "message": log_line} for log_line in logs | ||
] | ||
try: | ||
store_cloudwatch_logs( | ||
logs_client, log_item.log_group, log_item.log_stream, log_item.logs | ||
) | ||
try: | ||
logs_client.put_log_events( | ||
logGroupName=log_item.log_group, | ||
logStreamName=log_item.log_stream, | ||
logEvents=log_events, | ||
) | ||
except logs_client.exceptions.ResourceNotFoundException: | ||
# create new log group | ||
try: | ||
logs_client.create_log_group(logGroupName=log_item.log_group) | ||
except logs_client.exceptions.ResourceAlreadyExistsException: | ||
pass | ||
logs_client.create_log_stream( | ||
logGroupName=log_item.log_group, logStreamName=log_item.log_stream | ||
) | ||
logs_client.put_log_events( | ||
logGroupName=log_item.log_group, | ||
logStreamName=log_item.log_stream, | ||
logEvents=log_events, | ||
) | ||
except Exception as e: | ||
LOG.warning( | ||
"Error saving logs to group %s in region %s: %s", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
A simple handler which does a print on the "body" key of the event passed in. | ||
Can be used to log different payloads, to check for the correct format in cloudwatch logs | ||
""" | ||
|
||
|
||
def handler(event, context): | ||
# Just print the log line that was passed to lambda | ||
print(event["body"]) | ||
return event |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a test for the empty lines case?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but cloud watch does not accept empty lines. The logic matches the one in
store_cloudwatch_logs
though, so we at least do not get worse in terms of parity. We should write tests for this though, when we rework the logging in general.