Skip to content

Commit 7fa3bf2

Browse files
authored
fix(cloudwatch): automatic metric math label cannot be suppressed (#17639)
According to CloudWatch [docs](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html): > For the Label column of the expression, enter a name that describes what the expression is calculating. If the result of an expression is an array of time series, each of those time series is displayed on the graph with a separate line, with different colors. Immediately under the graph is a legend for each line in the graph. For a single expression that produces multiple time series, the legend captions for those time series are in the format Expression-Label Metric-Label. For example, if the graph includes a metric with a label of Errors and an expression FILL(METRICS(), 0) that has a label of Filled With 0:, one line in the legend would be Filled With 0: Errors. **To have the legend show only the original metric labels, set Expression-Label to be empty.** In the current implementation, if the label is left empty, the expression string is used, which makes the graph cumbersome. In multi widget dashboards where real estate is scarce, it becomes a real issue. See my cats widget before the fix: <img width="1432" alt="Screen Shot 2021-11-22 at 5 17 35 PM" src="https://user-images.githubusercontent.com/8578043/142959081-f3c28ea9-dd36-431f-b123-262eed0b2625.png"> My cats widget after the fix: <img width="1250" alt="Screen Shot 2021-11-22 at 5 20 34 PM" src="https://user-images.githubusercontent.com/8578043/142959125-2ea5f7b3-9170-43bc-ba8b-233dc1af9700.png"> ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 2df6dab commit 7fa3bf2

File tree

4 files changed

+100
-7
lines changed

4 files changed

+100
-7
lines changed

Diff for: packages/@aws-cdk/aws-cloudwatch/README.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ graph showing the Average statistic with an aggregation period of 5 minutes:
104104

105105
```ts
106106
const cpuUtilization = new cloudwatch.MathExpression({
107-
expression: "SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)"
107+
expression: "SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)",
108+
109+
// Specifying '' as the label suppresses the default behavior
110+
// of using the expression as metric label. This is especially appropriate
111+
// when using expressions that return multiple time series (like SEARCH()
112+
// or METRICS()), to show the labels of the retrieved metrics only.
113+
label: '',
108114
});
109115
```
110116

@@ -157,6 +163,33 @@ useful when embedding them in graphs, see below).
157163
> happen to know the Metric you want to alarm on makes sense as a rate
158164
> (`Average`) you can always choose to change the statistic.
159165
166+
### Labels
167+
168+
Metric labels are displayed in the legend of graphs that include the metrics.
169+
170+
You can use [dynamic labels](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/graph-dynamic-labels.html)
171+
to show summary information about the displayed time series
172+
in the legend. For example, if you use:
173+
174+
```ts
175+
declare const fn: lambda.Function;
176+
177+
const minuteErrorRate = fn.metricErrors({
178+
statistic: 'sum',
179+
period: Duration.hours(1),
180+
181+
// Show the maximum hourly error count in the legend
182+
label: '[max: ${MAX}] Lambda failure rate',
183+
});
184+
```
185+
186+
As the metric label, the maximum value in the visible range will
187+
be shown next to the time series name in the graph's legend.
188+
189+
If the metric is a math expression producing more than one time series, the
190+
maximum will be individually calculated and shown for each time series produce
191+
by the math expression.
192+
160193
## Alarms
161194

162195
Alarms can be created on metrics in one of two ways. Either create an `Alarm`
@@ -308,7 +341,7 @@ dashboard.addWidgets(new cloudwatch.GraphWidget({
308341
right: [errorCountMetric.with({
309342
statistic: "average",
310343
label: "Error rate",
311-
color: cloudwatch.Color.GREEN
344+
color: cloudwatch.Color.GREEN,
312345
})]
313346
}));
314347
```

Diff for: packages/@aws-cdk/aws-cloudwatch/lib/metric.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ export interface CommonMetricOptions {
7575

7676
/**
7777
* Label for this metric when added to a Graph in a Dashboard
78+
*
79+
* You can use [dynamic labels](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/graph-dynamic-labels.html)
80+
* to show summary information about the entire displayed time series
81+
* in the legend. For example, if you use:
82+
*
83+
* ```
84+
* [max: ${MAX}] MyMetric
85+
* ```
86+
*
87+
* As the metric label, the maximum value in the visible range will
88+
* be shown next to the time series name in the graph's legend.
89+
*
7890
* @default - No label
7991
*/
8092
readonly label?: string;
@@ -127,7 +139,28 @@ export interface MetricOptions extends CommonMetricOptions {
127139
*/
128140
export interface MathExpressionOptions {
129141
/**
130-
* Label for this metric when added to a Graph in a Dashboard
142+
* Label for this expression when added to a Graph in a Dashboard
143+
*
144+
* If this expression evaluates to more than one time series (for
145+
* example, through the use of `METRICS()` or `SEARCH()` expressions),
146+
* each time series will appear in the graph using a combination of the
147+
* expression label and the individual metric label. Specify the empty
148+
* string (`''`) to suppress the expression label and only keep the
149+
* metric label.
150+
*
151+
* You can use [dynamic labels](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/graph-dynamic-labels.html)
152+
* to show summary information about the displayed time series
153+
* in the legend. For example, if you use:
154+
*
155+
* ```
156+
* [max: ${MAX}] MyMetric
157+
* ```
158+
*
159+
* As the metric label, the maximum value in the visible range will
160+
* be shown next to the time series name in the graph's legend. If the
161+
* math expression produces more than one time series, the maximum
162+
* will be shown for each individual time series produce by this
163+
* math expression.
131164
*
132165
* @default - Expression value is used as label
133166
*/

Diff for: packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,17 @@ function metricGraphJson(metric: IMetric, yAxis?: string, id?: string) {
6666
if (yAxis !== 'left') { options.yAxis = yAxis; }
6767
if (id) { options.id = id; }
6868

69-
// If math expressions don't have a label (or an ID), they'll render with an unelegant
70-
// autogenerated id ("metric_alias0"). Our ids may in the future also be autogenerated,
71-
// so if an ME doesn't have a label, use its toString() as the label (renders the expression).
7269
if (options.visible !== false && options.expression && !options.label) {
73-
options.label = metric.toString();
70+
// Label may be '' or undefined.
71+
//
72+
// If undefined, we'll render the expression as the label, to suppress
73+
// the default behavior of CW where it would render the metric
74+
// id as label, which we (inelegantly) generate to be something like "metric_alias0".
75+
//
76+
// For array expressions (returning more than 1 TS) users may sometimes want to
77+
// suppress the label completely. For those cases, we'll accept the empty string,
78+
// and not render a label at all.
79+
options.label = options.label === '' ? undefined : metric.toString();
7480
}
7581

7682
const renderedOpts = dropUndefined(options);

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

+21
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,27 @@ describe('Metric Math', () => {
145145
]);
146146

147147

148+
});
149+
150+
test('passing an empty string as the label of a MathExpressions does not emit a label', () => {
151+
const graph = new GraphWidget({
152+
left: [
153+
new MathExpression({
154+
expression: 'a + e',
155+
label: '',
156+
usingMetrics: {
157+
a,
158+
},
159+
}),
160+
],
161+
});
162+
163+
graphMetricsAre(graph, [
164+
[{ expression: 'a + e' }],
165+
['Test', 'ACount', { visible: false, id: 'a' }],
166+
]);
167+
168+
148169
});
149170

150171
test('can reuse identifiers in MathExpressions if metrics are the same', () => {

0 commit comments

Comments
 (0)