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

aws-logs: Allow JSON filter patterns to use regex by removing automatic double quotes around pattern strings #30451

Closed
2 tasks
sobesim opened this issue Jun 4, 2024 · 15 comments · Fixed by #30741
Labels
@aws-cdk/aws-logs Related to Amazon CloudWatch Logs effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@sobesim
Copy link

sobesim commented Jun 4, 2024

Describe the feature

FilterPattern.stringValue should allow the user to specify if the value input field is a regex string or not.

Use Case

According to CloudWatch filter pattern documentation, regex expressions must be surrounded by %.

I'm trying to use cdk's FilterPattern.stringValue to match a json document.

CDK automatically adds double quotes in the final filter pattern, which prevents Cloudwatch from recognizing any string as a regex pattern. For example:

  • Correct: { $.foo = %bar% }
  • Incorrect: { $.foo = “%bar%” }, what is produced by CDK and does not match the document : {“foo”:“bar”}

Proposed Solution

Reason for this behavior may be this line: https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-logs/lib/pattern.ts#L399. Perhaps we need another input to stringValue that allows the user to specify if the input string is a regex pattern or not.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.144.0

Environment details (OS name and version, etc.)

Linux

@sobesim sobesim added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Jun 4, 2024
@github-actions github-actions bot added the @aws-cdk/aws-logs Related to Amazon CloudWatch Logs label Jun 4, 2024
@brian-stack
Copy link

Also impacted by this issue.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jun 7, 2024
@pahud
Copy link
Contributor

pahud commented Jun 7, 2024

Thank you for the issue report. Please help us prioritize with 👍 and we welcome pull requests.

@jan-xyz
Copy link
Contributor

jan-xyz commented Jul 3, 2024

I created a PR and maybe one of you is willing to also provide some feedback: #30741

@studds
Copy link

studds commented Jul 23, 2024

Is there any workaround for this?

@jan-xyz
Copy link
Contributor

jan-xyz commented Jul 23, 2024

Yes, provide your own function that adds a regex filter pattern, like the one from the PR I linked.

@jan-xyz
Copy link
Contributor

jan-xyz commented Aug 1, 2024

@pahud anything I can do to reopen the PR I made? It was far from abandoned. I requested support weeks ago and never got any response. Honestly, this felt super discouraging and I don't think I will contribute to CDK in the future.

@paulhcsun
Copy link
Contributor

paulhcsun commented Aug 7, 2024

Hi @jan-xyz, apologies for the late response on your PR. I've re-opened and replied on the PR with the information you've requested. Thank you for the contribution!

@JevinAnderson
Copy link

Hi @jan-xyz , thanks for raising this! Found this while trying to resolve my own similar problem, and used your solution.

I couldn't comment on the CR, so here are two suggestions:

  1. Add a validator. This would be helpful for all users. How many times have we all written code locally that fails once we try it for real? The syntax here could at least be added to a very simple regex, and if the user inputs something forbidden, i.e., a !, they could be directed to this same documentation with a message like, unsupported char, etc...
  2. For the comparison variable type, you could enforce the options like so: comparison: '=' | '!='
public static regexValue(jsonField: string, comparison: '=' | '!=', value: string): JsonPattern {
  return new JSONRegexPattern(jsonField, comparison, value);
}

@amrawadk
Copy link

@jan-xyz just wanted to say thank you for taking the time to document your solution, it helped me as well.

@jan-xyz
Copy link
Contributor

jan-xyz commented Sep 12, 2024

Nice! I am also planning to still do the remaining work for the integration test. It's just very busy at the moment and I didn't find the time to get to it again.

@prplecake
Copy link

@jan-xyz Thanks for the solution!

@jan-xyz
Copy link
Contributor

jan-xyz commented Feb 11, 2025

@paulhcsun I cannot comment on the PR anymore, I finally had the time to pick it up again and added the integration test that was missing. If you have the time, maybe you can check it again?

I only see this:
Image

@jan-xyz
Copy link
Contributor

jan-xyz commented Feb 11, 2025

@JevinAnderson I am not too sure on how to validate the value, I did add the suggested restrictions for the comparison though. That should catch quite a few errors. If you have a suggestion on how to further check the value then let me know.

@paulhcsun
Copy link
Contributor

paulhcsun commented Feb 13, 2025

Hi @jan-xyz, apologies for that inconvenience, I've unlocked the conversation on the PR. Thanks for adding the integ test, I will take a look and leave a review.

@mergify mergify bot closed this as completed in #30741 Feb 14, 2025
mergify bot pushed a commit that referenced this issue Feb 14, 2025
### Issue # (if applicable)

Closes #30451 

### Reason for this change

Support Regex in filter functions for JSON

### Description of changes

Adding a new JSONPattern factory that uses `%` instead of `"` to support the regex pattern.

### Description of how you validated changes

I tried it in our own CDK code. 

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2025
yashkh-amzn pushed a commit to yashkh-amzn/aws-cdk that referenced this issue Feb 21, 2025
### Issue # (if applicable)

Closes aws#30451 

### Reason for this change

Support Regex in filter functions for JSON

### Description of changes

Adding a new JSONPattern factory that uses `%` instead of `"` to support the regex pattern.

### Description of how you validated changes

I tried it in our own CDK code. 

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-logs Related to Amazon CloudWatch Logs effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants