Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logs): support regex patterns for JSON Metrics filters #30741

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/aws-cdk-lib/aws-logs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'),
);
```

Expand Down
31 changes: 31 additions & 0 deletions packages/aws-cdk-lib/aws-logs/lib/pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
*
Expand Down Expand Up @@ -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
*/
Expand Down
5 changes: 4 additions & 1 deletion packages/aws-cdk-lib/aws-logs/test/integ.metricfilter.lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions packages/aws-cdk-lib/aws-logs/test/pattern.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading