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

@@ -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);