mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
fix(lambda): pass LoggingConfig log format and level to the runtime (#6775)
* fix(lambda): pass LoggingConfig log format and level to the runtime A function with LogFormat=JSON logged text: neither launcher set AWS_LAMBDA_LOG_FORMAT or AWS_LAMBDA_LOG_LEVEL, which the runtime interface clients read. Both launchers now pass them, and a JSON function without an ApplicationLogLevel gets INFO, the level the API reports, through one default shared with the response. Closes #6774 * docs(lambda): note the log format and level the runtime receives * refactor(lambda): build the runtime environment in one place for both launchers ContainerLauncher and KubernetesPodLauncher wrote the same AWS_LAMBDA_* lines, the LoggingConfig ones included. LambdaRuntimeEnv.of now builds them once.
This commit is contained in:
1 parent
119b7e9580
commit
481b7f8d6d
8 files changed
+98
-23
No files matched your search
@@ -978,6 +978,11 @@ exist, and `AWS_LAMBDA_LOG_GROUP_NAME` names that group. Once a group is named,
|
||||
functions can share, each stream's name carries the function's name, as on AWS:
|
||||
`2026/10/16/my-fn[$LATEST]<id>` rather than the default group's `2026/10/16/[$LATEST]<id>`.
|
||||
|
||||
The execution environment gets `AWS_LAMBDA_LOG_FORMAT` from `LogFormat` and, under `LogFormat=JSON`,
|
||||
`AWS_LAMBDA_LOG_LEVEL` from `ApplicationLogLevel` (`INFO` when unset, the level the API reports).
|
||||
The runtime interface clients read both: a Python function writes each record as a JSON object,
|
||||
`extra` keys included, and drops records under the level.
|
||||
|
||||
`DurableConfig` requires `ExecutionTimeout` on create. A durable function created without
|
||||
a `Timeout` gets `min(ExecutionTimeout, 900)`. It always logs in JSON format, and
|
||||
`LogFormat: Text` is rejected. On update the members are merged, and a function created
|
||||
|
||||
@@ -888,8 +888,7 @@ public class LambdaController {
|
||||
ObjectNode logging = node.putObject("LoggingConfig");
|
||||
logging.put("LogFormat", logFormat);
|
||||
if ("JSON".equals(logFormat)) {
|
||||
logging.put("ApplicationLogLevel",
|
||||
fn.getApplicationLogLevel() != null ? fn.getApplicationLogLevel() : "INFO");
|
||||
logging.put("ApplicationLogLevel", fn.applicationLogLevelOrDefault());
|
||||
logging.put("SystemLogLevel",
|
||||
fn.getSystemLogLevel() != null ? fn.getSystemLogLevel() : "INFO");
|
||||
}
|
||||
|
||||
+1
-11
@@ -246,17 +246,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
String lambdaRegion = extractRegionFromArn(fn.getFunctionArn(), config.defaultRegion());
|
||||
String lambdaAccountId = AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId());
|
||||
|
||||
// Build env vars
|
||||
List<String> env = new ArrayList<>();
|
||||
env.add("AWS_LAMBDA_RUNTIME_API=" + runtimeApiEndpoint);
|
||||
env.add("AWS_LAMBDA_FUNCTION_NAME=" + fn.getFunctionName());
|
||||
env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=" + fn.getMemorySize());
|
||||
env.add("AWS_LAMBDA_FUNCTION_TIMEOUT=" + fn.getTimeout());
|
||||
env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + cwLogGroup);
|
||||
env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + cwLogStream);
|
||||
if (fn.getHandler() != null && !fn.getHandler().isBlank()) {
|
||||
env.add("_HANDLER=" + fn.getHandler());
|
||||
}
|
||||
List<String> env = LambdaRuntimeEnv.of(fn, runtimeApiEndpoint, cwLogGroup, cwLogStream);
|
||||
// Region, credentials and the Floci endpoint the SDK should target: the same baseline
|
||||
// Floci gives every launched container. When the host ~/.aws is mounted (awsConfigPath),
|
||||
// the SDK discovers credentials from /opt/aws-config instead of injected credentials.
|
||||
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package io.github.hectorvent.floci.services.lambda.launcher;
|
||||
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/** The Lambda runtime environment every launcher gives an execution environment, before the SDK baseline. */
|
||||
public final class LambdaRuntimeEnv {
|
||||
|
||||
private LambdaRuntimeEnv() {
|
||||
}
|
||||
|
||||
/** Returns a mutable list, so a launcher can add or override variables after it. */
|
||||
public static List<String> of(LambdaFunction fn, String runtimeApi, String logGroup, String logStream) {
|
||||
List<String> env = new ArrayList<>();
|
||||
env.add("AWS_LAMBDA_RUNTIME_API=" + runtimeApi);
|
||||
env.add("AWS_LAMBDA_FUNCTION_NAME=" + fn.getFunctionName());
|
||||
env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=" + fn.getMemorySize());
|
||||
env.add("AWS_LAMBDA_FUNCTION_TIMEOUT=" + fn.getTimeout());
|
||||
env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + logGroup);
|
||||
env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + logStream);
|
||||
// LoggingConfig: the runtime interface clients read these to pick the log format and the
|
||||
// application log level, so without them a JSON function still logs text.
|
||||
if (fn.getLogFormat() != null) {
|
||||
env.add("AWS_LAMBDA_LOG_FORMAT=" + fn.getLogFormat());
|
||||
}
|
||||
if (fn.applicationLogLevelOrDefault() != null) {
|
||||
env.add("AWS_LAMBDA_LOG_LEVEL=" + fn.applicationLogLevelOrDefault());
|
||||
}
|
||||
if (fn.getHandler() != null && !fn.getHandler().isBlank()) {
|
||||
env.add("_HANDLER=" + fn.getHandler());
|
||||
}
|
||||
return env;
|
||||
}
|
||||
}
|
||||
+3
-10
@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ContainerHandle;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ContainerLauncher;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageResolver;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.LambdaRuntimeEnv;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.LambdaRuntimeLauncher;
|
||||
import io.github.hectorvent.floci.services.lambda.model.ContainerState;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
@@ -184,16 +185,8 @@ public class KubernetesPodLauncher implements LambdaRuntimeLauncher {
|
||||
|
||||
Optional<String> caConfigMap = ContainerCaBundle.hostPath(config).map(bundle -> ensureCaConfigMap(namespace, bundle));
|
||||
|
||||
ArrayList<String> env = new ArrayList<>();
|
||||
env.add("AWS_LAMBDA_RUNTIME_API=" + addressResolver.resolve() + ":" + runtimeApiServer.getPort());
|
||||
env.add("AWS_LAMBDA_FUNCTION_NAME=" + fn.getFunctionName());
|
||||
env.add("AWS_LAMBDA_FUNCTION_MEMORY_SIZE=" + fn.getMemorySize());
|
||||
env.add("AWS_LAMBDA_FUNCTION_TIMEOUT=" + fn.getTimeout());
|
||||
env.add("AWS_LAMBDA_LOG_GROUP_NAME=" + cwLogGroup);
|
||||
env.add("AWS_LAMBDA_LOG_STREAM_NAME=" + cwLogStream);
|
||||
if (fn.getHandler() != null && !fn.getHandler().isBlank()) {
|
||||
env.add("_HANDLER=" + fn.getHandler());
|
||||
}
|
||||
List<String> env = LambdaRuntimeEnv.of(fn,
|
||||
addressResolver.resolve() + ":" + runtimeApiServer.getPort(), cwLogGroup, cwLogStream);
|
||||
env.addAll(awsEnv.sdkBaselineEnv(region, Optional.empty(), addressResolver.flociBaseUrl(),
|
||||
Optional.empty(), AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId())));
|
||||
if (fn.getEnvironment() != null) {
|
||||
|
||||
@@ -205,6 +205,17 @@ public class LambdaFunction {
|
||||
public String getApplicationLogLevel() { return applicationLogLevel; }
|
||||
public void setApplicationLogLevel(String applicationLogLevel) { this.applicationLogLevel = applicationLogLevel; }
|
||||
|
||||
/**
|
||||
* The level a JSON-format function logs at: its own, else INFO, as Lambda reports it. None under
|
||||
* Text, where log levels are refused.
|
||||
*/
|
||||
public String applicationLogLevelOrDefault() {
|
||||
if (applicationLogLevel != null) {
|
||||
return applicationLogLevel;
|
||||
}
|
||||
return "JSON".equals(logFormat) ? "INFO" : null;
|
||||
}
|
||||
|
||||
public String getSystemLogLevel() { return systemLogLevel; }
|
||||
public void setSystemLogLevel(String systemLogLevel) { this.systemLogLevel = systemLogLevel; }
|
||||
|
||||
|
||||
+24
@@ -333,6 +333,30 @@ class ContainerLauncherTest {
|
||||
.filter(value -> value.startsWith("AWS_LAMBDA_FUNCTION_VERSION=")).toList());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"JSON, DEBUG, DEBUG", "JSON, , INFO", "Text, , "})
|
||||
void launchPassesTheLoggingConfigToTheRuntime(String logFormat, String level, String runtimeLevel) {
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
fn.setFunctionName("logging-config-fn");
|
||||
fn.setHotReloadHostPath("/home/ci/code");
|
||||
fn.setRuntime("python3.13");
|
||||
fn.setHandler("fn.handler");
|
||||
fn.setLogFormat(logFormat);
|
||||
fn.setApplicationLogLevel(level);
|
||||
|
||||
launcher.launch(fn);
|
||||
|
||||
List<String> expected = new ArrayList<>(List.of("AWS_LAMBDA_LOG_FORMAT=" + logFormat));
|
||||
if (runtimeLevel != null) {
|
||||
expected.add("AWS_LAMBDA_LOG_LEVEL=" + runtimeLevel);
|
||||
}
|
||||
assertEquals(expected,
|
||||
captureRealContainerSpec().env().stream()
|
||||
.filter(value -> value.startsWith("AWS_LAMBDA_LOG_FORMAT=")
|
||||
|| value.startsWith("AWS_LAMBDA_LOG_LEVEL="))
|
||||
.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchFunction_hotReloadMountsTheHostDirectoryReadOnlyAtVarTask() {
|
||||
LambdaFunction fn = new LambdaFunction();
|
||||
|
||||
+17
@@ -247,6 +247,23 @@ class KubernetesPodLauncherTest {
|
||||
verify(runtimeApiServer).setFunctionMetadata("my-fn", version, "index.handler", "000000000000");
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchPassesTheLoggingConfigToTheRuntime() {
|
||||
markPodPhaseInBackground("floci-lambda-my-fn-", "Running");
|
||||
LambdaFunction fn = function();
|
||||
fn.setLogFormat("JSON");
|
||||
fn.setApplicationLogLevel("DEBUG");
|
||||
|
||||
ContainerHandle handle = launcher.launch(fn);
|
||||
|
||||
Pod pod = client.pods().inNamespace("default").withName(handle.getContainerId()).get();
|
||||
assertThat(pod.getSpec().getContainers().getFirst().getEnv())
|
||||
.filteredOn(variable -> "AWS_LAMBDA_LOG_FORMAT".equals(variable.getName())
|
||||
|| "AWS_LAMBDA_LOG_LEVEL".equals(variable.getName()))
|
||||
.extracting(EnvVar::getValue)
|
||||
.containsExactly("JSON", "DEBUG");
|
||||
}
|
||||
|
||||
@Test
|
||||
void launchPassesTheFunctionsOwningAccountIntoTheBaselineAwsEnv() {
|
||||
// A pod-launched Lambda is subject to the same placeholder-credential bug as a
|
||||
|
||||
Reference in new issue
Block a user