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
Changes from 1 commit
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
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
Loading