Skip to content

Commit

Permalink
fix(cloudwatch): Math:UnknownIdentifier warning for INSIGHT_RULE_METR…
Browse files Browse the repository at this point in the history
…IC (#28870)

The CloudWatch `MathExpression` class warns about identifiers missing from `usingMetrics` when `INSIGHT_RULE_METRIC` is used in the expression. It incorrectly parses the arguments to `INSIGHT_RULE_METRIC` as identifiers. When using `INSIGHT_RULE_METRIC`, I don't believe there is anything that needs to be added to `usingMetrics`.

This implementation follows a similar fix done for some other expressions here: #24313

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
aws-sde committed Feb 6, 2024
1 parent 17826b4 commit 7eedb54
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 @@ -625,7 +625,7 @@ export class MathExpression implements IMetric {
const missingIdentifiers = allIdentifiersInExpression(this.expression).filter(i => !this.usingMetrics[i]);

const warnings: { [id: string]: string } = {};
if (!this.expression.toUpperCase().match('\\s*SELECT|SEARCH|METRICS\\s.*') && missingIdentifiers.length > 0) {
if (!this.expression.toUpperCase().match('\\s*INSIGHT_RULE_METRIC|SELECT|SEARCH|METRICS\\s.*') && 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 @@ -100,6 +100,15 @@ describe('Metric Math', () => {
expect(m.warningsV2).toBeUndefined();
});

test('metrics INSIGHT_RULE_METRIC expression does not produce warning for unknown identifier', () => {
const m = new MathExpression({
expression: "INSIGHT_RULE_METRIC('RejectedConnectionsRule', 'Sum')",
usingMetrics: {},
});

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

test('math expression referring to unknown expressions produces a warning, even when nested', () => {
const m = new MathExpression({
expression: 'e1 + 5',
Expand Down

0 comments on commit 7eedb54

Please sign in to comment.