Skip to content

Commit 0e8ba92

Browse files
committed
WIP adding logframer and tests to fdk-java, before each function invocation
1 parent afca0c0 commit 0e8ba92

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@
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+
40+
2041
/**
2142
* Invoke the function wrapped by this loader
2243
*
@@ -33,6 +54,8 @@ public Optional<OutputEvent> tryInvoke(InvocationContext ctx, InputEvent evt) th
3354

3455
Object rawResult;
3556

57+
logFramer(runtimeContext, evt);
58+
3659
try {
3760
rawResult = method.getTargetMethod().invoke(ctx.getRuntimeContext().getInvokeInstance().orElse(null), userFunctionParams);
3861
} catch (IllegalAccessException | InvocationTargetException e) {

testing-junit4/src/test/java/com/fnproject/fn/testing/FnTestingRuleTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,22 @@ public void shouldHandleBodyAsInputStream() throws IOException {
250250
Assertions.assertThat(capturedBodies.get(0)).isEqualTo("FOO BAR".getBytes());
251251
}
252252

253+
@Test
254+
public void shouldPrintLogFrame() throws Exception {
255+
fn.setConfig("FN_LOGFRAME_NAME", "containerID");
256+
fn.setConfig("FN_LOGFRAME_HDR", "fnID");
257+
fn.givenEvent().withBody("Hello World").enqueue();
258+
259+
fn.thenRun(TestFn.class, "copyConfiguration");
260+
Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_NAME", "containerID");
261+
Assertions.assertThat(configuration).containsEntry("FN_LOGFRAME_HDR", "fnID");
262+
263+
FnResult result = fn.getOnlyResult();
264+
Assertions.assertThat(result.getBodyAsString()).isEqualTo("containerID=fnID\nHello World");
265+
// assertThat(fn.getOnlyOutputAsString()).isEqualTo("containerID=fnID\nHello World");
266+
}
267+
268+
253269
// TODO move this to HTTP gateway
254270
// @Test
255271
// public void shouldLeaveQueryParamtersOffIfNotSpecified() {

0 commit comments

Comments
 (0)