2828import com .google .common .truth .Expect ;
2929import com .google .gson .Gson ;
3030import com .google .gson .JsonObject ;
31+ import com .google .gson .JsonParser ;
32+ import com .google .gson .stream .JsonReader ;
3133import io .cloudevents .CloudEvent ;
3234import io .cloudevents .core .builder .CloudEventBuilder ;
3335import io .cloudevents .core .format .EventFormat ;
3941import java .io .IOException ;
4042import java .io .InputStream ;
4143import java .io .InputStreamReader ;
44+ import java .io .StringReader ;
4245import java .io .UncheckedIOException ;
4346import java .net .ServerSocket ;
4447import java .net .URI ;
@@ -89,6 +92,8 @@ public class IntegrationTest {
8992 @ Rule public final TestName testName = new TestName ();
9093
9194 private static final String SERVER_READY_STRING = "Started ServerConnector" ;
95+ private static final String EXECUTION_ID_HTTP_HEADER = "HTTP_FUNCTION_EXECUTION_ID" ;
96+ private static final String EXECUTION_ID = "1234abcd" ;
9297
9398 private static final ExecutorService EXECUTOR = Executors .newCachedThreadPool ();
9499
@@ -286,14 +291,18 @@ public void exceptionHttp() throws Exception {
286291 String exceptionExpectedOutput =
287292 "\" severity\" : \" ERROR\" , \" logging.googleapis.com/sourceLocation\" : {\" file\" :"
288293 + " \" com/google/cloud/functions/invoker/HttpFunctionExecutor.java\" , \" method\" :"
289- + " \" service\" }, \" message\" : \" Failed to execute"
294+ + " \" service\" }, \" execution_id\" : \" "
295+ + EXECUTION_ID
296+ + "\" ,"
297+ + " \" message\" : \" Failed to execute"
290298 + " com.google.cloud.functions.invoker.testfunctions.ExceptionHttp\\ n"
291299 + "java.lang.RuntimeException: exception thrown for test" ;
292300 testHttpFunction (
293301 fullTarget ("ExceptionHttp" ),
294302 ImmutableList .of (
295303 TestCase .builder ()
296304 .setExpectedResponseCode (500 )
305+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
297306 .setExpectedOutput (exceptionExpectedOutput )
298307 .build ()));
299308 }
@@ -303,7 +312,10 @@ public void exceptionBackground() throws Exception {
303312 String exceptionExpectedOutput =
304313 "\" severity\" : \" ERROR\" , \" logging.googleapis.com/sourceLocation\" : {\" file\" :"
305314 + " \" com/google/cloud/functions/invoker/BackgroundFunctionExecutor.java\" , \" method\" :"
306- + " \" service\" }, \" message\" : \" Failed to execute"
315+ + " \" service\" }, \" execution_id\" : \" "
316+ + EXECUTION_ID
317+ + "\" , "
318+ + "\" message\" : \" Failed to execute"
307319 + " com.google.cloud.functions.invoker.testfunctions.ExceptionBackground\\ n"
308320 + "java.lang.RuntimeException: exception thrown for test" ;
309321
@@ -317,6 +329,7 @@ public void exceptionBackground() throws Exception {
317329 ImmutableList .of (
318330 TestCase .builder ()
319331 .setRequestText (gcfRequestText )
332+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
320333 .setExpectedResponseCode (500 )
321334 .setExpectedOutput (exceptionExpectedOutput )
322335 .build ()),
@@ -359,25 +372,37 @@ public void stackDriverLogging() throws Exception {
359372 + "\" logging.googleapis.com/sourceLocation\" : "
360373 + "{\" file\" : \" com/google/cloud/functions/invoker/testfunctions/Log.java\" ,"
361374 + " \" method\" : \" service\" },"
375+ + " \" execution_id\" : \" "
376+ + EXECUTION_ID
377+ + "\" ,"
362378 + " \" message\" : \" blim\" }" ;
363379 TestCase simpleTestCase =
364- TestCase .builder ().setUrl ("/?message=blim" ).setExpectedOutput (simpleExpectedOutput ).build ();
380+ TestCase .builder ()
381+ .setUrl ("/?message=blim" )
382+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
383+ .setExpectedOutput (simpleExpectedOutput )
384+ .build ();
365385 String quotingExpectedOutput = "\" message\" : \" foo\\ nbar\\ \" " ;
366386 TestCase quotingTestCase =
367387 TestCase .builder ()
368388 .setUrl ("/?message=" + URLEncoder .encode ("foo\n bar\" " , "UTF-8" ))
389+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
369390 .setExpectedOutput (quotingExpectedOutput )
370391 .build ();
371392 String exceptionExpectedOutput =
372393 "{\" severity\" : \" ERROR\" , "
373394 + "\" logging.googleapis.com/sourceLocation\" : "
374395 + "{\" file\" : \" com/google/cloud/functions/invoker/testfunctions/Log.java\" , "
375396 + "\" method\" : \" service\" }, "
397+ + "\" execution_id\" : \" "
398+ + EXECUTION_ID
399+ + "\" , "
376400 + "\" message\" : \" oops\\ njava.lang.Exception: disaster\\ n"
377401 + " at com.google.cloud.functions.invoker.testfunctions.Log.service(Log.java:" ;
378402 TestCase exceptionTestCase =
379403 TestCase .builder ()
380404 .setUrl ("/?message=oops&level=severe&exception=disaster" )
405+ .setHttpHeaders (ImmutableMap .of (EXECUTION_ID_HTTP_HEADER , EXECUTION_ID ))
381406 .setExpectedOutput (exceptionExpectedOutput )
382407 .build ();
383408 testHttpFunction (
@@ -753,7 +778,11 @@ private void testFunction(
753778 for (TestCase testCase : testCases ) {
754779 testCase
755780 .expectedOutput ()
756- .ifPresent (output -> expect .that (serverProcess .output ()).contains (output ));
781+ .ifPresent (
782+ (output ) -> {
783+ expect .that (serverProcess .output ()).contains (output );
784+ parseLogJson (serverProcess .output ());
785+ });
757786 }
758787 // Wait for the output monitor task to terminate. If it threw an exception, we will get an
759788 // ExecutionException here.
@@ -842,7 +871,9 @@ private ServerProcess startServer(
842871 "FUNCTION_SIGNATURE_TYPE" ,
843872 signatureType .toString (),
844873 "FUNCTION_TARGET" ,
845- target );
874+ target ,
875+ "LOG_EXECUTION_ID" ,
876+ "true" );
846877 processBuilder .environment ().putAll (environment );
847878 processBuilder .environment ().putAll (environmentVariables );
848879 Process serverProcess = processBuilder .start ();
@@ -879,4 +910,12 @@ private void monitorOutput(
879910 throw new UncheckedIOException (e );
880911 }
881912 }
913+
914+ // Attempt to parse Json object, throws on parse failure
915+ private void parseLogJson (String json ) throws RuntimeException {
916+ System .out .println ("trying to parse the following object " );
917+ System .out .println (json );
918+ JsonReader reader = new JsonReader (new StringReader (json ));
919+ JsonParser .parseReader (reader );
920+ }
882921}
0 commit comments