diff --git a/packages/aws-cdk-lib/aws-logs/README.md b/packages/aws-cdk-lib/aws-logs/README.md index 2276fddfb2f7f..88f9f1fcd35a7 100644 --- a/packages/aws-cdk-lib/aws-logs/README.md +++ b/packages/aws-cdk-lib/aws-logs/README.md @@ -262,6 +262,8 @@ and then descending into it, such as `$.field` or `$.list[0].field`. * `FilterPattern.stringValue(field, comparison, string)`: matches if the given field compares as indicated with the given string value. +* `FilterPattern.regexValue(field, comparison, string)`: matches if the given + field compares as indicated with the given regex pattern. * `FilterPattern.numberValue(field, comparison, number)`: matches if the given field compares as indicated with the given numerical value. * `FilterPattern.isNull(field)`: matches if the given field exists and has the @@ -291,6 +293,7 @@ const pattern = logs.FilterPattern.all( logs.FilterPattern.booleanValue('$.error', true), logs.FilterPattern.numberValue('$.latency', '>', 1000), ), + logs.FilterPattern.regexValue('$.message', '=', 'bind address already in use'), ); ``` diff --git a/packages/aws-cdk-lib/aws-logs/lib/pattern.ts b/packages/aws-cdk-lib/aws-logs/lib/pattern.ts index 279ec55f9f372..bffad0ef04d75 100644 --- a/packages/aws-cdk-lib/aws-logs/lib/pattern.ts +++ b/packages/aws-cdk-lib/aws-logs/lib/pattern.ts @@ -117,6 +117,27 @@ export class FilterPattern { return new JSONNumberPattern(jsonField, comparison, value); } + /** + * A JSON log pattern that compares against a Regex values. + * + * This pattern only matches if the event is a JSON event, and the indicated field inside + * compares with the regex value. + * + * Use '$' to indicate the root of the JSON structure. The comparison operator can only + * compare equality or inequality. + * + * For more information, see: + * + * https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html + * + * @param jsonField Field inside JSON. Example: "$.myField" + * @param comparison Comparison to carry out. Either = or !=. + * @param value The regex value to compare to. + */ + public static regexValue(jsonField: string, comparison: string, value: string): JsonPattern { + return new JSONRegexPattern(jsonField, comparison, value); + } + /** * A JSON log pattern that matches if the field exists and has the special value 'null'. * @@ -226,6 +247,16 @@ class JSONStringPattern extends JsonPattern { } } +/** + * A regex comparison for JSON patterns + */ +class JSONRegexPattern extends JsonPattern { + public constructor(jsonField: string, comparison: string, value: string) { + // No validation, we assume these are generated by trusted factory functions + super(`${jsonField} ${comparison} %${value}%`); + } +} + /** * A number comparison for JSON values */ diff --git a/packages/aws-cdk-lib/aws-logs/test/integ.metricfilter.lit.ts b/packages/aws-cdk-lib/aws-logs/test/integ.metricfilter.lit.ts index dd61bf8ddedc9..9b5e8be5841c4 100644 --- a/packages/aws-cdk-lib/aws-logs/test/integ.metricfilter.lit.ts +++ b/packages/aws-cdk-lib/aws-logs/test/integ.metricfilter.lit.ts @@ -14,7 +14,10 @@ class MetricFilterIntegStack extends Stack { logGroup, metricNamespace: 'MyApp', metricName: 'Latency', - filterPattern: FilterPattern.exists('$.latency'), + filterPattern: FilterPattern.all( + FilterPattern.exists('$.latency'), + FilterPattern.regexValue('$.message', '==', 'bind: address already in use'), + ), metricValue: '$.latency', }); /// !hide diff --git a/packages/aws-cdk-lib/aws-logs/test/pattern.test.ts b/packages/aws-cdk-lib/aws-logs/test/pattern.test.ts index b49c9cd220588..4c277e344671c 100644 --- a/packages/aws-cdk-lib/aws-logs/test/pattern.test.ts +++ b/packages/aws-cdk-lib/aws-logs/test/pattern.test.ts @@ -51,6 +51,12 @@ describe('pattern', () => { expect('{ $.field = "value" }').toEqual(pattern.logPatternString); }); + test('regex pattern', () => { + const pattern = FilterPattern.regexValue('$.field', '==', 'value'); + + expect('{ $.field = %value% }').toEqual(pattern.logPatternString); + }); + test('number patterns', () => { const pattern = FilterPattern.numberValue('$.field', '<=', 300);