Skip to content

Commit c61c77c

Browse files
committed
Added support for logging
1 parent 61ec546 commit c61c77c

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/main/java/Handler.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.io.File;
1111
import java.io.IOException;
1212
import java.net.HttpURLConnection;
13-
import java.time.Duration;
1413
import java.time.Instant;
1514
import java.util.ArrayList;
1615
import java.util.List;
@@ -21,10 +20,11 @@ public class Handler {
2120
private static final String TMP_PATH = "/tmp/";
2221
private static final String BUCKET_KEY = "bucket";
2322
private static final String PREFIX_KEY = "prefix";
23+
private static final Metrics metrics = new Metrics();
2424

2525
public Response handleRequest(Event event, Context context) {
2626

27-
Instant instant1 = Instant.now();
27+
metrics.lambdaStart = Instant.now();
2828
LambdaLogger logger = context.getLogger();
2929
logger.log("body: " + event.getBody());
3030

@@ -44,15 +44,25 @@ public Response handleRequest(Event event, Context context) {
4444
execute(createCommand(request), logger);
4545
uploadData(request, logger);
4646
} catch (Exception e) {
47+
context.getLogger().log("Execution failed.");
4748
return handleException(e, request);
4849
}
50+
metrics.lambdaEnd = Instant.now();
51+
final String metricsString = "lambda start: " + metrics.lambdaStart.toEpochMilli() + " lambda end: " + metrics.lambdaEnd.toEpochMilli() +
52+
" download start: " + metrics.downloadStart.toEpochMilli() + " download end: " + metrics.downloadEnd.toEpochMilli() +
53+
" execution start: " + metrics.executionStart.toEpochMilli() + " execution end: " + metrics.executionEnd.toEpochMilli() +
54+
" upload start: " + metrics.uploadStart.toEpochMilli() + " upload end: " + metrics.uploadEnd.toEpochMilli();
55+
if (request.getLogName() != null) {
56+
S3Utils.S3.putObject(request.getOptions().get(BUCKET_KEY), "logs/" + request.getLogName(), metricsString);
57+
}
4958
Response response = new Response();
5059
response.setStatusCode(HttpURLConnection.HTTP_OK);
51-
response.setBody("Execution of " + request.getExecutable() + " successful, duration: " + Duration.between(instant1, Instant.now()).getSeconds() + " seconds");
60+
response.setBody(metricsString);
5261
return response;
5362
}
5463

5564
private void downloadData(Request request, LambdaLogger logger) throws IOException {
65+
metrics.downloadStart = Instant.now();
5666
for (Map<String, Object> input : request.getInputs()) {
5767
String fileName = input.get("name").toString();
5868
String key = request.getOptions().get(PREFIX_KEY) + "/" + fileName;
@@ -67,6 +77,7 @@ private void downloadExecutable(Request request, LambdaLogger logger) throws IOE
6777
String key = request.getOptions().get(PREFIX_KEY) + "/" + request.getExecutable();
6878
S3Object s3Object = S3Utils.getObject(request.getOptions().get(BUCKET_KEY), key);
6979
S3Utils.saveToFile(s3Object, TMP_PATH + request.getExecutable());
80+
metrics.downloadEnd = Instant.now();
7081
}
7182

7283
private List<String> createCommand(Request request) {
@@ -77,6 +88,7 @@ private List<String> createCommand(Request request) {
7788
}
7889

7990
private void execute(List<String> command, LambdaLogger logger) throws Exception {
91+
metrics.executionStart = Instant.now();
8092
logger.log("Executing: " + String.join(" ", command));
8193

8294
Process chmod = Runtime.getRuntime().exec("chmod -R 777 " + TMP_PATH);
@@ -91,16 +103,19 @@ private void execute(List<String> command, LambdaLogger logger) throws Exception
91103

92104
logger.log("Stdout: " + outputMsg);
93105
logger.log("Stderr: " + errorMsg);
106+
metrics.executionEnd = Instant.now();
94107
}
95108

96109
private void uploadData(Request request, LambdaLogger logger) {
110+
metrics.uploadStart = Instant.now();
97111
for (Map<String, String> input : request.getOutputs()) {
98112
String fileName = input.get("name");
99113
String filePath = TMP_PATH + fileName;
100114
String key = request.getOptions().get(PREFIX_KEY) + "/" + fileName;
101115
logger.log("Uploading to " + request.getOptions().get(BUCKET_KEY) + "/" + key + " from " + filePath);
102116
S3Utils.putObject(request.getOptions().get(BUCKET_KEY), key, filePath);
103117
}
118+
metrics.uploadEnd = Instant.now();
104119
}
105120

106121
private Response handleException(Exception e, Request request) {

src/main/java/Metrics.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.time.Instant;
2+
3+
class Metrics {
4+
5+
Instant lambdaStart;
6+
Instant lambdaEnd;
7+
Instant downloadStart;
8+
Instant downloadEnd;
9+
Instant executionStart;
10+
Instant executionEnd;
11+
Instant uploadStart;
12+
Instant uploadEnd;
13+
14+
}

src/main/java/S3Utils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
import java.util.logging.Level;
1313
import java.util.logging.Logger;
1414

15-
public class S3Utils {
15+
class S3Utils {
1616

1717
private static final Logger logger = Logger.getGlobal();
18-
private static final AmazonS3 S3 = AmazonS3ClientBuilder.defaultClient();
18+
static final AmazonS3 S3 = AmazonS3ClientBuilder.defaultClient();
1919

2020
private S3Utils() {
2121
}
2222

23-
public static S3Object getObject(String bucketName, String key) {
23+
static S3Object getObject(String bucketName, String key) {
2424
S3Object s3Object;
2525
try {
2626
s3Object = S3.getObject(bucketName, key);
@@ -31,7 +31,7 @@ public static S3Object getObject(String bucketName, String key) {
3131
return s3Object;
3232
}
3333

34-
public static void putObject(String bucketName, String key, String filePath) {
34+
static void putObject(String bucketName, String key, String filePath) {
3535
File file = new File(filePath);
3636
try {
3737
S3.putObject(bucketName, key, file);
@@ -41,7 +41,7 @@ public static void putObject(String bucketName, String key, String filePath) {
4141
}
4242
}
4343

44-
public static void saveToFile(S3Object s3Object, String fileName) throws IOException {
44+
static void saveToFile(S3Object s3Object, String fileName) throws IOException {
4545
try (InputStream in = s3Object.getObjectContent()) {
4646
Objects.requireNonNull(s3Object);
4747
Files.copy(in, Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING);

src/main/java/payloads/Request.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class Request {
77

88
private String executable;
9+
private String logName;
910
private List<String> args;
1011
private Map<String, String> env;
1112
private List<Map<String, Object>> inputs;
@@ -58,6 +59,14 @@ public List<Map<String, String>> getOutputs() {
5859

5960
public Map<String, String> getOptions() {
6061
return options;
62+
}
63+
64+
public void setLogName(String logName) {
65+
this.logName = logName;
66+
}
6167

68+
public String getLogName() {
69+
return this.logName;
6270
}
71+
6372
}

0 commit comments

Comments
 (0)