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

FilterLogEvents kept an event when its message contained filterPattern as
a substring. A JSON pattern such as { $.template = "sign-in-code" } then
never matched, and terms matched where AWS does not: an unquoted
sign-in-code, which AWS reads with its own term rules, matched every
message containing those characters.

Measured on AWS (eu-west-1, 2026-10-16) on five events, JSON, plain and
mixed: JSON patterns over nested paths and numbers, case-sensitive terms,
quoted phrases, ?alternatives and -exclusions each matched exactly the
events the parser Floci already uses for metric and subscription filters
matches. So FilterLogEvents now parses its pattern with it, once, before
the scan, and an invalid one is refused with InvalidParameterException
whatever the group holds.

The error text is the parser's own, as for PutMetricFilter and
TestMetricFilter; AWS words these differently per action, which is left
for a change of its own.

Tests: the measured events and patterns in CloudWatchLogsServiceTest and,
through the SDK, CloudWatchLogsFilterPatternTest, which fails on main.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-17 20:17:59 -04:00
1 parent c229311f8f
commit 964756145e
4 files changed
+170 -8

No files matched your search

@@ -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;
}
}
+8 -6
View File
@@ -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
@@ -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();
@@ -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);