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

chore: add regex capture to label application rule #214

Merged
merged 3 commits into from
Jun 6, 2024

Conversation

saxenakshitiz
Copy link
Contributor

Defined regex capture to perform operation on attribute value like substring etc.

@saxenakshitiz saxenakshitiz requested a review from a team as a code owner June 6, 2024 06:46
Copy link

github-actions bot commented Jun 6, 2024

Test Results

121 tests  ±0   121 ✅ ±0   59s ⏱️ -1s
 29 suites ±0     0 💤 ±0 
 29 files   ±0     0 ❌ ±0 

Results for commit 19f47cd. ± Comparison against base commit c0dd217.

♻️ This comment has been updated with latest results.

@sanket-mundra sanket-mundra self-requested a review June 6, 2024 08:41
if (tokenExtractionRule.hasAlias()) {
validKeys.add(tokenExtractionRule.getAlias());
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unintentional change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. If alias is provided then alias should be used not key.

Copy link
Contributor

@sanket-mundra sanket-mundra Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. So, till now we were validating key in case of alias as well, even though it wasn't getting used later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier both alias and key were being used. I will change to use alias if alias is provided.
Please note this expression is not exposed in UI so far, so no one should be using it.

sanket-mundra
sanket-mundra previously approved these changes Jun 6, 2024
if (tokenExtractionRule.hasAlias()) {
validKeys.add(tokenExtractionRule.getAlias());
} else {
Copy link
Contributor

@sanket-mundra sanket-mundra Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. So, till now we were validating key in case of alias as well, even though it wasn't getting used later?

@@ -264,6 +266,15 @@ public void validateLabelExpression(Action.DynamicLabel dynamicLabel) {
private void validateTokenExtractionRule(
Action.DynamicLabel.TokenExtractionRule tokenExtractionRule) {
validateNonDefaultPresenceOrThrow(tokenExtractionRule, tokenExtractionRule.KEY_FIELD_NUMBER);
if (tokenExtractionRule.hasRegexCapture()) {
try {
Pattern.compile(tokenExtractionRule.getRegexCapture());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have a validator somewhere that validates a number of capture groups - and we should be using re2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh we are :) I forget they use identical names to make it drop in. Anyway, RegexValidator.validateCaptureGroupCount is what I was referring to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@saxenakshitiz saxenakshitiz merged commit 31fc31e into main Jun 6, 2024
9 checks passed
@saxenakshitiz saxenakshitiz deleted the regex_capture branch June 6, 2024 13:56
@@ -264,6 +265,9 @@ public void validateLabelExpression(Action.DynamicLabel dynamicLabel) {
private void validateTokenExtractionRule(
Action.DynamicLabel.TokenExtractionRule tokenExtractionRule) {
validateNonDefaultPresenceOrThrow(tokenExtractionRule, tokenExtractionRule.KEY_FIELD_NUMBER);
if (tokenExtractionRule.hasRegexCapture()) {
RegexValidator.validateCaptureGroupCount(tokenExtractionRule.getRegexCapture(), 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we restricting only to one capture group?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we need a single value, we can use value only from one group

Copy link
Contributor

@sanket-mundra sanket-mundra Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was thinking of allowing users to use the values of each group accordingly if they want to like we have in Naming rules. for example: if the attribute value like: domain.sub-domain1.sub-domain2 and we want to extract: domain:sub-domain2 for label, then we need to support multiple capture groups.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd create create two rules with one capture group each.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that would lead to two different labels, right? If we want both of of them in a single label?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - a dynamic label defines tokens and an expression that indicates how to combine them - this PR is adding a new way to extract a token:

    message DynamicLabel {
      string label_expression = 1; // expression to combine tokens to generate label, example "${deployment}_${version}"
      repeated TokenExtractionRule token_extraction_rules = 2;
      message TokenExtractionRule {
        string key = 1;
        optional string json_path = 2;
        optional string alias = 3;
        optional string regex_capture = 4;
      }
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, okay

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants