Repository navigation
Commit c29dc17
authored
fix(cloudwatch): remove false positive warning for CDK tokens in MathExpression (#36882)
### Issue # (if applicable)
Closes #34977.
### Reason for this change
When using CDK tokens (from custom resources or other dynamic values) in CloudWatch MathExpression, users receive a false positive warning message. The `allIdentifiersInExpression` function incorrectly parses token strings like `${Token[TOKEN.81]}` and extracts "oken" as an identifier, triggering a confusing warning that suggests adding "oken" to the `usingMetrics` map.
This degrades user experience and creates confusion, particularly for users leveraging custom resources or dynamic values in CloudWatch dashboards.
### Description of changes
Modified the `allIdentifiersInExpression` function in `packages/aws-cdk-lib/aws-cloudwatch/lib/metric.ts` to filter out CDK token patterns before extracting metric identifiers:
- Added regex replacement to remove CDK token patterns (`${Token[TOKEN.N]}`) from expressions before identifier extraction
- Token pattern regex: `/\$\{Token\[[^\]]+\]\}/g` matches the standard CDK token format
- Preserves existing validation behavior for actual unknown identifiers
- Added 4 comprehensive unit tests covering single tokens, multiple tokens, mixed expressions, and complex patterns
**Technical Details:**
```typescript
// Before: Incorrectly extracted "oken" from "${Token[TOKEN.81]}"
function allIdentifiersInExpression(x: string) {
return Array.from(matchAll(x, FIND_VARIABLE)).map(m => m[0]);
}
// After: Filters out tokens first, then extracts identifiers
function allIdentifiersInExpression(x: string) {
// Remove CDK token patterns before extracting identifiers
// Token format: ${Token[TOKEN.123]} or ${Token[TOKEN.456]}
const withoutTokens = x.replace(/\$\{Token\[[^\]]+\]\}/g, '');
return Array.from(matchAll(withoutTokens, FIND_VARIABLE)).map(m => m[0]);
}
```
This approach follows the same pattern used for special functions (SEARCH, METRICS, SELECT, INSIGHT_RULE_METRIC) where certain syntax patterns are filtered or handled specially before validation.
**No breaking changes** - This fix only removes false positive warnings and does not affect CloudFormation template generation, resource behavior, or public APIs.
### Describe any new or updated permissions being added
N/A - No IAM permissions or resource access changes. This is a validation-only fix that does not affect CloudFormation templates or runtime behavior.
### Description of how you validated changes
- **Unit tests**: Added 4 new unit tests in `packages/aws-cdk-lib/aws-cloudwatch/test/metric-math.test.ts`:
1. Verified single token in expression doesn't produce warning
2. Verified multiple tokens in expression don't produce warning
3. Verified mixed token + real identifier warns only about the real identifier (not "oken")
4. Verified complex token patterns (arrays, functions) don't produce warning
- **Regression testing**: All 40 existing unit tests pass without modification, confirming:
- Warnings for actual unknown identifiers still work correctly
- Special function handling (METRICS, SEARCH, etc.) unchanged
- Nested expression validation preserved
- No impact on other CloudWatch functionality
- **Build validation**:
- Linting passed with no violations (`yarn lint --fix`)
- TypeScript compilation successful
- No JSII compatibility issues
### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
[comment]: <> (cdk-contribution-power preview)1 parent dedf99b commit c29dc17
2 files changed
Lines changed: 46 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1140 | 1140 | | |
1141 | 1141 | | |
1142 | 1142 | | |
1143 | | - | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
1144 | 1147 | | |
1145 | 1148 | | |
1146 | 1149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
113 | 155 | | |
114 | 156 | | |
115 | 157 | | |
| |||
0 commit comments