fix(cloudwatch): warn on unsupported Logs Insights filter operators (#2170)

parseFilter scans for the leftmost '!=', '==' or '=' outside a quoted
value, so the '=' inside '>=', '<=' and '=~' won. For '>=' and '<=' the
comparison character was absorbed into the field name ("@message >"),
which resolves to null for every row, and passesFilters treats null as
non-matching; for '=~' the value became the literal "~ /ERROR/". Either
way the query came back with no rows and no warning, because as far as
parseFilter was concerned it had found a valid operator.

Stop the scan when a position starts one of those three operators, so
control reaches the "Ignoring unsupported Logs Insights filter" warning
that '<', '>', 'like', 'not like' and 'in' already hit, and the stage is
dropped instead. Quote tracking is untouched, so a '>=' inside a quoted
value is still just part of the value.

The docs table describing how unsupported syntax degrades had the same
two cases inverted: '=~' listed as warn-and-drop when it mis-parsed, and
bare '<' / '>' as silent-zero-rows when they already warned. Folded into
the one row that now matches the code, and the compound and/or mis-parse
this change does not fix got its own row instead of staying implied.

Refs #2042
This commit is contained in:
soreavis authored and GitHub committed 2026-08-12 10:52:24 -04:00
1 parent 819bfedc6c
commit f1c81977ec
3 files changed
+57 -6

No files matched your search

+4 -4
View File
@@ -69,14 +69,14 @@ Unsupported syntax never fails the query, so it is worth knowing how each case d
| Input | Result |
|---|---|
| An unsupported command (`stats`, `parse`, ...) | Skipped with a warning in the server log. No aggregation happens |
| A `filter` whose operator is not `=`, `!=` or `==` — for example `like /ERROR/` or `=~ /ERROR/` | The whole stage is dropped with a warning, so **every** row is returned |
| A `filter` using `<`, `<=`, `>` or `>=` | The `=` is taken as the operator and the rest of the token becomes part of the field name, which then resolves to nothing — so the row never matches and you get **no** rows. No warning is logged |
| A `filter` whose operator is not `=`, `!=` or `==` — for example `<`, `<=`, `>`, `>=`, `like /ERROR/` or `=~ /ERROR/` | The whole stage is dropped with a warning, so **every** row is returned |
| A `filter` combining conditions with `and` / `or` — for example `filter level = 'ERROR' and status = 200` | Only the leftmost operator is parsed; the rest of the line becomes the compared value, so nothing matches and you get **no** rows. No warning is logged |
| A projected field that does not exist | Rendered as an empty string. No warning |
| A `sort` direction other than `asc` / `desc` | Treated as ascending. No warning |
In short, a query can come back either wider or narrower than intended without any error. When a
result set looks wrong, check the server log for `Ignoring unsupported Logs Insights ...` — and
note that the `>=` case above produces no log line at all.
result set looks wrong, check the server log for `Ignoring unsupported Logs Insights ...` — and note
that the compound-filter case above produces no log line at all.
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
@@ -30,8 +30,9 @@ import java.util.Set;
* {@code @ptr}, or a dotted path into the JSON log message (e.g. {@code params.job_id}, {@code level}).
*
* <p>This is intentionally <em>not</em> a full Insights implementation: unsupported commands
* (e.g. {@code stats}, {@code parse}) are ignored with a warning. Pipes ({@code |}) inside quoted
* filter values are respected and do not split the query into stages.
* (e.g. {@code stats}, {@code parse}) and unsupported filter operators (e.g. {@code >=},
* {@code like}) are ignored with a warning. Pipes ({@code |}) inside quoted filter values are
* respected and do not split the query into stages.
*/
final class LogsInsightsQuery {
@@ -154,6 +155,10 @@ final class LogsInsightsQuery {
}
} else if (c == '\'' || c == '"') {
quote = c;
} else if (expr.startsWith(">=", i) || expr.startsWith("<=", i) || expr.startsWith("=~", i)) {
// The '=' in >=, <= and =~ would be read as equality below, mangling the field
// (>=, <=) or the value (=~) into a dead match; stop so they warn instead.
break;
} else if (i > 0) {
String op = expr.startsWith("!=", i) ? "!="
: expr.startsWith("==", i) ? "=="
@@ -482,6 +482,43 @@ class CloudWatchLogsInsightsQueryTest {
assertTrue(state.rows().get(0).get("@message").contains("kept"));
}
@Test
void unsupportedFilterOperatorsDropTheStageInsteadOfMisparsing() {
String group = "search/query-operators";
String stream = "s-1";
createGroupStream(service, group, stream);
put(group, stream, BASE_MS + 1000, "INFO", "JOB-1", "info-line");
put(group, stream, BASE_MS + 2000, "ERROR", "JOB-1", "error-line");
// '>=', '<=' and '=~' used to lose their comparison character to the equality scan, leaving a
// dead field ("level >") that silently matched nothing. None of these is supported, so each one
// drops the whole stage with a warning and every row comes back.
for (String expr : List.of("level >= 'INFO'", "level <= 'INFO'", "level =~ /INFO/", ">= 'INFO'",
"level > 'INFO'", "level < 'INFO'", "level like /INFO/", "level not like /INFO/",
"level in ['INFO']")) {
assertEquals(2, rowsFor(group, "fields @message | filter " + expr).size(),
"unsupported operator must drop the whole filter stage: " + expr);
}
}
@Test
void supportedFilterOperatorsStillParse() {
String group = "search/query-equality";
String stream = "s-1";
createGroupStream(service, group, stream);
put(group, stream, BASE_MS + 1000, "INFO", "JOB-1", "info-line");
put(group, stream, BASE_MS + 2000, "ERROR", "JOB-1", "error-line");
putRaw(service, group, stream, BASE_MS + 3000, idLog("REQ>=42", "quoted"));
assertEquals(1, rowsFor(group, "fields @message | filter level = 'ERROR'").size());
assertEquals(1, rowsFor(group, "fields @message | filter level == 'ERROR'").size());
assertEquals(2, rowsFor(group, "fields @message | filter level != 'ERROR'").size());
// A '>=' inside the quoted value is not an operator — the real '=' still wins.
assertEquals("REQ>=42",
rowsFor(group, "fields params.id | filter params.id = 'REQ>=42'").get(0).get("params.id"));
}
@Test
void statisticsReportScannedAndMatched() {
String group = "data-studio/insights";
@@ -595,6 +632,15 @@ class CloudWatchLogsInsightsQueryTest {
return svc;
}
/** Run {@code query} over the full window on the default service and return its completed rows. */
private List<LinkedHashMap<String, String>> rowsFor(String group, String query) {
long startSec = BASE_MS / 1000 - 10;
CloudWatchLogsService.QueryState state = service.getQueryResults(
service.startQuery(List.of(group), startSec, startSec + 86400, query, null, REGION));
assertEquals("Complete", state.status());
return state.rows();
}
private void createGroupStream(CloudWatchLogsService svc, String group, String stream) {
svc.createLogGroup(group, null, null, REGION);
svc.createLogStream(group, stream, REGION);