Skip to content

Commit 97744e7

Browse files
authored
fix(cloudwatch): update regex expression that prevents CloudWatch:Mah:UnknownIdentifier warnings (#33591) (#33592)
### Issue # (if applicable) Closes #33591 ### Reason for this change * The validation is causing false-positive warning messages when using partial identifiers on CloudWatch Metric expressions supporting them. ### Description of changes * Update the condition to generate the warning message in order to prevent false-positives when one of the special keywords is used. ### 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*
1 parent a21190e commit 97744e7

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/aws-cdk-lib/aws-cloudwatch/lib/metric.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ export class MathExpression implements IMetric {
707707
// we can add warnings.
708708
const missingIdentifiers = allIdentifiersInExpression(this.expression).filter(i => !this.usingMetrics[i]);
709709

710-
if (!this.expression.toUpperCase().match('\\s*INSIGHT_RULE_METRIC|SELECT|SEARCH|METRICS\\s.*') && missingIdentifiers.length > 0) {
710+
if (!this.expression.toUpperCase().match('\\b(INSIGHT_RULE_METRIC|SELECT|SEARCH|METRICS)\\b') && missingIdentifiers.length > 0) {
711711
warnings['CloudWatch:Math:UnknownIdentifier'] = `Math expression '${this.expression}' references unknown identifiers: ${missingIdentifiers.join(', ')}. Please add them to the 'usingMetrics' map.`;
712712
}
713713

packages/aws-cdk-lib/aws-cloudwatch/test/metric-math.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ describe('Metric Math', () => {
8080
expect(m.warningsV2).toBeUndefined();
8181
});
8282

83+
test('metrics METRICS expression with parameter does not produce warning for unknown identifier', () => {
84+
const m = new MathExpression({
85+
expression: 'SUM(METRICS("parameter"))',
86+
usingMetrics: {},
87+
});
88+
89+
expect(m.warningsV2).toBeUndefined();
90+
});
91+
8392
test('metrics search expression does not produce warning for unknown identifier', () => {
8493
const m = new MathExpression({
8594
expression: "SEARCH('{dimension_one, dimension_two} my_metric', 'Average', 300)",

0 commit comments

Comments
 (0)