Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudwatch): match FilterLogEvents with the filter pattern syntax (#5903)

Alessandro Lattao964756145e63
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsFilterPatternTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsFilterPatternTest.java
new file mode 100644
index 0000000000..71667ba9ad
--- /dev/null
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/CloudWatchLogsFilterPatternTest.java
@@ -0,0 +1,98 @@
+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.FilteredLogEvent;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InputLogEvent;
+import software.amazon.awssdk.services.cloudwatchlogs.model.InvalidParameterException;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * FilterLogEvents reads filterPattern with the filter pattern syntax. The events and the matches are
+ * the ones measured on AWS (eu-west-1, 2026-10-16) on one stream holding these five events.
+ */
+@DisplayName("CloudWatch Logs FilterLogEvents filter patterns")
+class CloudWatchLogsFilterPatternTest {
+
+ private static final List<String> EVENTS = List.of(
+ "{\"time\":\"t\",\"level\":\"INFO\",\"msg\":\"mail not sent\",\"template\":\"sign-in-code\","
+ + "\"substitutions\":{\"code\":\"694692\"}}",
+ "{\"level\":\"ERROR\",\"msg\":\"boom\",\"template\":\"other\"}",
+ "ERROR something failed in sign-in-code path",
+ "WARN slow response",
+ "INFO all good");
+
+ @Test
+ @DisplayName("each pattern matches the events AWS matches")
+ void eachPatternMatchesTheEventsAwsMatches() {
+ String groupName = "/test/" + TestFixtures.uniqueName("logs-filter-pattern");
+ String streamName = "stream-1";
+
+ try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
+ logs.createLogGroup(request -> request.logGroupName(groupName));
+ try {
+ logs.createLogStream(request -> request.logGroupName(groupName).logStreamName(streamName));
+ long base = System.currentTimeMillis();
+ List<InputLogEvent> events = new ArrayList<>();
+ for (int i = 0; i < EVENTS.size(); i++) {
+ events.add(InputLogEvent.builder().timestamp(base + i).message(EVENTS.get(i)).build());
+ }
+ logs.putLogEvents(request -> request
+ .logGroupName(groupName)
+ .logStreamName(streamName)
+ .logEvents(events));
+
+ assertThat(matches(logs, groupName, "{ $.template = \"sign-in-code\" }")).containsExactly(1);
+ assertThat(matches(logs, groupName, "{ $.level = \"INFO\" }")).containsExactly(1);
+ assertThat(matches(logs, groupName, "{ $.substitutions.code = 694692 }")).containsExactly(1);
+ assertThat(matches(logs, groupName, "{ $.substitutions.code = \"694692\" }")).containsExactly(1);
+ assertThat(matches(logs, groupName, "sign-in-code")).isEmpty();
+ assertThat(matches(logs, groupName, "\"sign-in-code\"")).containsExactly(1, 3);
+ assertThat(matches(logs, groupName, "ERROR")).containsExactly(2, 3);
+ assertThat(matches(logs, groupName, "error")).isEmpty();
+ assertThat(matches(logs, groupName, "?ERROR ?WARN")).containsExactly(2, 3, 4);
+ assertThat(matches(logs, groupName, "ERROR -sign")).containsExactly(2);
+ assertThat(matches(logs, groupName, "\"mail not sent\"")).containsExactly(1);
+ assertThat(matches(logs, groupName, "mail")).containsExactly(1);
+ assertThat(matches(logs, groupName, "{ $.missing = \"x\" }")).isEmpty();
+ } finally {
+ logs.deleteLogGroup(request -> request.logGroupName(groupName));
+ }
+ }
+ }
+
+ @Test
+ @DisplayName("an invalid pattern is refused with InvalidParameterException")
+ void anInvalidPatternIsRefused() {
+ String groupName = "/test/" + TestFixtures.uniqueName("logs-filter-pattern-invalid");
+
+ try (CloudWatchLogsClient logs = TestFixtures.cloudWatchLogsClient()) {
+ logs.createLogGroup(request -> request.logGroupName(groupName));
+ try {
+ assertThatThrownBy(() -> matches(logs, groupName, "{ $.x = }"))
+ .isInstanceOf(InvalidParameterException.class);
+ assertThatThrownBy(() -> matches(logs, groupName, "[a, b"))
+ .isInstanceOf(InvalidParameterException.class);
+ } finally {
+ logs.deleteLogGroup(request -> request.logGroupName(groupName));
+ }
+ }
+ }
+
+ /** The 1-based positions in {@link #EVENTS} of the events the pattern matches, oldest first. */
+ private static List<Integer> matches(CloudWatchLogsClient logs, String groupName, String pattern) {
+ List<Integer> positions = new ArrayList<>();
+ logs.filterLogEventsPaginator(request -> request.logGroupName(groupName).filterPattern(pattern))
+ .events()
+ .stream()
+ .map(FilteredLogEvent::message)
+ .forEach(message -> positions.add(EVENTS.indexOf(message) + 1));
+ return positions;
+ }
+}
diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.md
index f61696e299..ed80ece055 100644
--- a/docs/services/cloudwatch.md
+++ b/docs/services/cloudwatch.md
@@ -55,13 +55,16 @@ Log group deletion protection defaults to disabled and is persisted with the log
enabled, `DeleteLogGroup` returns `ValidationException` until protection is explicitly disabled
with `PutLogGroupDeletionProtection`.
-Two actions are currently simplified:
+One action is currently simplified:
- **`GetDataProtectionPolicy`** does not model data-protection policies. It returns
HTTP 200 with the resolved `logGroupIdentifier` and no `policyDocument`, including for
a log group that does not exist, where real AWS returns `ResourceNotFoundException`.
-- **`FilterLogEvents`** matches `filterPattern` as a plain substring of the message. The full
- filter pattern syntax described below is applied by metric filters and subscription filters.
+
+`FilterLogEvents` reads `filterPattern` with the filter pattern syntax described below, the one
+metric filters and subscription filters use: terms, quoted phrases, `?` alternatives, `-`
+exclusions, JSON and space-delimited patterns. An invalid pattern is refused with
+`InvalidParameterException`.
### Subscription filters {#subscription-filters}
@@ -233,9 +236,8 @@ When a `Complete` result set looks narrower or wider than intended, check the qu
compound `filter` or a stray `=` first: those cases produce neither a failure nor a warning. The
only log line is the usual INFO summary, `Logs Insights query <id>: scanned ... -> <n> row(s)`.
-For simple substring matching, `FilterLogEvents` is the more predictable option today. Note that
-Floci matches `--filter-pattern` as a plain substring of the message; the real filter-pattern
-syntax (`?ERROR ?WARN`, `{ $.level = "ERROR" }`, and so on) is not parsed.
+For matching events by a pattern, `FilterLogEvents` is the more predictable option today: it
+applies the filter pattern syntax (`?ERROR ?WARN`, `{ $.level = "ERROR" }`, and so on) as AWS does.
### Reading events past the limit
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
index 5fb6adac67..68b07e61d8 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsService.java
@@ -932,6 +932,11 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
String nextToken, String region) {
int maxEvents = Math.min(limit > 0 ? limit : Integer.MAX_VALUE,
maxEventsPerQuery);
+ // The filter pattern syntax metric and subscription filters use: terms, quoted phrases,
+ // ?alternatives, -exclusions, JSON and space-delimited patterns. Parsed before the scan,
+ // so an invalid pattern is refused whatever the group holds.
+ FilterPattern pattern = filterPattern == null || filterPattern.isBlank()
+ ? null : requireFilterPattern(filterPattern);
String groupPrefix = groupKeyPrefix(region) + groupName + "::";
List<String> requested = streamNames == null ? List.of() : streamNames;
@@ -957,8 +962,7 @@ public class CloudWatchLogsService implements ResourceProvider, Resettable {
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))
+ .filter(f -> pattern == null || pattern.match(f.event().getMessage()).matched())
.sorted(Comparator.comparing(FilteredEvent::event, EVENT_ORDER))
.toList();
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
index bb928f887d..e752e71c40 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsServiceTest.java
@@ -16,6 +16,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -1245,6 +1246,63 @@ class CloudWatchLogsServiceTest {
assertTrue(result.events().stream().allMatch(f -> f.event().getMessage().contains("ERROR")));
}
+ /**
+ * FilterLogEvents reads its pattern with the filter pattern syntax, not as a substring.
+ * Measured on AWS (eu-west-1, 2026-10-16) against these five events: each pattern matched
+ * exactly the events listed with it.
+ */
+ @Test
+ void filterLogEventsMatchesWithTheFilterPatternSyntax() {
+ service.createLogGroup("/app/logs", null, null, REGION);
+ service.createLogStream("/app/logs", "stream-1", REGION);
+ List<String> messages = List.of(
+ "{\"time\":\"t\",\"level\":\"INFO\",\"msg\":\"mail not sent\",\"template\":\"sign-in-code\","
+ + "\"substitutions\":{\"code\":\"694692\"}}",
+ "{\"level\":\"ERROR\",\"msg\":\"boom\",\"template\":\"other\"}",
+ "ERROR something failed in sign-in-code path",
+ "WARN slow response",
+ "INFO all good");
+ long now = System.currentTimeMillis();
+ List<Map<String, Object>> events = new ArrayList<>();
+ for (int i = 0; i < messages.size(); i++) {
+ events.add(Map.of("timestamp", now + i, "message", messages.get(i)));
+ }
+ service.putLogEvents("/app/logs", "stream-1", events, REGION);
+
+ Map<String, List<Integer>> expected = new LinkedHashMap<>();
+ expected.put("{ $.template = \"sign-in-code\" }", List.of(0));
+ expected.put("{ $.level = \"INFO\" }", List.of(0));
+ expected.put("{ $.substitutions.code = 694692 }", List.of(0));
+ expected.put("{ $.substitutions.code = \"694692\" }", List.of(0));
+ expected.put("sign-in-code", List.of());
+ expected.put("\"sign-in-code\"", List.of(0, 2));
+ expected.put("ERROR", List.of(1, 2));
+ expected.put("error", List.of());
+ expected.put("?ERROR ?WARN", List.of(1, 2, 3));
+ expected.put("ERROR -sign", List.of(1));
+ expected.put("\"mail not sent\"", List.of(0));
+ expected.put("mail", List.of(0));
+ expected.put("{ $.missing = \"x\" }", List.of());
+ for (Map.Entry<String, List<Integer>> pattern : expected.entrySet()) {
+ List<String> matched = service.filterLogEvents("/app/logs", null, null, null, pattern.getKey(),
+ 100, null, REGION).events().stream().map(f -> f.event().getMessage()).toList();
+ assertEquals(pattern.getValue().stream().map(messages::get).toList(), matched, pattern.getKey());
+ }
+ }
+
+ @Test
+ void filterLogEventsRefusesAnInvalidPattern() {
+ service.createLogGroup("/app/logs", null, null, REGION);
+
+ AwsException json = assertThrows(AwsException.class, () -> service.filterLogEvents(
+ "/app/logs", null, null, null, "{ $.x = }", 100, null, REGION));
+ AwsException delimited = assertThrows(AwsException.class, () -> service.filterLogEvents(
+ "/app/logs", null, null, null, "[a, b", 100, null, REGION));
+
+ assertEquals("InvalidParameterException", json.getErrorCode());
+ assertEquals("InvalidParameterException", delimited.getErrorCode());
+ }
+
@Test
void filterLogEventsCarriesEmittingStreamPerEvent() {
service.createLogGroup("/app/logs", null, null, REGION);