Light, fluffy, and always free - The AWS Local Emulator alternative
fix(cloudwatch): warn on unsupported Logs Insights filter operators (#2170)
soreavisf1c81977ec39
diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.mdindex f52cc86068..1abe72902e 100644--- a/docs/services/cloudwatch.md+++ b/docs/services/cloudwatch.md@@ -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 thatFloci matches `--filter-pattern` as a plain substring of the message; the real filter-patterndiff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogsInsightsQuery.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogsInsightsQuery.javaindex 3f0bbb4412..9af0e37c05 100644--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogsInsightsQuery.java+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/logs/LogsInsightsQuery.java@@ -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) ? "=="diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInsightsQueryTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInsightsQueryTest.javaindex 2c7998c941..6ffab56818 100644--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInsightsQueryTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/logs/CloudWatchLogsInsightsQueryTest.java@@ -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"));+ }+@Testvoid 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);