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:
Alessandro Lattao authored and GitHub committed 2026-10-17 17:54:30 -04:00
1 parent 9dcb1b65ae
commit 58097a832a
11 files changed
+271 -21

No files matched your search

@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.firehose.model.Record;
import io.github.hectorvent.floci.services.kinesis.KinesisService;
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 jakarta.enterprise.event.Event;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -31,13 +32,16 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class CloudWatchLogsSubscriptionDeliveryServiceTest {
@@ -375,7 +379,82 @@ class CloudWatchLogsSubscriptionDeliveryServiceTest {
), REGION);
assertNotNull(nextToken);
verifyNoInteractions(lambdaService);
verify(lambdaService, never()).invokeArn(anyString(), any(), any());
}
/**
* A function whose LoggingConfig names a group writes there, so a subscription that sends that
* group to the function itself is skipped too, and delivered again once the function writes to
* its default group instead.
*/
@Test
void aFunctionSubscribedToTheCustomGroupItWritesToIsSkipped() {
String shared = "/shared/app";
String forwarderArn = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":function:forwarder";
LambdaFunction forwarder = new LambdaFunction();
forwarder.setFunctionName("forwarder");
forwarder.setLogGroup(shared);
when(lambdaService.getFunctionByArn(forwarderArn)).thenReturn(forwarder);
logsService.createLogGroup(shared, null, null, REGION);
logsService.createLogStream(shared, "2026/10/16/forwarder[$LATEST]abc", REGION);
logsService.putSubscriptionFilter(shared, "loop-filter", "", forwarderArn, null, REGION);
logsService.putLogEvents(shared, "2026/10/16/forwarder[$LATEST]abc", List.of(
Map.of("timestamp", 5000L, "message", "forwarder log line")), REGION);
verify(lambdaService, never()).invokeArn(anyString(), any(), any());
forwarder.setLogGroup(null);
logsService.putLogEvents(shared, "2026/10/16/forwarder[$LATEST]abc", List.of(
Map.of("timestamp", 5001L, "message", "another function's log line")), REGION);
verify(lambdaService).invokeArn(eq(forwarderArn), any(), eq(InvocationType.Event));
}
/**
* A group LoggingConfig names can be shared: the forwarder subscribed to it gets the other
* functions' streams, and only its own, the ones carrying its name, are skipped.
*/
@Test
void anotherFunctionsLogsInASharedGroupStillReachTheForwarder() {
String shared = "/shared/app";
String forwarderArn = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":function:forwarder";
LambdaFunction forwarder = new LambdaFunction();
forwarder.setFunctionName("forwarder");
forwarder.setLogGroup(shared);
when(lambdaService.getFunctionByArn(forwarderArn)).thenReturn(forwarder);
logsService.createLogGroup(shared, null, null, REGION);
logsService.createLogStream(shared, "2026/10/17/forwarder[$LATEST]abc", REGION);
logsService.createLogStream(shared, "2026/10/17/billing[$LATEST]def", REGION);
logsService.putSubscriptionFilter(shared, "forward-filter", "", forwarderArn, null, REGION);
logsService.putLogEvents(shared, "2026/10/17/forwarder[$LATEST]abc", List.of(
Map.of("timestamp", 5000L, "message", "forwarder log line")), REGION);
verify(lambdaService, never()).invokeArn(anyString(), any(), any());
logsService.putLogEvents(shared, "2026/10/17/billing[$LATEST]def", List.of(
Map.of("timestamp", 5001L, "message", "billing log line")), REGION);
verify(lambdaService).invokeArn(eq(forwarderArn), any(), eq(InvocationType.Event));
}
/**
* A destination naming a published version is checked against that version's LoggingConfig,
* looked up by the ARN with its account and qualifier, not against {@code $LATEST}'s.
*/
@Test
void aPublishedVersionIsCheckedAgainstItsOwnLogGroup() {
String versionGroup = "/forwarder/v1";
String versionArn = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":function:forwarder:1";
LambdaFunction version = new LambdaFunction();
version.setFunctionName("forwarder");
version.setLogGroup(versionGroup);
when(lambdaService.getFunctionByArn(versionArn)).thenReturn(version);
logsService.createLogGroup(versionGroup, null, null, REGION);
logsService.createLogStream(versionGroup, "2026/10/17/forwarder[$LATEST]abc", REGION);
logsService.putSubscriptionFilter(versionGroup, "loop-filter", "", versionArn, null, REGION);
logsService.putLogEvents(versionGroup, "2026/10/17/forwarder[$LATEST]abc", List.of(
Map.of("timestamp", 5000L, "message", "version log line")), REGION);
verify(lambdaService, never()).invokeArn(anyString(), any(), any());
}
@Test
@@ -0,0 +1,64 @@
package io.github.hectorvent.floci.services.lambda;
import io.github.hectorvent.floci.services.lambda.model.LambdaFunction;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Measured on AWS (eu-west-1, 2026-10-16): with no LoggingConfig a stream is
* {@code 2026/10/16/[$LATEST]<id>} in {@code /aws/lambda/<name>}; once LoggingConfig names a log
* group, the default one's name included, the stream is {@code 2026/10/16/<name>[$LATEST]<id>}.
*/
class LambdaLogDestinationsTest {
@Test
void aFunctionWithoutLoggingConfigWritesToItsOwnGroup() {
LambdaFunction fn = function();
assertEquals("/aws/lambda/orders", LambdaLogDestinations.logGroup(fn));
assertEquals("[$LATEST]1a2b3c4d", LambdaLogDestinations.logStreamSuffix(fn, "1a2b3c4d"));
}
@Test
void aGroupLoggingConfigNamesTakesTheLogsAndTheFunctionNameInEachStream() {
LambdaFunction fn = function();
fn.setLogGroup("/shared/app-logs");
assertEquals("/shared/app-logs", LambdaLogDestinations.logGroup(fn));
assertEquals("orders[$LATEST]1a2b3c4d", LambdaLogDestinations.logStreamSuffix(fn, "1a2b3c4d"));
}
@Test
void inASharedGroupOnlyTheStreamsCarryingTheFunctionNameAreItsOwn() {
LambdaFunction fn = function();
fn.setLogGroup("/shared/app-logs");
assertTrue(LambdaLogDestinations.isOwnStream(fn, "2026/10/17/orders[$LATEST]1a2b3c4d"));
assertFalse(LambdaLogDestinations.isOwnStream(fn, "2026/10/17/orders-archive[$LATEST]1a2b3c4d"));
assertFalse(LambdaLogDestinations.isOwnStream(fn, "2026/10/17/billing[$LATEST]1a2b3c4d"));
assertFalse(LambdaLogDestinations.isOwnStream(fn, null));
}
@Test
void everyStreamOfTheDefaultGroupIsTheFunctionsOwn() {
assertTrue(LambdaLogDestinations.isOwnStream(function(), "2026/10/17/[$LATEST]1a2b3c4d"));
}
@Test
void namingTheDefaultGroupStillPutsTheFunctionNameInEachStream() {
LambdaFunction fn = function();
fn.setLogGroup("/aws/lambda/orders");
assertEquals("/aws/lambda/orders", LambdaLogDestinations.logGroup(fn));
assertEquals("orders[$LATEST]1a2b3c4d", LambdaLogDestinations.logStreamSuffix(fn, "1a2b3c4d"));
}
private static LambdaFunction function() {
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("orders");
return fn;
}
}
@@ -673,6 +673,34 @@ class ContainerLauncherTest {
"a partial user-supplied access key must never override the owner-account baseline");
}
@Test
void launchFunction_writesToTheLogGroupLoggingConfigNames() throws Exception {
Path codePath = Files.createDirectory(tempDir.resolve("custom-log-group"));
LambdaFunction fn = new LambdaFunction();
fn.setFunctionName("custom-logs-fn");
fn.setRuntime("nodejs20.x");
fn.setHandler("index.handler");
fn.setCodeLocalPath(codePath.toString());
fn.setFunctionArn("arn:aws:lambda:us-east-1:000000000000:function:custom-logs-fn");
fn.setLogGroup("/shared/app-logs");
launcher.launch(fn);
List<String> env = captureRealContainerSpec().env();
assertTrue(env.contains("AWS_LAMBDA_LOG_GROUP_NAME=/shared/app-logs"));
String stream = env.stream()
.filter(e -> e.startsWith("AWS_LAMBDA_LOG_STREAM_NAME="))
.findFirst().orElseThrow()
.substring("AWS_LAMBDA_LOG_STREAM_NAME=".length());
assertTrue(stream.matches("\\d{4}/\\d{2}/\\d{2}/custom-logs-fn\\[\\$LATEST][0-9a-f]{8}"), stream);
verify(logStreamer).ensureLogGroupAndStreamForAccount(
eq("000000000000"), eq("/shared/app-logs"), eq(stream), eq("us-east-1"));
verify(logStreamer).attachForAccount(
eq("000000000000"), eq("container-123"), eq("/shared/app-logs"), eq(stream),
eq("us-east-1"), eq("lambda:custom-logs-fn"));
}
@Test
void launchFunction_endpointUsesListenPortNotAdvertisedBaseUrlPort() throws Exception {
// docker run -p 4811:4566 -e FLOCI_BASE_URL=http://localhost:4811: 4811 exists only on the
@@ -127,7 +127,7 @@ class KubernetesPodLauncherTest {
logStreamer = mock(KubernetesPodLogStreamer.class);
lenient().when(logStreamer.attachForAccount(anyString(), anyString(), anyString(), anyString(),
anyString(), anyString(), anyString())).thenReturn(() -> { });
lenient().when(logStreamer.logStreamName(anyString())).thenReturn("2026/07/25/[$LATEST]test");
lenient().when(logStreamer.logStreamName(any(), anyString())).thenReturn("2026/07/25/[$LATEST]test");
s3Service = mock(S3Service.class);
presignGenerator = mock(PreSignedUrlGenerator.class);
lenient().when(presignGenerator.generatePresignedUrl(anyString(), anyString(), anyString(),