mirror of
https://github.com/floci-io/floci.git
synced 2026-12-02 09:47:56 -05:00
fix(lambda): send a function's logs to the log group its LoggingConfig names (#5895)
* fix(lambda): send a function's logs to the log group its LoggingConfig names Floci stored LoggingConfig.LogGroup and read it back, but every execution environment still wrote to /aws/lambda/<name> and told the function so in AWS_LAMBDA_LOG_GROUP_NAME. Measured on AWS (eu-west-1, 2026-10-16): a function whose LoggingConfig names a group writes INIT_START, START, its own output, END and REPORT there, Lambda creates the group when it is missing, and /aws/lambda/<name> is never created. AWS_LAMBDA_LOG_GROUP_NAME and AWS_LAMBDA_LOG_STREAM_NAME name that group and stream. Once LoggingConfig names a group, which several functions can share, each stream's name carries the function's name: 2026/10/16/my-fn[$LATEST]<id> instead of 2026/10/16/[$LATEST]<id>. It does even when the group named is /aws/lambda/<name> itself, so the rule is whether a group is named, not whether it differs from the default. Both launchers, Docker and Kubernetes, and GetFunctionConfiguration's LoggingConfig now take the group and the stream from one place, LambdaLogDestinations. Floci skips delivering a log group to a Lambda subscription destination that writes its own logs there, which would feed the function its own output forever. That check assumed the group was /aws/lambda/<name>, so a forwarder subscribed to the group its LoggingConfig names would loop. It now asks LambdaLogDestinations for the function's group, and falls back to the default name, from the same place, for a function it cannot find. * fix(logs): skip only a function's own streams when it forwards its log group The guard that keeps a subscription from feeding a Lambda destination its own output looked the function up by name in the caller's account and Region at $LATEST, and skipped the whole group once it matched. In a group LoggingConfig names, which several functions can share, a forwarder then lost every other function's logs; a destination naming a published version or alias was checked against $LATEST's group; and a function in another account than the ambient one was not found, so its own custom group was fed back to it. The function is now the one the destination ARN names, through getFunctionByArn, with its account, Region and qualifier, and in a named group only the streams carrying its name are skipped, the shape measured on AWS for a named group (2026/10/16/<name>[$LATEST]<id>); every stream of the default group is still the function's own. A destination ARN that does not parse as a function's is not a loop and is no longer caught as any RuntimeException. Tests: another function's stream in a shared group reaches the forwarder while its own is skipped, and a destination naming a published version is checked against that version's group (both fail on the previous head); the stream ownership rule in LambdaLogDestinationsTest.
This commit is contained in:
1 parent
9dcb1b65ae
commit
58097a832a
11 files changed
+271
-21
No files matched your search
+33
-10
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudwatch.logs;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
@@ -13,8 +14,10 @@ import io.github.hectorvent.floci.services.firehose.FirehoseService;
|
||||
import io.github.hectorvent.floci.services.firehose.model.Record;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaArnUtils;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLogDestinations;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import io.github.hectorvent.floci.services.lambda.model.InvocationType;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
@@ -46,7 +49,6 @@ public class CloudWatchLogsSubscriptionDeliveryService {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(CloudWatchLogsSubscriptionDeliveryService.class);
|
||||
private static final String DATA_MESSAGE = "DATA_MESSAGE";
|
||||
private static final String LAMBDA_LOG_PREFIX = "/aws/lambda/";
|
||||
/** Max compressed record size for Kinesis and Firehose delivery (1 MB). */
|
||||
private static final int MAX_RECORD_PAYLOAD_BYTES = 1_000_000;
|
||||
|
||||
@@ -163,9 +165,9 @@ public class CloudWatchLogsSubscriptionDeliveryService {
|
||||
String service = arn.service();
|
||||
|
||||
if ("lambda".equals(service)) {
|
||||
if (isSelfSubscribedLambda(event.logGroupName(), destinationArn)) {
|
||||
LOG.warnv("Skipping delivery to Lambda destination {0} to prevent recursive loop on its own log group {1}",
|
||||
destinationArn, event.logGroupName());
|
||||
if (isOwnOutput(event, destinationArn)) {
|
||||
LOG.warnv("Skipping delivery to Lambda destination {0} to prevent recursive loop on its own log stream {1} in {2}",
|
||||
destinationArn, event.logStreamName(), event.logGroupName());
|
||||
return;
|
||||
}
|
||||
deliverChunks(filter, event, account, matching, gzipped -> deliverToLambda(destinationArn, gzipped));
|
||||
@@ -180,17 +182,38 @@ public class CloudWatchLogsSubscriptionDeliveryService {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isSelfSubscribedLambda(String logGroupName, String functionArn) {
|
||||
if (logGroupName == null || functionArn == null || !logGroupName.startsWith(LAMBDA_LOG_PREFIX)) {
|
||||
/**
|
||||
* Whether the event is the destination function's own output, which delivering would feed back
|
||||
* to it forever. Floci skips only that: the function is the one the destination ARN names, with
|
||||
* its account, Region and version or alias, and its output is in the group its LoggingConfig
|
||||
* names, or else {@code /aws/lambda/<name>}. A named group can be shared, so there only the
|
||||
* function's own streams are skipped, and another function's logs in it still reach it.
|
||||
*/
|
||||
private boolean isOwnOutput(LogEventsIngested event, String functionArn) {
|
||||
String logGroupName = event.logGroupName();
|
||||
if (logGroupName == null) {
|
||||
return false;
|
||||
}
|
||||
String logFunctionName = logGroupName.substring(LAMBDA_LOG_PREFIX.length());
|
||||
String functionName;
|
||||
try {
|
||||
String targetFunctionName = LambdaArnUtils.resolve(functionArn).name();
|
||||
return logFunctionName.equals(targetFunctionName);
|
||||
} catch (Exception e) {
|
||||
functionName = LambdaArnUtils.resolve(functionArn).name();
|
||||
} catch (AwsException e) {
|
||||
// Not a function ARN: invoking it fails as well, so there is no loop to break.
|
||||
return false;
|
||||
}
|
||||
LambdaFunction target = null;
|
||||
if (lambdaService != null) {
|
||||
try {
|
||||
target = lambdaService.getFunctionByArn(functionArn);
|
||||
} catch (AwsException e) {
|
||||
LOG.debugv("Destination function {0} not found: {1}", functionArn, e.getMessage());
|
||||
}
|
||||
}
|
||||
if (target == null) {
|
||||
return logGroupName.equals(LambdaLogDestinations.defaultLogGroup(functionName));
|
||||
}
|
||||
return logGroupName.equals(LambdaLogDestinations.logGroup(target))
|
||||
&& LambdaLogDestinations.isOwnStream(target, event.logStreamName());
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
|
||||
@@ -862,9 +862,7 @@ public class LambdaController {
|
||||
logging.put("SystemLogLevel",
|
||||
fn.getSystemLogLevel() != null ? fn.getSystemLogLevel() : "INFO");
|
||||
}
|
||||
logging.put("LogGroup", fn.getLogGroup() != null && !fn.getLogGroup().isBlank()
|
||||
? fn.getLogGroup()
|
||||
: "/aws/lambda/" + fn.getFunctionName());
|
||||
logging.put("LogGroup", LambdaLogDestinations.logGroup(fn));
|
||||
}
|
||||
|
||||
/** Managed-runtime only: AWS omits it for container images, which pin their own runtime. */
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
|
||||
/**
|
||||
* Where a function's execution environments write their logs: the log group its LoggingConfig
|
||||
* names, else {@code /aws/lambda/<name>}. Once LoggingConfig names a group, which several
|
||||
* functions can share, AWS puts the function's name in each stream's name, even when the group
|
||||
* named is the default one: {@code 2026/10/16/my-fn[$LATEST]<id>} rather than
|
||||
* {@code 2026/10/16/[$LATEST]<id>}.
|
||||
*/
|
||||
public final class LambdaLogDestinations {
|
||||
|
||||
private LambdaLogDestinations() {
|
||||
}
|
||||
|
||||
public static String logGroup(LambdaFunction fn) {
|
||||
return namesLogGroup(fn) ? fn.getLogGroup() : defaultLogGroup(fn.getFunctionName());
|
||||
}
|
||||
|
||||
/** The group of a function whose LoggingConfig names none: {@code /aws/lambda/<name>}. */
|
||||
public static String defaultLogGroup(String functionName) {
|
||||
return "/aws/lambda/" + functionName;
|
||||
}
|
||||
|
||||
/** The stream's name after its {@code yyyy/MM/dd/} date, for the environment {@code instanceId}. */
|
||||
public static String logStreamSuffix(LambdaFunction fn, String instanceId) {
|
||||
String version = "[$LATEST]" + instanceId;
|
||||
return namesLogGroup(fn) ? fn.getFunctionName() + version : version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code logStreamName} in the function's log group is the function's own: every stream
|
||||
* of the default group is, and in a group LoggingConfig names, which several functions can
|
||||
* share, the ones that carry its name.
|
||||
*/
|
||||
public static boolean isOwnStream(LambdaFunction fn, String logStreamName) {
|
||||
if (!namesLogGroup(fn)) {
|
||||
return true;
|
||||
}
|
||||
return logStreamName != null && logStreamName.contains("/" + fn.getFunctionName() + "[");
|
||||
}
|
||||
|
||||
private static boolean namesLogGroup(LambdaFunction fn) {
|
||||
return fn.getLogGroup() != null && !fn.getLogGroup().isBlank();
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.core.common.docker.RetryingTarCopier;
|
||||
import io.github.hectorvent.floci.services.ecr.registry.EcrRegistryManager;
|
||||
import io.github.hectorvent.floci.services.iam.model.SessionCreds;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLayerService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLogDestinations;
|
||||
import io.github.hectorvent.floci.services.lambda.model.ContainerState;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaLayerVersion;
|
||||
@@ -213,8 +214,9 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
|
||||
resolveContainerNamePrefix(config), fn.getFunctionName() + "-" + shortId);
|
||||
|
||||
// CloudWatch log coordinates — computed here so they can be injected as env vars
|
||||
String cwLogGroup = "/aws/lambda/" + fn.getFunctionName();
|
||||
String cwLogStream = LOG_STREAM_DATE_FMT.format(LocalDate.now()) + "/[$LATEST]" + shortId;
|
||||
String cwLogGroup = LambdaLogDestinations.logGroup(fn);
|
||||
String cwLogStream = LOG_STREAM_DATE_FMT.format(LocalDate.now()) + "/"
|
||||
+ LambdaLogDestinations.logStreamSuffix(fn, shortId);
|
||||
String lambdaRegion = extractRegionFromArn(fn.getFunctionArn(), config.defaultRegion());
|
||||
String lambdaAccountId = AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId());
|
||||
LambdaDockerFlags dockerFlags = configuredDockerFlags();
|
||||
|
||||
+3
-2
@@ -9,6 +9,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.docker.LaunchedContainerAwsEnv;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLayerService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLogDestinations;
|
||||
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;
|
||||
@@ -175,8 +176,8 @@ public class KubernetesPodLauncher implements LambdaRuntimeLauncher {
|
||||
podName = LambdaPodSpecFactory.podName(fn.getFunctionName(), shortId);
|
||||
String region = AwsArnUtils.regionOrDefault(fn.getFunctionArn(), config.defaultRegion());
|
||||
String accountId = AwsArnUtils.accountOrDefault(fn.getFunctionArn(), config.defaultAccountId());
|
||||
String cwLogGroup = "/aws/lambda/" + fn.getFunctionName();
|
||||
String cwLogStream = logStreamer.logStreamName(shortId);
|
||||
String cwLogGroup = LambdaLogDestinations.logGroup(fn);
|
||||
String cwLogStream = logStreamer.logStreamName(fn, shortId);
|
||||
|
||||
String codeDownloadUrl = imagePackage ? null : codeDownloadUrl(fn, region);
|
||||
List<String> layerUrls = imagePackage ? List.of() : layerDownloadUrls(fn, region);
|
||||
|
||||
+4
-2
@@ -1,6 +1,8 @@
|
||||
package io.github.hectorvent.floci.services.lambda.launcher.kubernetes;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLogDestinations;
|
||||
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.jboss.logging.Logger;
|
||||
@@ -33,8 +35,8 @@ public class KubernetesPodLogStreamer {
|
||||
}
|
||||
|
||||
/** CloudWatch log stream name for one execution environment, same shape as the docker path. */
|
||||
public String logStreamName(String shortId) {
|
||||
return cloudWatchWriter.generateLogStreamName("[$LATEST]" + shortId);
|
||||
public String logStreamName(LambdaFunction fn, String shortId) {
|
||||
return cloudWatchWriter.generateLogStreamName(LambdaLogDestinations.logStreamSuffix(fn, shortId));
|
||||
}
|
||||
|
||||
public Closeable attach(String namespace, String podName, String logGroup,
|
||||
|
||||
Reference in new issue
Block a user