mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -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
+124
@@ -0,0 +1,124 @@
|
||||
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.FilterLogEventsResponse;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.model.FilteredLogEvent;
|
||||
import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("CloudWatch Logs FilterLogEvents pagination")
|
||||
class CloudWatchLogsFilterPaginationTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("the SDK paginator walks every match once and terminates")
|
||||
void paginatorWalksEveryMatchOnceAndTerminates() {
|
||||
String groupName = "/test/" + TestFixtures.uniqueName("logs-filter-pagination");
|
||||
String streamName = "stream-1";
|
||||
|
||||
try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
|
||||
try {
|
||||
logs.createLogGroup(request -> request.logGroupName(groupName));
|
||||
logs.createLogStream(request -> request
|
||||
.logGroupName(groupName)
|
||||
.logStreamName(streamName));
|
||||
|
||||
long base = System.currentTimeMillis();
|
||||
List<InputLogEvent> events = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
int index = i;
|
||||
events.add(InputLogEvent.builder()
|
||||
.timestamp(base + index)
|
||||
.message("msg-" + index)
|
||||
.build());
|
||||
}
|
||||
logs.putLogEvents(request -> request
|
||||
.logGroupName(groupName)
|
||||
.logStreamName(streamName)
|
||||
.logEvents(events));
|
||||
|
||||
// The paginator is the point of the test. This SDK's FilterLogEventsIterable
|
||||
// keeps requesting while nextToken is non-empty and, unlike GetLogEventsIterable,
|
||||
// has no guard against being handed the same token twice. A response that
|
||||
// advertises a next page on the final page therefore never terminates here.
|
||||
List<String> messages = new ArrayList<>();
|
||||
List<FilterLogEventsResponse> pages = new ArrayList<>();
|
||||
logs.filterLogEventsPaginator(request -> request
|
||||
.logGroupName(groupName)
|
||||
.limit(2))
|
||||
.stream()
|
||||
.forEach(page -> {
|
||||
pages.add(page);
|
||||
page.events().stream().map(FilteredLogEvent::message).forEach(messages::add);
|
||||
});
|
||||
|
||||
assertThat(messages)
|
||||
.as("every match is seen exactly once, oldest first")
|
||||
.containsExactly("msg-0", "msg-1", "msg-2", "msg-3", "msg-4");
|
||||
assertThat(pages).hasSize(3);
|
||||
assertThat(pages.get(pages.size() - 1).nextToken())
|
||||
.as("an absent token is how FilterLogEvents signals the end")
|
||||
.isNull();
|
||||
} finally {
|
||||
deleteIfPresent(logs, groupName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a token from a previous page resumes rather than restarting")
|
||||
void tokenResumesRatherThanRestarting() {
|
||||
String groupName = "/test/" + TestFixtures.uniqueName("logs-filter-pagination-resume");
|
||||
String streamName = "stream-1";
|
||||
|
||||
try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
|
||||
try {
|
||||
logs.createLogGroup(request -> request.logGroupName(groupName));
|
||||
logs.createLogStream(request -> request
|
||||
.logGroupName(groupName)
|
||||
.logStreamName(streamName));
|
||||
|
||||
long base = System.currentTimeMillis();
|
||||
logs.putLogEvents(request -> request
|
||||
.logGroupName(groupName)
|
||||
.logStreamName(streamName)
|
||||
.logEvents(
|
||||
InputLogEvent.builder().timestamp(base).message("first").build(),
|
||||
InputLogEvent.builder().timestamp(base + 1).message("second").build(),
|
||||
InputLogEvent.builder().timestamp(base + 2).message("third").build()));
|
||||
|
||||
FilterLogEventsResponse firstPage = logs.filterLogEvents(request -> request
|
||||
.logGroupName(groupName)
|
||||
.limit(2));
|
||||
assertThat(firstPage.events()).extracting(FilteredLogEvent::message)
|
||||
.containsExactly("first", "second");
|
||||
assertThat(firstPage.nextToken()).isNotNull();
|
||||
|
||||
FilterLogEventsResponse secondPage = logs.filterLogEvents(request -> request
|
||||
.logGroupName(groupName)
|
||||
.limit(2)
|
||||
.nextToken(firstPage.nextToken()));
|
||||
assertThat(secondPage.events()).extracting(FilteredLogEvent::message)
|
||||
.containsExactly("third");
|
||||
assertThat(secondPage.nextToken()).isNull();
|
||||
} finally {
|
||||
deleteIfPresent(logs, groupName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void deleteIfPresent(CloudWatchLogsClient logs, String groupName) {
|
||||
boolean present = logs.describeLogGroups(request -> request.logGroupNamePrefix(groupName))
|
||||
.logGroups()
|
||||
.stream()
|
||||
.anyMatch(group -> groupName.equals(group.logGroupName()));
|
||||
if (present) {
|
||||
logs.deleteLogGroup(request -> request.logGroupName(groupName));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user