Skip to content

Commit b79ff9c

Browse files
author
Davide Melfi
committed
chore: enabling different format outputs
1 parent 5c6d3fa commit b79ff9c

File tree

3 files changed

+78
-26
lines changed

3 files changed

+78
-26
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.amazonaws.services.lambda.extension;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class Constants {
7+
8+
private static final String DEFAULT_AWS_LAMBDA_PROFILER_START_COMMAND =
9+
"start,event=wall,interval=1us";
10+
private static final String DEFAULT_AWS_LAMBDA_PROFILER_STOP_COMMAND =
11+
"stop,file=%s,include=*AWSLambda.main,include=start_thread";
12+
public static final String PROFILER_START_COMMAND =
13+
System.getenv().getOrDefault(
14+
"AWS_LAMBDA_PROFILER_START_COMMAND",
15+
DEFAULT_AWS_LAMBDA_PROFILER_START_COMMAND
16+
);
17+
public static final String PROFILER_STOP_COMMAND =
18+
System.getenv().getOrDefault(
19+
"AWS_LAMBDA_PROFILER_STOP_COMMAND",
20+
DEFAULT_AWS_LAMBDA_PROFILER_STOP_COMMAND
21+
);
22+
23+
public static String getFilePathFromEnv(){
24+
Pattern pattern = Pattern.compile("file=([^,]+)");
25+
Matcher matcher = pattern.matcher(PROFILER_START_COMMAND);
26+
27+
return matcher.find() ? matcher.group(1) : "/tmp/profiling-data-%s.html";
28+
}
29+
}

experimental/aws-lambda-java-profiler/extension/src/main/java/com/amazonaws/services/lambda/extension/PreMain.java

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,57 @@
22
// SPDX-License-Identifier: MIT-0
33
package com.amazonaws.services.lambda.extension;
44

5+
import com.sun.net.httpserver.HttpExchange;
6+
import com.sun.net.httpserver.HttpHandler;
7+
import com.sun.net.httpserver.HttpServer;
58
import java.io.File;
69
import java.io.IOException;
710
import java.io.OutputStream;
811
import java.lang.instrument.Instrumentation;
912
import java.net.InetSocketAddress;
1013
import java.nio.charset.StandardCharsets;
14+
import one.profiler.AsyncProfiler;
1115

