Skip to content

Using the raw fd directly rather than opening the fd pseudo file #56

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

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions awslambdaric/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ class FramedTelemetryLogSink(object):
big-endian.
"""

def __init__(self, filename):
self.filename = filename
def __init__(self, fd):
self.fd = int(fd)
self.frame_type = 0xA55A0001 .to_bytes(4, "big")

def __enter__(self):
self.file = open(self.filename, "wb", 0)
self.file = os.fdopen(self.fd, 'wb', 0)
return self

def __exit__(self, exc_type, exc_value, exc_tb):
Expand Down Expand Up @@ -355,7 +355,7 @@ def create_log_sink():
if "_LAMBDA_TELEMETRY_LOG_FD" in os.environ:
fd = os.environ["_LAMBDA_TELEMETRY_LOG_FD"]
del os.environ["_LAMBDA_TELEMETRY_LOG_FD"]
return FramedTelemetryLogSink("/proc/self/fd/" + fd)
return FramedTelemetryLogSink(fd)

else:
return StandardLogSink()
Expand Down
16 changes: 8 additions & 8 deletions tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ def test_log_error_standard_log_sink(self, mock_stdout):

def test_log_error_framed_log_sink(self):
with NamedTemporaryFile() as temp_file:
with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink:
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink:
err_to_log = bootstrap.make_error("Error message", "ErrorType", None)
bootstrap.log_error(err_to_log, log_sink)

Expand Down Expand Up @@ -949,7 +949,7 @@ def test_log_error_indentation_standard_log_sink(self, mock_stdout):

def test_log_error_indentation_framed_log_sink(self):
with NamedTemporaryFile() as temp_file:
with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink:
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink:
err_to_log = bootstrap.make_error(
"Error message", "ErrorType", [" line1 ", " line2 ", " "]
)
Expand Down Expand Up @@ -984,7 +984,7 @@ def test_log_error_empty_stacktrace_line_standard_log_sink(self, mock_stdout):

def test_log_error_empty_stacktrace_line_framed_log_sink(self):
with NamedTemporaryFile() as temp_file:
with bootstrap.FramedTelemetryLogSink(temp_file.name) as log_sink:
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as log_sink:
err_to_log = bootstrap.make_error(
"Error message", "ErrorType", ["line1", "", "line2"]
)
Expand Down Expand Up @@ -1070,19 +1070,19 @@ def test_create_unbuffered_log_sinks(self, mock_stdout):
self.assertEqual(mock_stdout.getvalue(), "log")

def test_create_framed_telemetry_log_sinks(self):
fd = "test_fd"
os.environ["_LAMBDA_TELEMETRY_LOG_FD"] = fd
fd = 3
os.environ["_LAMBDA_TELEMETRY_LOG_FD"] = "3"

actual = bootstrap.create_log_sink()

self.assertIsInstance(actual, bootstrap.FramedTelemetryLogSink)
self.assertEqual(actual.filename, "/proc/self/fd/" + fd)
self.assertEqual(actual.fd, fd)
self.assertFalse("_LAMBDA_TELEMETRY_LOG_FD" in os.environ)

def test_single_frame(self):
with NamedTemporaryFile() as temp_file:
message = "hello world\nsomething on a new line!\n"
with bootstrap.FramedTelemetryLogSink(temp_file.name) as ls:
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as ls:
ls.log(message)
with open(temp_file.name, "rb") as f:
content = f.read()
Expand All @@ -1101,7 +1101,7 @@ def test_multiple_frame(self):
first_message = "hello world\nsomething on a new line!"
second_message = "hello again\nhere's another message\n"

with bootstrap.FramedTelemetryLogSink(temp_file.name) as ls:
with bootstrap.FramedTelemetryLogSink(os.open(temp_file.name, os.O_CREAT | os.O_RDWR)) as ls:
ls.log(first_message)
ls.log(second_message)

Expand Down