Skip to content

Commit

Permalink
fix(cloudwatch): update regex expression that prevents CloudWatch:Mat…
Browse files Browse the repository at this point in the history
…h:UnknownIdentifier warnings (aws#33591)

Signed-off-by: Sergio García Prado <[email protected]>
  • Loading branch information
garciparedes committed Feb 26, 2025
1 parent 6df9bfe commit b3628d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-cloudwatch/lib/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ export class MathExpression implements IMetric {
// we can add warnings.
const missingIdentifiers = allIdentifiersInExpression(this.expression).filter(i => !this.usingMetrics[i]);

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

Expand Down
9 changes: 9 additions & 0 deletions packages/aws-cdk-lib/aws-cloudwatch/test/metric-math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ describe('Metric Math', () => {
expect(m.warningsV2).toBeUndefined();
});

test('metrics METRICS expression with parameters does not produce warning for unknown identifier', () => {
const m = new MathExpression({
expression: 'SUM(METRICS("partial"))',
usingMetrics: {},
});

expect(m.warningsV2).toBeUndefined();
});

test('metrics search expression does not produce warning for unknown identifier', () => {
const m = new MathExpression({
expression: "SEARCH('{dimension_one, dimension_two} my_metric', 'Average', 300)",
Expand Down

0 comments on commit b3628d0

Please sign in to comment.