(aws-cloudwatch): support the visible property on MathExpression
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 74
Description
### Describe the feature
`Metric` supports a `visible` property (added in #19876) that controls whether the metric is shown or hidden in dashboard graphs, but `MathExpression` does not. Both are rendered the same way in the CloudWatch console UI and can be toggled between visible and hidden there, so the CDK API should support `visible` on both.
### Use Case
When building dashboards, it is common to plot a math expression while hiding it by default (for example, an auxiliary band or intermediate expression that users can toggle on when investigating). Today there is no clean way to hide a top-level `MathExpression` on a graph:
- `MathExpressionOptions` has no `visible` property.
- `MathExpression.toMetricConfig()` only passes `label` and `color` in `renderingProperties`.
The rendering layer already sets `visible: false` on sub-metrics that are not top-level, so only top-level expressions are affected. Working around this requires escape-hatching the widget JSON.
### Proposed Solution
Mirror the existing `Metric` implementation:
1. Add `readonly visible?: boolean` to `MathExpressionOptions` (same JSDoc and `@default true` as `CommonMetricOptions.visible`).
2. Store it on the `MathExpression` class and assign it in the constructor.
3. Handle it in `with()` (short-circuit check and pass-through), matching how `label` and `color` are handled.
4. Include `visible: this.visible` in `renderingProperties` in `toMetricConfig()`.
This is backwards compatible: when unset, the property is `undefined` and no `visible` key is rendered, so existing templates are unchanged. Note that `AnomalyDetectionMetricOptions` extends `MathExpressionOptions`, so it would inherit the new property as well.
I have a working implementation with unit tests and will raise a PR for this.
### Other Information
The `visible` and `id` properties were added to `Metric` via #19876, but `MathExpressionOptions` was never updated to match. This issue proposes `visible` only; `id` could be a follow-up.
### Acknowledgements
- [x] I may be able to implement this feature request
- [ ] This feature might incur a breaking change
### CDK version used
2.x (latest)
### Environment details (OS name and version, etc.)
macOS
Contributor guide
Research direction
Review the existing Metric implementation, then inspect MathExpressionOptions, MathExpression.with(), and MathExpression.toMetricConfig(). Run the relevant unit tests and add coverage for visible behavior, including unset and false values; done means top-level MathExpression rendering preserves existing templates when unset and hides expressions when requested.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, observability-sre
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100