fix(logs): support or in Logs Insights filters (#7924)

* fix(logs): support or in Logs Insights filters

A filter or where stage now keeps rows matching any or-joined comparison, case-insensitive, with parenthesized and nested groups. Syntax the engine does not model (and, not, ||, escapes, backtick fields, expressions in place of a field, empty stages) fails the query instead of returning a silently wrong result set.

Closes #7907

* fix(logs): limit Insights bare filter values to numbers and enforce the queryString length

An unquoted filter value is now a number compared by value, as on AWS (n = 2 matches 2 and 2.0).
Other unquoted values, such as a word or 1+1, fail the query instead of being read as text.
StartQuery rejects a queryString over 10,000 characters with InvalidParameterException, which
also bounds the nesting the or parser walks.

* fix(logs): keep Insights field parsing stack-safe and count queryString in code points

A long dotted field such as a.a.a... overflowed the stack in the field regex, which repeated a
group recursively; the repetition is now possessive and runs in a loop. The queryString limit
uses the shared Smithy length check, which counts code points as AWS does, so a query of
emoji under 10,000 code points is no longer rejected.

* refactor(logs): share one quote-skipping scanner across the Insights parser
This commit is contained in:
Mario Toffia authored and GitHub committed 2026-11-10 16:18:56 -04:00
1 parent 0e244e5724
commit f37afdad81
5 files changed
+324 -71

No files matched your search

@@ -67,13 +67,7 @@ class CloudWatchLogsInsightsTest {
@Test
@DisplayName("GetQueryResults filters by JSON field, drops TRACE, sorts desc and dedups")
void getQueryResultsFiltersSortsDedups() {
// On real AWS, events are not queryable for several seconds after ingestion; retry the
// full start+poll cycle until rows appear or the ~60s deadline elapses.
GetQueryResultsResponse res = pollUntilComplete(startTestQuery().queryId());
long deadline = System.currentTimeMillis() + 60_000;
while (res.results().isEmpty() && System.currentTimeMillis() < deadline) {
res = pollUntilComplete(startTestQuery().queryId());
}
GetQueryResultsResponse res = resultsOf(query());
assertThat(res.status()).isEqualTo(QueryStatus.COMPLETE);
@@ -97,6 +91,19 @@ class CloudWatchLogsInsightsTest {
assertThat(messages).noneMatch(m -> m.contains("verbose noise") || m.contains("unrelated"));
}
@Test
@DisplayName("GetQueryResults keeps rows matching either side of an or filter")
void getQueryResultsOrFilter() {
GetQueryResultsResponse res = resultsOf("fields @timestamp, @message"
+ " | filter params.job_id = 'other-job' or level = 'ERROR'"
+ " | sort @timestamp desc");
assertThat(res.status()).isEqualTo(QueryStatus.COMPLETE);
assertThat(res.results()).hasSize(2);
assertThat(messageOf(res.results().get(0))).contains("job boom");
assertThat(messageOf(res.results().get(1))).contains("unrelated");
}
@Test
@DisplayName("StopQuery on a finished query fails with InvalidParameterException")
void stopFinishedQueryFailsNotRunning() {
@@ -138,12 +145,27 @@ class CloudWatchLogsInsightsTest {
// ──────────────────────────── helpers ────────────────────────────
private static StartQueryResponse startTestQuery() {
return startTestQuery(query());
}
private static StartQueryResponse startTestQuery(String queryString) {
long nowSec = baseMs / 1000;
return logs.startQuery(b -> b
.logGroupNames(GROUP)
.startTime(nowSec - 300)
.endTime(nowSec + 300)
.queryString(query()));
.queryString(queryString));
}
private static GetQueryResultsResponse resultsOf(String queryString) {
// On real AWS, events are not queryable for several seconds after ingestion; retry the
// full start+poll cycle until rows appear or the ~60s deadline elapses.
GetQueryResultsResponse res = pollUntilComplete(startTestQuery(queryString).queryId());
long deadline = System.currentTimeMillis() + 60_000;
while (res.results().isEmpty() && System.currentTimeMillis() < deadline) {
res = pollUntilComplete(startTestQuery(queryString).queryId());
}
return res;
}
private static String query() {