Skip to content

Commit 55108b9

Browse files
authored
fix(cloudwatch): math expressions incorrectly warn about metricsinsights variable names (#23316)
It is intended that all metric identifiers referenced in a MathExpression are included in the usingMetrics map and we will raise warnings if the customer does not follow this contract. However for Metricsinsights queries, we can refer directly to metrics attribute values inside the query. Therefore we do not raise warnings for Metricsinsights queries for not referencing metrics ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6a28b7f commit 55108b9

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ export class MathExpression implements IMetric {
589589

590590
const warnings = [];
591591

592-
if (missingIdentifiers.length > 0) {
592+
if (!this.expression.toUpperCase().match('\\s*SELECT\\s.*') && missingIdentifiers.length > 0) {
593593
warnings.push(`Math expression '${this.expression}' references unknown identifiers: ${missingIdentifiers.join(', ')}. Please add them to the 'usingMetrics' map.`);
594594
}
595595

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

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ describe('Metric Math', () => {
7575
expect(m.warnings).toContainEqual(expect.stringContaining("'m1 + m2' references unknown identifiers"));
7676
});
7777

78+
test('metrics insights expression does not produce warning for unknown identifier', () => {
79+
const m = new MathExpression({
80+
expression: "SELECT AVG(CpuUsage) FROM EC2 WHERE Instance = '123456'",
81+
});
82+
83+
expect(m.warnings).toBeUndefined();
84+
});
85+
7886
test('math expression referring to unknown expressions produces a warning, even when nested', () => {
7987
const m = new MathExpression({
8088
expression: 'e1 + 5',

0 commit comments

Comments
 (0)