Skip to content

Commit 7eedb54

Browse files
authored
fix(cloudwatch): Math:UnknownIdentifier warning for INSIGHT_RULE_METRIC (#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*
1 parent 17826b4 commit 7eedb54

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
@@ -625,7 +625,7 @@ export class MathExpression implements IMetric {
625625
const missingIdentifiers = allIdentifiersInExpression(this.expression).filter(i => !this.usingMetrics[i]);
626626

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

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

+9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ describe('Metric Math', () => {
100100
expect(m.warningsV2).toBeUndefined();
101101
});
102102

103+
test('metrics INSIGHT_RULE_METRIC expression does not produce warning for unknown identifier', () => {
104+
const m = new MathExpression({
105+
expression: "INSIGHT_RULE_METRIC('RejectedConnectionsRule', 'Sum')",
106+
usingMetrics: {},
107+
});
108+
109+
expect(m.warningsV2).toBeUndefined();
110+
});
111+
103112
test('math expression referring to unknown expressions produces a warning, even when nested', () => {
104113
const m = new MathExpression({
105114
expression: 'e1 + 5',

0 commit comments

Comments
 (0)