Skip to content

Commit f3579e2

Browse files
authored
Merge pull request #195 from fnproject/logFrame
Adding env vars to be logged before each invocation
2 parents afca0c0 + b5da35d commit f3579e2

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

runtime/src/main/java/com/fnproject/fn/runtime/MethodFunctionInvoker.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@
1717
*/
1818
public class MethodFunctionInvoker implements FunctionInvoker {
1919

20+
/*
21+
* If enabled, print the logging framing content
22+
*/
23+
public void logFramer(FunctionRuntimeContext rctx, InputEvent evt) {
24+
String framer = rctx.getConfigurationByKey("FN_LOGFRAME_NAME").orElse("");
25+
26+
if (framer != "") {
27+
String valueSrc = rctx.getConfigurationByKey("FN_LOGFRAME_HDR").orElse("");
28+
29+
if (valueSrc != "") {
30+
String id = evt.getHeaders().get(valueSrc).orElse("");
31+
if (id != "") {
32+
System.out.println(framer + "=" + id);
33+
System.err.println(framer + "=" + id);
34+
}
35+
}
36+
}
37+
}
38+
39+
2040
/**
2141
* Invoke the function wrapped by this loader
2242
*
@@ -33,6 +53,8 @@ public Optional<OutputEvent> tryInvoke(InvocationContext ctx, InputEvent evt) th
3353

3454
Object rawResult;
3555

56+
logFramer(runtimeContext, evt);
57+
3658
try {
3759
rawResult = method.getTargetMethod().invoke(ctx.getRuntimeContext().getInvokeInstance().orElse(null), userFunctionParams);
3860
} catch (IllegalAccessException | InvocationTargetException e) {

runtime/src/test/java/com/fnproject/fn/runtime/EndToEndInvokeTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ public void shouldReadBytesOnDefaultCodec() throws Exception {
211211

212212
}
213213

214+
@Test
215+
public void shouldPrintLogFrame() throws Exception {
216+
fn.setConfig("FN_LOGFRAME_NAME", "containerID");
217+
fn.setConfig("FN_LOGFRAME_HDR", "fnID");
218+
fn.givenEvent().withHeader("fnID", "fnIDVal").withBody( "Hello world!").enqueue();
219+
220+
fn.thenRun(TestFn.class, "fnEcho");
221+
assertThat(fn.getOnlyOutputAsString()).isEqualTo("Hello world!");
222+
// stdout gets redirected to stderr - hence printing out twice
223+
assertThat(fn.getStdErrAsString()).isEqualTo("containerID=fnIDVal\ncontainerID=fnIDVal\n");
224+
225+
}
226+
214227

215228
@Test
216229
public void shouldWriteBytesOnDefaultCodec() throws Exception {

0 commit comments

Comments
 (0)