Skip to content

Commit 5f81c0d

Browse files
committed
reduce requests necessary for log publishing to cloudwatch logs
1 parent fd067cb commit 5f81c0d

File tree

1 file changed

+26
-4
lines changed
  • localstack/services/lambda_/invocation

1 file changed

+26
-4
lines changed

localstack/services/lambda_/invocation/logs.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import dataclasses
22
import logging
33
import threading
4+
import time
45
from queue import Queue
56
from typing import Optional, Union
67

78
from localstack.aws.connect import connect_to
89
from localstack.utils.aws.client_types import ServicePrincipal
910
from localstack.utils.bootstrap import is_api_enabled
10-
from localstack.utils.cloudwatch.cloudwatch_util import store_cloudwatch_logs
1111
from localstack.utils.threads import FuncThread
1212

1313
LOG = logging.getLogger(__name__)
@@ -50,10 +50,32 @@ def run_log_loop(self, *args, **kwargs) -> None:
5050
log_item = self.log_queue.get()
5151
if log_item is QUEUE_SHUTDOWN:
5252
return
53+
logs = log_item.logs.splitlines()
54+
# until we have a better way to have timestamps, log events have the same time for a single invocation
55+
log_events = [
56+
{"timestamp": int(time.time() * 1000), "message": log_line} for log_line in logs
57+
]
5358
try:
54-
store_cloudwatch_logs(
55-
logs_client, log_item.log_group, log_item.log_stream, log_item.logs
56-
)
59+
try:
60+
logs_client.put_log_events(
61+
logGroupName=log_item.log_group,
62+
logStreamName=log_item.log_stream,
63+
logEvents=log_events,
64+
)
65+
except logs_client.exceptions.ResourceNotFoundException:
66+
# create new log group
67+
try:
68+
logs_client.create_log_group(logGroupName=log_item.log_group)
69+
except logs_client.exceptions.ResourceAlreadyExistsException:
70+
pass
71+
logs_client.create_log_stream(
72+
logGroupName=log_item.log_group, logStreamName=log_item.log_stream
73+
)
74+
logs_client.put_log_events(
75+
logGroupName=log_item.log_group,
76+
logStreamName=log_item.log_stream,
77+
logEvents=log_events,
78+
)
5779
except Exception as e:
5880
LOG.warning(
5981
"Error saving logs to group %s in region %s: %s",

0 commit comments

Comments
 (0)