diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts index 5068187f9b9ae..770c3604c3c5a 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts @@ -609,9 +609,9 @@ export class MathExpression implements IMetric { // we can add warnings. const missingIdentifiers = allIdentifiersInExpression(this.expression).filter(i => !this.usingMetrics[i]); - const warnings = []; + const warnings: string[] = []; - if (!this.expression.toUpperCase().match('\\s*SELECT\\s.*') && missingIdentifiers.length > 0) { + if (!this.expression.toUpperCase().match('\\s*SELECT|SEARCH|METRICS\\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 72e61cbd0a19a..24ffebfcfb76f 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,24 @@ describe('Metric Math', () => { expect(m.warnings).toContainEqual(expect.stringContaining("'m1 + m2' references unknown identifiers")); }); + test('metrics METRICS expression does not produce warning for unknown identifier', () => { + const m = new MathExpression({ + expression: 'SUM(METRICS())', + usingMetrics: {}, + }); + + expect(m.warnings).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)", + usingMetrics: {}, + }); + + expect(m.warnings).toBeUndefined(); + }); + test('metrics insights expression does not produce warning for unknown identifier', () => { const m = new MathExpression({ expression: "SELECT AVG(CpuUsage) FROM EC2 WHERE Instance = '123456'",