fix(cloudwatch): return storedBytes on DescribeLogGroups (#4202)

* fix(cloudwatch): return storedBytes on DescribeLogGroups

AWS reports log-group storedBytes as the sum of stream sizes. Floci
always returned 0, which breaks SDK clients that read the field.

* style(cloudwatch): import collection test helpers
This commit is contained in:
c3m2r4 authored and GitHub committed 2026-09-24 07:30:14 -04:00
1 parent 35605937c8
commit babb43ac3d
5 files changed
+119 -29

No files matched your search

@@ -3,6 +3,7 @@ package com.floci.test;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
import software.amazon.awssdk.services.cloudwatchlogs.model.LogGroup;
import static org.assertj.core.api.Assertions.assertThat;
@@ -18,13 +19,7 @@ class CloudWatchLogsDescribeLogGroupsTest {
try {
logs.createLogGroup(request -> request.logGroupName(groupName));
LogGroup group = logs.describeLogGroups(request -> request
.logGroupNamePrefix(groupName))
.logGroups()
.stream()
.filter(candidate -> groupName.equals(candidate.logGroupName()))
.findFirst()
.orElseThrow();
LogGroup group = findGroup(logs, groupName);
assertThat(group.arn())
.isEqualTo("arn:aws:logs:us-east-1:000000000000:log-group:" + groupName + ":*");
@@ -37,6 +32,46 @@ class CloudWatchLogsDescribeLogGroupsTest {
}
}
@Test
@DisplayName("returns storedBytes for the log group after events are ingested")
void returnsStoredBytesAfterIngest() {
String groupName = "/test/" + TestFixtures.uniqueName("logs-describe-bytes");
String streamName = "stream-1";
try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
try {
logs.createLogGroup(request -> request.logGroupName(groupName));
logs.createLogStream(request -> request
.logGroupName(groupName)
.logStreamName(streamName));
LogGroup empty = findGroup(logs, groupName);
assertThat(empty.storedBytes()).isZero();
logs.putLogEvents(request -> request
.logGroupName(groupName)
.logStreamName(streamName)
.logEvents(InputLogEvent.builder()
.timestamp(System.currentTimeMillis())
.message("hello")
.build()));
LogGroup afterIngest = findGroup(logs, groupName);
assertThat(afterIngest.storedBytes()).isPositive();
} finally {
deleteIfPresent(logs, groupName);
}
}
}
private static LogGroup findGroup(CloudWatchLogsClient logs, String groupName) {
return logs.describeLogGroups(request -> request.logGroupNamePrefix(groupName))
.logGroups()
.stream()
.filter(candidate -> groupName.equals(candidate.logGroupName()))
.findFirst()
.orElseThrow();
}
private static void deleteIfPresent(CloudWatchLogsClient logs, String groupName) {
logs.describeLogGroups(request -> request.logGroupNamePrefix(groupName))
.logGroups()
@@ -136,7 +136,7 @@ public class CloudWatchLogsHandler {
if (g.getKmsKeyId() != null) {
node.put("kmsKeyId", g.getKmsKeyId());
}
node.put("storedBytes", 0);
node.put("storedBytes", logsService.getStoredBytesForLogGroup(g.getLogGroupName(), region));
node.put("metricFilterCount", metricFilterHandler.metricFilterCount(g.getLogGroupName(), region));
groupsArray.add(node);
}
@@ -337,6 +337,13 @@ public class CloudWatchLogsService implements ResourceProvider {
return result;
}
public long getStoredBytesForLogGroup(String groupName, String region) {
String storagePrefix = streamKeyPrefix(region, groupName);
return streamStore.scan(k -> k.startsWith(storagePrefix)).stream()
.mapToLong(LogStream::getStoredBytes)
.sum();
}
public void putLogGroupDeletionProtection(String groupName, boolean enabled, String region) {
String key = groupKey(region, groupName);
LogGroup group = groupStore.get(key)
@@ -7,11 +7,15 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogStream;
import io.github.hectorvent.floci.services.cloudwatch.metrics.CloudWatchMetricsService;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
@@ -88,9 +92,9 @@ class CloudWatchLogsHandlerTest {
void describeLogStreamsHonorsOrderByDescendingLimitAndReturnsNextToken() {
service.createLogStream(GROUP, "another-stream", REGION);
service.putLogEvents(GROUP, STREAM,
java.util.List.of(java.util.Map.of("timestamp", 2000L, "message", "new")), REGION);
List.of(Map.of("timestamp", 2000L, "message", "new")), REGION);
service.putLogEvents(GROUP, "another-stream",
java.util.List.of(java.util.Map.of("timestamp", 1000L, "message", "old")), REGION);
List.of(Map.of("timestamp", 1000L, "message", "old")), REGION);
ObjectNode request = MAPPER.createObjectNode()
.put("logGroupName", GROUP)
.put("orderBy", "LastEventTime")
@@ -126,6 +130,26 @@ class CloudWatchLogsHandlerTest {
assertFalse(group.has("createdTime"));
}
@Test
void describeLogGroupsReturnsStoredBytesSummedAcrossStreams() {
service.createLogStream(GROUP, "second-stream", REGION);
service.putLogEvents(GROUP, STREAM,
List.of(Map.of("timestamp", 1L, "message", "alpha")), REGION);
service.putLogEvents(GROUP, "second-stream",
List.of(Map.of("timestamp", 2L, "message", "beta")), REGION);
long expected = service.describeLogStreams(GROUP, null, REGION).stream()
.mapToLong(LogStream::getStoredBytes)
.sum();
JsonNode group = ((JsonNode) handler.handle("DescribeLogGroups",
MAPPER.createObjectNode().put("logGroupNamePrefix", GROUP), REGION).getEntity())
.path("logGroups").get(0);
assertTrue(expected > 0);
assertEquals(expected, group.path("storedBytes").asLong());
}
@Test
void describeLogStreamsReturnsCreationTimeNotCreatedTime() {
ObjectNode request = MAPPER.createObjectNode().put("logGroupName", GROUP);
@@ -355,7 +379,7 @@ class CloudWatchLogsHandlerTest {
void getLogEventsByLogGroupIdentifierArnAndLogStreamArn() {
long now = System.currentTimeMillis();
service.putLogEvents(GROUP, STREAM,
java.util.List.of(java.util.Map.of("timestamp", now, "message", "via arn")),
List.of(Map.of("timestamp", now, "message", "via arn")),
REGION);
ObjectNode request = MAPPER.createObjectNode();
@@ -375,9 +399,9 @@ class CloudWatchLogsHandlerTest {
@Test
void filterLogEventsByLogGroupIdentifierArnAndStreamArns() {
long now = System.currentTimeMillis();
service.putLogEvents(GROUP, STREAM, java.util.List.of(
java.util.Map.of("timestamp", now, "message", "ERROR: kaboom"),
java.util.Map.of("timestamp", now + 1, "message", "INFO: fine")
service.putLogEvents(GROUP, STREAM, List.of(
Map.of("timestamp", now, "message", "ERROR: kaboom"),
Map.of("timestamp", now + 1, "message", "INFO: fine")
), REGION);
ObjectNode request = MAPPER.createObjectNode();
@@ -403,11 +427,11 @@ class CloudWatchLogsHandlerTest {
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")
service.putLogEvents(GROUP, STREAM, List.of(
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")
service.putLogEvents(GROUP, otherStream, List.of(
Map.of("timestamp", now + 1, "message", "ERROR: from second")
), REGION);
ObjectNode request = MAPPER.createObjectNode();
@@ -428,8 +452,8 @@ class CloudWatchLogsHandlerTest {
// 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")
service.putLogEvents(GROUP, STREAM, List.of(
Map.of("timestamp", now, "message", "only one stream here")
), REGION);
ObjectNode request = MAPPER.createObjectNode();
@@ -447,9 +471,9 @@ class CloudWatchLogsHandlerTest {
@Test
void filterLogEventsByLogGroupIdentifierArnWithoutStreamFilter() {
long now = System.currentTimeMillis();
service.putLogEvents(GROUP, STREAM, java.util.List.of(
java.util.Map.of("timestamp", now, "message", "a"),
java.util.Map.of("timestamp", now + 1, "message", "b")
service.putLogEvents(GROUP, STREAM, List.of(
Map.of("timestamp", now, "message", "a"),
Map.of("timestamp", now + 1, "message", "b")
), REGION);
ObjectNode request = MAPPER.createObjectNode();
@@ -467,12 +491,12 @@ class CloudWatchLogsHandlerTest {
// response, goes out on the next request, and the second page carries the matches the
// first one capped off.
long now = System.currentTimeMillis();
service.putLogEvents(GROUP, STREAM, java.util.List.of(
java.util.Map.of("timestamp", now, "message", "msg-0"),
java.util.Map.of("timestamp", now + 1, "message", "msg-1"),
java.util.Map.of("timestamp", now + 2, "message", "msg-2"),
java.util.Map.of("timestamp", now + 3, "message", "msg-3"),
java.util.Map.of("timestamp", now + 4, "message", "msg-4")
service.putLogEvents(GROUP, STREAM, List.of(
Map.of("timestamp", now, "message", "msg-0"),
Map.of("timestamp", now + 1, "message", "msg-1"),
Map.of("timestamp", now + 2, "message", "msg-2"),
Map.of("timestamp", now + 3, "message", "msg-3"),
Map.of("timestamp", now + 4, "message", "msg-4")
), REGION);
ObjectNode firstRequest = MAPPER.createObjectNode();
@@ -802,6 +802,30 @@ class CloudWatchLogsServiceTest {
assertEquals(2, result.events().size());
}
@Test
void getStoredBytesForLogGroupSumsStreamBytesAndIgnoresOtherGroups() {
service.createLogGroup("/app/logs", null, null, REGION);
service.createLogStream("/app/logs", "stream-1", REGION);
service.createLogStream("/app/logs", "stream-2", REGION);
service.createLogGroup("/app/other", null, null, REGION);
service.createLogStream("/app/other", "stream-1", REGION);
long now = System.currentTimeMillis();
service.putLogEvents("/app/logs", "stream-1",
List.of(Map.of("timestamp", now, "message", "one")), REGION);
service.putLogEvents("/app/logs", "stream-2",
List.of(Map.of("timestamp", now + 1, "message", "two")), REGION);
service.putLogEvents("/app/other", "stream-1",
List.of(Map.of("timestamp", now + 2, "message", "other")), REGION);
long expected = service.describeLogStreams("/app/logs", null, REGION).stream()
.mapToLong(LogStream::getStoredBytes)
.sum();
assertTrue(expected > 0);
assertEquals(expected, service.getStoredBytesForLogGroup("/app/logs", REGION));
assertEquals(0, service.getStoredBytesForLogGroup("/app/empty", REGION));
}
@Test
void putLogEventsUpdatesStreamMetadata() {
service.createLogGroup("/app/logs", null, null, REGION);