From 55108b969a94f671a492b4536d2ad9d13d11cf9d Mon Sep 17 00:00:00 2001 From: caoxy98 <120102643+caoxy98@users.noreply.github.com> Date: Mon, 12 Dec 2022 16:26:07 +0000 Subject: [PATCH] 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* --- packages/@aws-cdk/aws-cloudwatch/lib/metric.ts | 2 +- packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts index f3f9400712f02..f2a8093e27685 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts @@ -589,7 +589,7 @@ export class MathExpression implements IMetric { const warnings = []; - if (missingIdentifiers.length > 0) { + if (!this.expression.toUpperCase().match('\\s*SELECT\\s.*') && missingIdentifiers.length > 0) { warnings.push(`Math expression '${this.expression}' references unknown identifiers: ${missingIdentifiers.join(', ')}. Please add them to the 'usingMetrics' map.`); } diff --git a/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts b/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts index 480a25e3d748e..72e61cbd0a19a 100644 --- a/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts +++ b/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts @@ -75,6 +75,14 @@ describe('Metric Math', () => { expect(m.warnings).toContainEqual(expect.stringContaining("'m1 + m2' references unknown identifiers")); }); + test('metrics insights expression does not produce warning for unknown identifier', () => { + const m = new MathExpression({ + expression: "SELECT AVG(CpuUsage) FROM EC2 WHERE Instance = '123456'", + }); + + expect(m.warnings).toBeUndefined(); + }); + test('math expression referring to unknown expressions produces a warning, even when nested', () => { const m = new MathExpression({ expression: 'e1 + 5',