fix(cloudwatch): return logStreamName on FilterLogEvents results (#2259)

FilterLogEvents is the cross-stream search API, so a match is only
attributable if the response names the stream that emitted it. Floci
served FilterLogEvents and GetLogEvents from one serializer emitting
eventId, timestamp, message and ingestionTime, so a caller searching a
whole log group could tell which group a hit came from but not which
stream, and tooling written against real AWS lost that attribution
without any error.

Derive the stream from the event's storage key rather than persisting it
on the event. Keys are already
<region>::<group>::<stream>::<timestamp>::<uuid>, so the key is an
authoritative record of the emitter for every event in the store,
including ones written before this change. Storing the name on LogEvent
instead would leave every already-persisted event with a null
logStreamName and need a backfill pass to repair it. The stream is read
back between the group prefix and the trailing timestamp and uuid, both
generated here, so a stream name is recovered whole even though nothing
stops a caller from creating one that holds a ':'.

filterLogEvents walks the keyset once and pairs each event with its
stream, which also collapses the stream-restricted branch: it previously
ran one full keyset scan per requested stream, since a scan walks every
key regardless of the prefix it filters on.

GetLogEvents keeps its existing shape. Real AWS returns OutputLogEvent
there, which carries no logStreamName, because the caller named the
stream in the request.

Closes #2210
This commit is contained in:
TiM authored and GitHub committed 2026-08-13 18:01:34 -04:00
1 parent a49c3e57f6
commit a0adea9073
4 files changed
+201 -23

No files matched your search

@@ -305,6 +305,56 @@ class CloudWatchLogsHandlerTest {
JsonNode events = ((ObjectNode) response.getEntity()).path("events");
assertEquals(1, events.size());
assertThat(events.get(0).path("message").asText(), containsString("ERROR"));
assertEquals(STREAM, events.get(0).path("logStreamName").asText());
}
@Test
void filterLogEventsAttributesEachMatchToItsStream() {
// FilterLogEvents spans streams, so a caller can only attribute a hit if the response
// says which stream emitted it.
String otherStream = "postgresql.log.2026-06-05";
service.createLogStream(GROUP, otherStream, REGION);
long now = System.currentTimeMillis();
service.putLogEvents(GROUP, STREAM, java.util.List.of(
java.util.Map.of("timestamp", now, "message", "ERROR: from first")
), REGION);
service.putLogEvents(GROUP, otherStream, java.util.List.of(
java.util.Map.of("timestamp", now + 1, "message", "ERROR: from second")
), REGION);
ObjectNode request = MAPPER.createObjectNode();
request.put("logGroupName", GROUP);
request.put("filterPattern", "ERROR");
Response response = handler.handle("FilterLogEvents", request, REGION);
assertEquals(200, response.getStatus());
JsonNode events = ((ObjectNode) response.getEntity()).path("events");
assertEquals(2, events.size());
assertEquals(STREAM, events.get(0).path("logStreamName").asText());
assertEquals(otherStream, events.get(1).path("logStreamName").asText());
}
@Test
void getLogEventsOmitsLogStreamName() {
// OutputLogEvent has no logStreamName in real AWS: the caller named the stream in the
// request, so echoing it back would be a shape floci invents.
long now = System.currentTimeMillis();
service.putLogEvents(GROUP, STREAM, java.util.List.of(
java.util.Map.of("timestamp", now, "message", "only one stream here")
), REGION);
ObjectNode request = MAPPER.createObjectNode();
request.put("logGroupName", GROUP);
request.put("logStreamName", STREAM);
Response response = handler.handle("GetLogEvents", request, REGION);
assertEquals(200, response.getStatus());
JsonNode events = ((ObjectNode) response.getEntity()).path("events");
assertEquals(1, events.size());
assertTrue(events.get(0).path("logStreamName").isMissingNode());
}
@Test
@@ -249,7 +249,7 @@ class CloudWatchLogsServiceTest {
assertEquals(10, result.events().size());
for (int i = 0; i < 10; i++) {
assertEquals("SEQLINE-" + i, result.events().get(i).getMessage(),
assertEquals("SEQLINE-" + i, result.events().get(i).event().getMessage(),
"event at index " + i + " out of ingestion order");
}
}
@@ -286,7 +286,87 @@ class CloudWatchLogsServiceTest {
CloudWatchLogsService.FilteredLogEventsResult result = service.filterLogEvents(
"/app/logs", null, null, null, "ERROR", 100, REGION);
assertEquals(2, result.events().size());
assertTrue(result.events().stream().allMatch(e -> e.getMessage().contains("ERROR")));
assertTrue(result.events().stream().allMatch(f -> f.event().getMessage().contains("ERROR")));
}
@Test
void filterLogEventsCarriesEmittingStreamPerEvent() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
service.createLogStream("/app/logs", "stream-2", REGION);
long now = System.currentTimeMillis();
service.putLogEvents("/app/logs", "stream-1",
List.of(Map.of("timestamp", now, "message", "ERROR: from one")), REGION);
service.putLogEvents("/app/logs", "stream-2",
List.of(Map.of("timestamp", now + 1, "message", "ERROR: from two")), REGION);
CloudWatchLogsService.FilteredLogEventsResult result = service.filterLogEvents(
"/app/logs", null, null, null, "ERROR", 100, REGION);
assertEquals(2, result.events().size());
assertEquals("stream-1", result.events().get(0).logStreamName());
assertEquals("stream-2", result.events().get(1).logStreamName());
}
@Test
void filterLogEventsRestrictedToNamedStreamsSkipsTheRest() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
service.createLogStream("/app/logs", "stream-2", REGION);
long now = System.currentTimeMillis();
service.putLogEvents("/app/logs", "stream-1",
List.of(Map.of("timestamp", now, "message", "keep me")), REGION);
service.putLogEvents("/app/logs", "stream-2",
List.of(Map.of("timestamp", now + 1, "message", "drop me")), REGION);
CloudWatchLogsService.FilteredLogEventsResult result = service.filterLogEvents(
"/app/logs", List.of("stream-2"), null, null, null, 100, REGION);
assertEquals(1, result.events().size());
assertEquals("stream-2", result.events().getFirst().logStreamName());
assertEquals("drop me", result.events().getFirst().event().getMessage());
}
@Test
void filterLogEventsDoesNotLeakEventsFromAPrefixSiblingGroup() {
// "/app/logs" must not sweep up "/app/logs-archive": the key layout separates the group
// from the stream with "::", so the group prefix has to be matched with it.
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
service.createLogGroup("/app/logs-archive", null, null, REGION);
service.createLogStream("/app/logs-archive", "stream-1", REGION);
long now = System.currentTimeMillis();
service.putLogEvents("/app/logs", "stream-1",
List.of(Map.of("timestamp", now, "message", "live")), REGION);
service.putLogEvents("/app/logs-archive", "stream-1",
List.of(Map.of("timestamp", now, "message", "archived")), REGION);
CloudWatchLogsService.FilteredLogEventsResult result = service.filterLogEvents(
"/app/logs", null, null, null, null, 100, REGION);
assertEquals(1, result.events().size());
assertEquals("live", result.events().getFirst().event().getMessage());
}
@Test
void filterLogEventsRecoversStreamNamesHoldingAColon() {
// AWS forbids ':' in a stream name but floci does not enforce that, so the stream a match
// is attributed to must not depend on the name being AWS-legal.
String awkward = "app:worker:1";
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", awkward, REGION);
service.putLogEvents("/app/logs", awkward,
List.of(Map.of("timestamp", System.currentTimeMillis(), "message", "hello")), REGION);
CloudWatchLogsService.FilteredLogEventsResult result = service.filterLogEvents(
"/app/logs", null, null, null, null, 100, REGION);
assertEquals(1, result.events().size());
assertEquals(awkward, result.events().getFirst().logStreamName());
}
@Test