fix(logs): paginate log group descriptions (#6142)

* fix(logs): paginate log group descriptions

* fix(logs): keep group paging and validation in service

Preserve legacy ordered callers and share the existing bad-token error. Render out-of-range limits with SmithyValidationErrors.

For 1000 reverse-ordered groups, a 50-group page now reads names 2199 times instead of 4197 in each of three samples. Keep a deterministic budget regression for the redundant sort.
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-21 12:07:52 -04:00
1 parent 4fa11104b4
commit 804b1d9597
5 files changed
+273 -6

No files matched your search

@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsErrorResponse;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.services.cloudwatch.logs.model.AccountPolicy;
import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogDestination;
import io.github.hectorvent.floci.services.cloudwatch.logs.model.LogEvent;
@@ -127,11 +128,23 @@ public class CloudWatchLogsHandler {
private Response handleDescribeLogGroups(JsonNode request, String region) {
String prefix = request.path("logGroupNamePrefix").asText(null);
List<LogGroup> groups = logsService.describeLogGroups(prefix, region);
int limit = 50;
if (request.hasNonNull("limit")) {
JsonNode value = request.get("limit");
if (!value.isIntegralNumber() || !value.canConvertToInt()) {
throw new AwsException("InvalidParameterException", "limit must be an integer.", 400);
}
limit = value.intValue();
}
String nextToken = request.path("nextToken").asText(null);
if (request.hasNonNull("nextToken") && !request.get("nextToken").isTextual()) {
throw CloudWatchLogsService.invalidNextToken();
}
PaginatedResult<LogGroup> page = logsService.describeLogGroups(prefix, region, limit, nextToken);
ObjectNode response = objectMapper.createObjectNode();
ArrayNode groupsArray = objectMapper.createArrayNode();
for (LogGroup g : groups) {
for (LogGroup g : page.items()) {
ObjectNode node = objectMapper.createObjectNode();
node.put("logGroupName", g.getLogGroupName());
node.put("creationTime", g.getCreatedTime());
@@ -150,6 +163,9 @@ public class CloudWatchLogsHandler {
groupsArray.add(node);
}
response.set("logGroups", groupsArray);
if (page.nextToken() != null) {
response.put("nextToken", page.nextToken());
}
return Response.ok(response).build();
}
@@ -5,6 +5,8 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.Base64Url;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.Pagination;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.Resettable;
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
@@ -361,8 +363,28 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
}
public List<LogGroup> describeLogGroups(String prefix, String region) {
List<LogGroup> result = matchingLogGroups(prefix, region);
result.sort(Comparator.comparing(LogGroup::getLogGroupName));
return result;
}
PaginatedResult<LogGroup> describeLogGroups(String prefix, String region, int limit, String nextToken) {
if (limit < 1 || limit > 50) {
String constraint = limit < 1 ? "greater than or equal to 1" : "less than or equal to 50";
throw SmithyValidationErrors.exception("InvalidParameterException",
"Value '" + limit + "' at 'limit' failed to satisfy constraint: Member must have value " + constraint);
}
if (nextToken != null && nextToken.isEmpty()) {
throw invalidNextToken();
}
return Pagination.paginate(matchingLogGroups(prefix, region), LogGroup::getLogGroupName, limit, nextToken,
"DescribeLogGroups:" + region + ":" + (prefix == null ? "" : prefix),
token -> invalidNextToken());
}
private List<LogGroup> matchingLogGroups(String prefix, String region) {
String storagePrefix = groupKeyPrefix(region);
List<LogGroup> result = groupStore.scan(k -> {
return groupStore.scan(k -> {
if (!k.startsWith(storagePrefix)) {
return false;
}
@@ -372,8 +394,6 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String groupName = k.substring(storagePrefix.length());
return groupName.startsWith(prefix);
});
result.sort(Comparator.comparing(LogGroup::getLogGroupName));
return result;
}
public long getStoredBytesForLogGroup(String groupName, String region) {
@@ -650,7 +670,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
return Base64Url.encode(raw);
}
private static AwsException invalidNextToken() {
static AwsException invalidNextToken() {
return new AwsException("InvalidParameterException", "The specified nextToken is invalid.", 400);
}