12-
import com.sun.net.httpserver.HttpExchange;
13-
import com.sun.net.httpserver.HttpHandler;
14-
import com.sun.net.httpserver.HttpServer;
16+
import static com.amazonaws.services.lambda.extension.Constants.PROFILER_START_COMMAND;
17+
import static com.amazonaws.services.lambda.extension.Constants.PROFILER_STOP_COMMAND;
18+
19+
public class PreMain {
1520

16-
import one.profiler.AsyncProfiler;
1721

18-
public class PreMain {
22+
private static final String INTERNAL_COMMUNICATION_PORT =
23+
System.getenv().getOrDefault(
24+
"AWS_LAMBDA_PROFILER_COMMUNICATION_PORT",
25+
"1234"
26+
);
1927

20-
private static final String DEFAULT_AWS_LAMBDA_PROFILER_START_COMMAND = "start,event=wall,interval=1us";
21-
private static final String DEFAULT_AWS_LAMBDA_PROFILER_STOP_COMMAND = "stop,file=%s,include=*AWSLambda.main,include=start_thread";
22-
private static final String PROFILER_START_COMMAND = System.getenv().getOrDefault("AWS_LAMBDA_PROFILER_START_COMMAND", DEFAULT_AWS_LAMBDA_PROFILER_START_COMMAND);
23-
private static final String PROFILER_STOP_COMMAND = System.getenv().getOrDefault("AWS_LAMBDA_PROFILER_STOP_COMMAND", DEFAULT_AWS_LAMBDA_PROFILER_STOP_COMMAND);
24-
private static final String INTERNAL_COMMUNICATION_PORT = System.getenv().getOrDefault("AWS_LAMBDA_PROFILER_COMMUNICATION_PORT", "1234");
28+
29+
private String filepath;
2530

2631
public static void premain(String agentArgs, Instrumentation inst) {
2732
Logger.debug("premain is starting");
28-
if(!createFileIfNotExist("/tmp/aws-lambda-java-profiler")) {
33+
if (!createFileIfNotExist("/tmp/aws-lambda-java-profiler")) {
2934
Logger.debug("starting the profiler for coldstart");
3035
startProfiler();
3136
registerShutdownHook();
3237
try {
3338
Integer port = Integer.parseInt(INTERNAL_COMMUNICATION_PORT);
3439
Logger.debug("using profile communication port = " + port);
35-
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
40+
HttpServer server = HttpServer.create(
41+
new InetSocketAddress(port),
42+
0
43+
);
3644
server.createContext("/profiler/start", new StartProfiler());
3745
server.createContext("/profiler/stop", new StopProfiler());
3846
server.setExecutor(null); // Use the default executor
3947
server.start();
40-
} catch(Exception e) {
48+
} catch (Exception e) {
4149
e.printStackTrace();
4250
}
4351
}
4452
}
4553

4654
private static boolean createFileIfNotExist(String filePath) {
47-
File file = new File(filePath);
55+
File file = new File(filePath);
4856
try {
4957
return file.createNewFile();
5058
} catch (IOException e) {
@@ -54,10 +62,13 @@ private static boolean createFileIfNotExist(String filePath) {
5462
}
5563

5664
public static class StopProfiler implements HttpHandler {
65+
5766
@Override
5867
public void handle(HttpExchange exchange) throws IOException {
5968
Logger.debug("hit /profiler/stop");
60-
final String fileName = exchange.getRequestHeaders().getFirst(ExtensionMain.HEADER_NAME);
69+
final String fileName = exchange
70+
.getRequestHeaders()
71+
.getFirst(ExtensionMain.HEADER_NAME);
6172
stopProfiler(fileName);
6273
String response = "ok";
6374
exchange.sendResponseHeaders(200, response.length());
@@ -68,6 +79,7 @@ public void handle(HttpExchange exchange) throws IOException {
6879
}
6980

7081
public static class StartProfiler implements HttpHandler {
82+
7183
@Override
7284
public void handle(HttpExchange exchange) throws IOException {
7385
Logger.debug("hit /profiler/start");
@@ -80,21 +92,32 @@ public void handle(HttpExchange exchange) throws IOException {
8092
}
8193
}
8294

83-
8495
public static void stopProfiler(String fileNameSuffix) {
8596
try {
86-
final String fileName = String.format("/tmp/profiling-data-%s.html", fileNameSuffix);
87-
Logger.debug("stopping the profiler with filename = " + fileName + " with command = " + PROFILER_STOP_COMMAND);
88-
AsyncProfiler.getInstance().execute(String.format(PROFILER_STOP_COMMAND, fileName));
89-
} catch(Exception e) {
97+
final String fileName = String.format(
98+
Constants.getFilePathFromEnv(),
99+
fileNameSuffix
100+
);
101+
Logger.debug(
102+
"stopping the profiler with filename = " +
103+
fileName +
104+
" with command = " +
105+
PROFILER_STOP_COMMAND
106+
);
107+
AsyncProfiler.getInstance().execute(
108+
String.format(PROFILER_STOP_COMMAND, fileName)
109+
);
110+
} catch (Exception e) {
90111
Logger.error("could not stop the profiler");
91112
e.printStackTrace();
92113
}
93114
}
94115

95116
public static void startProfiler() {
96117
try {
97-
Logger.debug("staring the profiler with command = " + PROFILER_START_COMMAND);
118+
Logger.debug(
119+
"staring the profiler with command = " + PROFILER_START_COMMAND
120+
);
98121
AsyncProfiler.getInstance().execute(PROFILER_START_COMMAND);
99122
} catch (IOException e) {
100123
throw new RuntimeException(e);
@@ -103,8 +126,9 @@ public static void startProfiler() {
103126

104127
public static void registerShutdownHook() {
105128
Logger.debug("registering shutdown hook");
106-
Thread shutdownHook = new Thread(new ShutdownHook(PROFILER_STOP_COMMAND));
129+
Thread shutdownHook = new Thread(
130+
new ShutdownHook(PROFILER_STOP_COMMAND)
131+
);
107132
Runtime.getRuntime().addShutdownHook(shutdownHook);
108133
}
109-
110-
}
134+
}

experimental/aws-lambda-java-profiler/extension/src/main/java/com/amazonaws/services/lambda/extension/S3Manager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import java.io.File;
66
import java.time.format.DateTimeFormatter;
7-
import java.time.Instant;
87
import java.time.LocalDate;
98

109
import software.amazon.awssdk.core.sync.RequestBody;
@@ -39,7 +38,7 @@ public void upload(String fileName, boolean isShutDownEvent) {
3938
.bucket(bucketName)
4039
.key(key)
4140
.build();
42-
File file = new File(String.format("/tmp/profiling-data-%s.html", suffix));
41+
File file = new File(String.format(Constants.getFilePathFromEnv(), suffix));
4342
if (file.exists()) {
4443
Logger.debug("file size is " + file.length());
4544
RequestBody requestBody = RequestBody.fromFile(file);

0 commit comments

Comments
 (0)