1010import java .io .File ;
1111import java .io .IOException ;
1212import java .net .HttpURLConnection ;
13- import java .time .Duration ;
1413import java .time .Instant ;
1514import java .util .ArrayList ;
1615import 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 ) {
0 commit comments