-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix: remove log & data separation from base_local_service #4563
Changes from all commits
0231797
b30312f
6fc40ed
2085203
1e74f9f
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,7 +1,8 @@ | ||
"""Base class for all Services that interact with Local Lambda""" | ||
|
||
import io | ||
import json | ||
import logging | ||
from typing import Tuple | ||
|
||
from flask import Response | ||
|
||
|
@@ -81,7 +82,7 @@ def service_response(body, headers, status_code): | |
|
||
class LambdaOutputParser: | ||
@staticmethod | ||
def get_lambda_output(stdout_stream): | ||
def get_lambda_output(stdout_stream: io.BytesIO) -> Tuple[str, bool]: | ||
""" | ||
This method will extract read the given stream and return the response from Lambda function separated out | ||
from any log statements it might have outputted. Logs end up in the stdout stream if the Lambda function | ||
|
@@ -96,39 +97,18 @@ def get_lambda_output(stdout_stream): | |
------- | ||
str | ||
String data containing response from Lambda function | ||
str | ||
String data containng logs statements, if any. | ||
bool | ||
If the response is an error/exception from the container | ||
""" | ||
# We only want the last line of stdout, because it's possible that | ||
# the function may have written directly to stdout using | ||
# System.out.println or similar, before docker-lambda output the result | ||
stdout_data = stdout_stream.getvalue().rstrip(b"\n") | ||
|
||
# Usually the output is just one line and contains response as JSON string, but if the Lambda function | ||
# wrote anything directly to stdout, there will be additional lines. So just extract the last line as | ||
# response and everything else as log output. | ||
lambda_response = stdout_data | ||
lambda_logs = None | ||
|
||
last_line_position = stdout_data.rfind(b"\n") | ||
if last_line_position >= 0: | ||
# So there are multiple lines. Separate them out. | ||
# Everything but the last line are logs | ||
lambda_logs = stdout_data[:last_line_position] | ||
# Last line is Lambda response. Make sure to strip() so we get rid of extra whitespaces & newlines around | ||
lambda_response = stdout_data[last_line_position:].strip() | ||
|
||
lambda_response = lambda_response.decode("utf-8") | ||
lambda_response = stdout_stream.getvalue().decode("utf-8") | ||
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. Do we still need the 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. I've confirmed this by running local integration tests (some of them confirms the output of the command). So I believe we don't need that particular line |
||
|
||
# When the Lambda Function returns an Error/Exception, the output is added to the stdout of the container. From | ||
# our perspective, the container returned some value, which is not always true. Since the output is the only | ||
# information we have, we need to inspect this to understand if the container returned a some data or raised an | ||
# error | ||
is_lambda_user_error_response = LambdaOutputParser.is_lambda_error_response(lambda_response) | ||
|
||
return lambda_response, lambda_logs, is_lambda_user_error_response | ||
return lambda_response, is_lambda_user_error_response | ||
|
||
@staticmethod | ||
def is_lambda_error_response(lambda_response): | ||
|
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.
I wonder if
NamedTuple
or a Dataclass would provide better readability and typing. I would not block on this but just stood out to me with the update.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.
I do agree it would be better, but I didn't want to introduce more changes to this PR, I've only added some typing into the functions that is been changed.