Skip to content

Support sockets as transport for framed telemetry #378

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
Nov 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URLClassLoader;
import java.nio.file.Paths;
Expand Down Expand Up @@ -169,16 +171,31 @@ public static String getEnvOrExit(String envVariableName) {

protected static URLClassLoader customerClassLoader;

/**
* convert an integer into a FileDescriptor object using reflection to access private members.
*/
private static FileDescriptor intToFd(int fd) throws RuntimeException {
try {
Class<FileDescriptor> clazz = FileDescriptor.class;
Constructor<FileDescriptor> c = clazz.getDeclaredConstructor(new Class<?>[] { Integer.TYPE });
c.setAccessible(true);
return c.newInstance(new Integer(fd));
} catch(Exception e) {
throw new RuntimeException(e);
}
}

private static LogSink createLogSink() {
final String fd = System.getenv("_LAMBDA_TELEMETRY_LOG_FD");
if(fd == null) {
final String fdStr = System.getenv("_LAMBDA_TELEMETRY_LOG_FD");
if(fdStr == null) {
return new StdOutLogSink();
}

try {
File pipeFdFile = Paths.get("/proc", "self", "fd", fd).toFile();
return new FramedTelemetryLogSink(pipeFdFile);
} catch (IOException e) {
int fdInt = Integer.parseInt(fdStr);
FileDescriptor fd = intToFd(fdInt);
return new FramedTelemetryLogSink(fd);
} catch (Exception e) {
return new StdOutLogSink();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

package com.amazonaws.services.lambda.runtime.api.client.logging;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -31,8 +31,8 @@ public class FramedTelemetryLogSink implements LogSink {
private final FileOutputStream logOutputStream;
private final ByteBuffer headerBuf;

public FramedTelemetryLogSink(File file) throws IOException {
this.logOutputStream = new FileOutputStream(file);
public FramedTelemetryLogSink(FileDescriptor fd) throws IOException {
this.logOutputStream = new FileOutputStream(fd);
this.headerBuf = ByteBuffer.allocate(HEADER_LENGTH).order(ByteOrder.BIG_ENDIAN);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
Expand All @@ -29,7 +31,9 @@ public class FramedTelemetryLogSinkTest {
public void logSingleFrame() throws IOException {
byte[] message = "hello world\nsomething on a new line!\n".getBytes();
File tmpFile = tmpFolder.resolve("pipe").toFile();
try (FramedTelemetryLogSink logSink = new FramedTelemetryLogSink(tmpFile)) {
FileOutputStream fos = new FileOutputStream(tmpFile);
FileDescriptor fd = fos.getFD();
try (FramedTelemetryLogSink logSink = new FramedTelemetryLogSink(fd)) {
logSink.log(message);
}

Expand Down Expand Up @@ -63,7 +67,9 @@ public void logMultipleFrames() throws IOException {
byte[] firstMessage = "hello world\nsomething on a new line!".getBytes();
byte[] secondMessage = "hello again\nhere's another message\n".getBytes();
File tmpFile = tmpFolder.resolve("pipe").toFile();
try (FramedTelemetryLogSink logSink = new FramedTelemetryLogSink(tmpFile)) {
FileOutputStream fos = new FileOutputStream(tmpFile);
FileDescriptor fd = fos.getFD();
try (FramedTelemetryLogSink logSink = new FramedTelemetryLogSink(fd)) {
logSink.log(firstMessage);
logSink.log(secondMessage);
}
Expand Down Expand Up @@ -107,7 +113,9 @@ public void interruptedThread() throws IOException {
try {
byte[] message = "hello world\nsomething on a new line!\n".getBytes();
File tmpFile = tmpFolder.resolve("pipe").toFile();
try (FramedTelemetryLogSink logSink = new FramedTelemetryLogSink(tmpFile)) {
FileOutputStream fos = new FileOutputStream(tmpFile);
FileDescriptor fd = fos.getFD();
try (FramedTelemetryLogSink logSink = new FramedTelemetryLogSink(fd)) {
Thread.currentThread().interrupt();

logSink.log(message);
Expand Down