mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(cloudwatch): page FilterLogEvents forward with a usable nextToken (#2354)
FilterLogEvents returned UUID.randomUUID() as its nextToken and had no nextToken parameter to accept one back, so a response could advertise more results while giving a client no way to reach them. Truncation compounded it: the time and pattern filters ran before .limit(maxEvents) over an ascending-sorted list, so an over-cap query kept the oldest matches and the newest were unreachable. The canonical use of this action is finding the error that just happened across a group, which is exactly the event most likely to be dropped. Accept a nextToken and make it an f/<index> offset into the matched set, reusing the parser GetLogEvents already validates its tokens with so a malformed, negative or overflowing token raises InvalidParameterException here too. The offset indexes matches rather than stored events, so a request narrowed by pattern, time range or stream names pages through only what it matched. Emit the token only when a further page exists, unlike GetLogEvents which echoes its forward token back. The two AWS actions signal exhaustion differently: FilterLogEvents documents an absent nextToken as "pagination is finished", and its SDK paginators keep requesting while the token is present with no same-token guard, so echoing a final token would never terminate. Also refuse to emit a cursor that cannot advance, which a max-events cap configured to zero would otherwise produce, and compute the page end by subtracting from the total so a cap near Integer.MAX_VALUE cannot overflow the index negative. startFromHead is still unsupported, so results run oldest first.
This commit is contained in:
6 files changed
+490
-13
No files matched your search
+3
-1
@@ -203,12 +203,14 @@ public class CloudWatchLogsHandler {
|
||||
Long endTime = request.has("endTime") ? request.path("endTime").asLong() : null;
|
||||
String filterPattern = request.path("filterPattern").asText(null);
|
||||
int limit = request.path("limit").asInt(0);
|
||||
String nextToken = request.has("nextToken") ? request.path("nextToken").asText(null) : null;
|
||||
|
||||
List<String> streamNames = new ArrayList<>();
|
||||
request.path("logStreamNames").forEach(n -> streamNames.add(resolveLogStreamName(n.asText(null))));
|
||||
|
||||
CloudWatchLogsService.FilteredLogEventsResult result =
|
||||
logsService.filterLogEvents(groupName, streamNames, startTime, endTime, filterPattern, limit, region);
|
||||
logsService.filterLogEvents(groupName, streamNames, startTime, endTime, filterPattern, limit,
|
||||
nextToken, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.set("events", buildFilteredEventsArray(result.events()));
|
||||
|
||||
+27
-5
@@ -475,7 +475,7 @@ public class CloudWatchLogsService {
|
||||
public FilteredLogEventsResult filterLogEvents(String groupName, List<String> streamNames,
|
||||
Long startTime, Long endTime,
|
||||
String filterPattern, int limit,
|
||||
String region) {
|
||||
String nextToken, String region) {
|
||||
int maxEvents = Math.min(limit > 0 ? limit : Integer.MAX_VALUE,
|
||||
maxEventsPerQuery);
|
||||
|
||||
@@ -500,16 +500,38 @@ public class CloudWatchLogsService {
|
||||
|
||||
all.sort(Comparator.comparing(FilteredEvent::event, EVENT_ORDER));
|
||||
|
||||
List<FilteredEvent> result = all.stream()
|
||||
List<FilteredEvent> matches = all.stream()
|
||||
.filter(f -> (startTime == null || f.event().getTimestamp() >= startTime)
|
||||
&& (endTime == null || f.event().getTimestamp() <= endTime))
|
||||
.filter(f -> filterPattern == null || filterPattern.isBlank()
|
||||
|| f.event().getMessage().contains(filterPattern))
|
||||
.limit(maxEvents)
|
||||
.toList();
|
||||
|
||||
String nextToken = result.size() >= maxEvents ? UUID.randomUUID().toString() : null;
|
||||
return new FilteredLogEventsResult(result, nextToken);
|
||||
// The cursor indexes matches rather than stored events, which is why the window is applied
|
||||
// here instead of folded into the stream as a limit. Forward-only, so one prefix where
|
||||
// GetLogEvents needs two.
|
||||
int total = matches.size();
|
||||
int pageStart;
|
||||
if (nextToken != null && nextToken.startsWith("f/")) {
|
||||
pageStart = Math.min(parseTokenIndex(nextToken, 2), total);
|
||||
} else if (nextToken != null) {
|
||||
throw invalidNextToken();
|
||||
} else {
|
||||
pageStart = 0;
|
||||
}
|
||||
// Subtracting from `total` rather than adding to `pageStart` keeps the arithmetic inside
|
||||
// the list's own bounds, so a max-events cap configured near Integer.MAX_VALUE cannot
|
||||
// overflow the end index negative.
|
||||
int pageEnd = pageStart + Math.min(maxEvents, total - pageStart);
|
||||
|
||||
List<FilteredEvent> page = matches.subList(pageStart, pageEnd);
|
||||
|
||||
// Unlike GetLogEvents, which echoes its token back so an SDK paginator can stop on a repeat,
|
||||
// FilterLogEvents signals exhaustion by omitting the token, and its paginators keep going
|
||||
// while one is present. The second clause refuses a cursor that cannot advance, which a
|
||||
// max-events cap of zero would otherwise produce.
|
||||
String pageToken = pageEnd < total && pageEnd > pageStart ? "f/" + pageEnd : null;
|
||||
return new FilteredLogEventsResult(page, pageToken);
|
||||
}
|
||||
|
||||
// ──────────────────────────── Logs Insights Queries ────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user