From df8989cf5bf891d2e8b3aca55d69d896fb5184da Mon Sep 17 00:00:00 2001 From: Niels Kootstra <545768+nkootstra@users.noreply.github.com> Date: Thu, 26 Nov 2026 19:43:51 +0100 Subject: [PATCH] fix(logs): read a minus followed by a space as an exclusion in a filter pattern (#9539) A term filter pattern with a space between the minus and the excluded term, "ERROR" - "Unauthorized", was rejected with "expected a value", while AWS reads it as an exclusion, the same as "ERROR" -"Unauthorized". The term parser now skips whitespace after a minus and reads the next term as excluded, so metric filters, subscription filters and FilterLogEvents accept it alike. --- docs/services/cloudwatch.md | 2 +- .../aws/services/cloudwatch/logs/filter/TermPattern.java | 4 +++- .../services/cloudwatch/logs/filter/FilterPatternTest.java | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/services/cloudwatch.md b/docs/services/cloudwatch.md index 109d6ec766..a1aa016a9a 100644 --- a/docs/services/cloudwatch.md +++ b/docs/services/cloudwatch.md @@ -249,7 +249,7 @@ Samples are readable with `GetMetricStatistics` and `GetMetricData`, and alarms The filter pattern syntax follows the AWS reference: - Terms: `ERROR ARGUMENTS` (all present), `?ERROR ?ARGUMENTS` (any present), `ERROR -ARGUMENTS` - (exclusion), `"exact phrase"`. Terms are case-sensitive substrings of the message. + or `ERROR - ARGUMENTS` (exclusion), `"exact phrase"`. Terms are case-sensitive substrings of the message. - Regular expressions between percent signs, `%^[hc]at%`, with the operators and escapes AWS allows. Parentheses and characters outside ASCII are rejected, as on AWS. - JSON patterns: `{ $.eventType = "UpdateTrail" && $.code >= 400 }`, with `=`, `!=`, `<`, `<=`, diff --git a/src/main/java/io/floci/aws/services/cloudwatch/logs/filter/TermPattern.java b/src/main/java/io/floci/aws/services/cloudwatch/logs/filter/TermPattern.java index eba22895ad..b515d2f3ef 100644 --- a/src/main/java/io/floci/aws/services/cloudwatch/logs/filter/TermPattern.java +++ b/src/main/java/io/floci/aws/services/cloudwatch/logs/filter/TermPattern.java @@ -70,8 +70,10 @@ final class TermPattern extends FilterPattern { private static Term readTerm(PatternCursor cursor) { Mode mode = Mode.INCLUDE; - if (cursor.peek() == '-' && !cursor.lookingAt("- ")) { + if (cursor.peek() == '-') { + // AWS also reads "- term" as an exclusion. cursor.next(); + cursor.skipWhitespace(); mode = Mode.EXCLUDE; } else if (cursor.peek() == '?' && !cursor.lookingAt("? ")) { cursor.next(); diff --git a/src/test/java/io/floci/aws/services/cloudwatch/logs/filter/FilterPatternTest.java b/src/test/java/io/floci/aws/services/cloudwatch/logs/filter/FilterPatternTest.java index 2248fffdaa..2119d29952 100644 --- a/src/test/java/io/floci/aws/services/cloudwatch/logs/filter/FilterPatternTest.java +++ b/src/test/java/io/floci/aws/services/cloudwatch/logs/filter/FilterPatternTest.java @@ -95,6 +95,13 @@ class FilterPatternTest { assertFalse(matches("ERROR -ARGUMENTS", "[ERROR 420] INVALID ARGUMENTS")); } + @Test + void aMinusSeparatedFromItsTermBySpaceStillExcludesIt() { + assertTrue(matches("\"ERROR\" - \"Unauthorized\"", "[ERROR 400] Bad request")); + assertFalse(matches("\"ERROR\" - \"Unauthorized\"", "[ERROR 401] Unauthorized request")); + assertFalse(matches("ERROR - ARGUMENTS", "[ERROR 419] MISSING ARGUMENTS")); + } + @Test void aQuotedSpaceMatchesEverythingWithASpaceAndAnEmptyPatternMatchesEverything() { assertTrue(matches("\" \"", "two words"));