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;
}
}