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"));