From 5e14fbfbb0253a798c78f3c97c09bd5754840828 Mon Sep 17 00:00:00 2001 From: NetaNir Date: Mon, 22 Nov 2021 17:27:12 -0800 Subject: [PATCH 01/12] fix(cloudwatch): allow empty label im a math expression --- packages/@aws-cdk/aws-cloudwatch/lib/metric.ts | 2 +- packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts index 03d9e4fe5ca7d..8dbf63ad70cc5 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts @@ -129,7 +129,7 @@ export interface MathExpressionOptions { /** * Label for this metric when added to a Graph in a Dashboard * - * @default - Expression value is used as label + * @default - The legend will show only the original metric labels */ readonly label?: string; diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts b/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts index 9223695a96c67..c1ef49e480860 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts @@ -66,13 +66,6 @@ function metricGraphJson(metric: IMetric, yAxis?: string, id?: string) { if (yAxis !== 'left') { options.yAxis = yAxis; } if (id) { options.id = id; } - // If math expressions don't have a label (or an ID), they'll render with an unelegant - // autogenerated id ("metric_alias0"). Our ids may in the future also be autogenerated, - // so if an ME doesn't have a label, use its toString() as the label (renders the expression). - if (options.visible !== false && options.expression && !options.label) { - options.label = metric.toString(); - } - const renderedOpts = dropUndefined(options); if (Object.keys(renderedOpts).length !== 0) { From 885eecb4025cb9dde8852a20743bd5844a919d70 Mon Sep 17 00:00:00 2001 From: NetaNir Date: Mon, 22 Nov 2021 17:35:45 -0800 Subject: [PATCH 02/12] english --- packages/@aws-cdk/aws-cloudwatch/lib/metric.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts index 8dbf63ad70cc5..52fd1f3e6bacd 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts @@ -129,7 +129,7 @@ export interface MathExpressionOptions { /** * Label for this metric when added to a Graph in a Dashboard * - * @default - The legend will show only the original metric labels + * @default - The legend will only show the original metric labels */ readonly label?: string; From 461487efc7054a6270a9f3394ac8c9cb46d103b1 Mon Sep 17 00:00:00 2001 From: NetaNir Date: Mon, 22 Nov 2021 17:59:33 -0800 Subject: [PATCH 03/12] update tests --- .../aws-cloudwatch/test/metric-math.test.ts | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) 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 5f244f3b11352..0dfa4b66c47fe 100644 --- a/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts +++ b/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts @@ -83,7 +83,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + b', label: 'a + b' }], + [{ expression: 'a + b' }], ['Test', 'ACount', { visible: false, id: 'a' }], ['Test', 'BCount', { visible: false, id: 'b' }], ]); @@ -110,7 +110,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ label: 'a + e', expression: 'a + e' }], + [{ expression: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'b + c', visible: false, id: 'e' }], ['Test', 'BCount', { visible: false, id: 'b' }], @@ -137,7 +137,7 @@ describe('Metric Math', () => { }); graphMetricsAre(graph, [ - [{ label: 'a + e', expression: 'a + e' }], + [{ expression: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'b + c', visible: false, id: 'e' }], ['Test', 'ACount', { visible: false, id: 'b' }], @@ -165,7 +165,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ label: 'a + e', expression: 'a + e' }], + [{ expression: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'a + c', visible: false, id: 'e' }], ['Test', 'CCount', { visible: false, id: 'c' }], @@ -188,7 +188,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ ['Test', 'ACount', { id: 'a' }], - [{ label: 'a + b', expression: 'a + b' }], + [{ expression: 'a + b' }], ['Test', 'BCount', { visible: false, id: 'b' }], ]); @@ -210,7 +210,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ ['Test', 'ACount', { period: 10 }], - [{ label: 'a + b', expression: 'a + b' }], + [{ expression: 'a + b' }], ['Test', 'ACount', { visible: false, id: 'a' }], ['Test', 'BCount', { visible: false, id: 'b' }], ]); @@ -232,7 +232,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ ['Test', 'ACount'], - [{ label: 'a + b', expression: 'a + b', period: 60 }], + [{ expression: 'a + b', period: 60 }], ['Test', 'ACount', { visible: false, id: 'a', period: 60 }], ['Test', 'BCount', { visible: false, id: 'b', period: 60 }], ]); @@ -260,7 +260,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + e', label: 'a + e' }], + [{ expression: 'a + e'}], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'a + b', visible: false, id: 'e' }], ['Test', 'BCount', { visible: false, id: 'b' }], @@ -281,7 +281,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + b99', label: 'a + b99' }], + [{ expression: 'a + b99'}], ['Test', 'ACount', { visible: false, id: 'a' }], ['Test', 'BCount', { visible: false, id: 'b99', stat: 'p99' }], ]); @@ -308,9 +308,8 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ label: 'a + 1', expression: 'a + 1' }], ['Test', 'ACount', { visible: false, id: 'a' }], - [{ label: 'a + 2', expression: 'a + 2', yAxis: 'right' }], + [{ expression: 'a + 2', yAxis: 'right' }], ]); From 79d09829f036031c1556da99b6004f95fe8885d5 Mon Sep 17 00:00:00 2001 From: NetaNir Date: Mon, 22 Nov 2021 19:48:41 -0800 Subject: [PATCH 04/12] i never liked this linter anyway --- packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 0dfa4b66c47fe..744f8ce1a7ea2 100644 --- a/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts +++ b/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts @@ -260,7 +260,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + e'}], + [{ expression: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'a + b', visible: false, id: 'e' }], ['Test', 'BCount', { visible: false, id: 'b' }], @@ -281,7 +281,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + b99'}], + [{ expression: 'a + b99' }], ['Test', 'ACount', { visible: false, id: 'a' }], ['Test', 'BCount', { visible: false, id: 'b99', stat: 'p99' }], ]); From c2bf6c70683b619365e512dfa9a5507f330fd131 Mon Sep 17 00:00:00 2001 From: NetaNir Date: Sat, 27 Nov 2021 15:30:39 -0800 Subject: [PATCH 05/12] emoty string --- .../@aws-cdk/aws-cloudwatch/lib/metric.ts | 3 +- .../aws-cloudwatch/lib/private/rendering.ts | 11 +++++++- .../aws-cloudwatch/test/metric-math.test.ts | 28 +++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts index 52fd1f3e6bacd..2d99cb1b3e1b5 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts @@ -128,8 +128,9 @@ export interface MetricOptions extends CommonMetricOptions { export interface MathExpressionOptions { /** * Label for this metric when added to a Graph in a Dashboard + * To have the legend show the original metric labels only, pass an empty string, i.e. label: ''. * - * @default - The legend will only show the original metric labels + * @default - Expression value is used as label */ readonly label?: string; diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts b/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts index c1ef49e480860..538e8544cf487 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts @@ -66,6 +66,15 @@ function metricGraphJson(metric: IMetric, yAxis?: string, id?: string) { if (yAxis !== 'left') { options.yAxis = yAxis; } if (id) { options.id = id; } + if (options.visible !== false && options.expression && !options.label) { + if (options.label === undefined) { + options.label = metric.toString(); + } else { + // if an empty string was passed explicitly, set the label to undefined so CloudWatch will use the original metric legend + options.label = undefined; + } + } + const renderedOpts = dropUndefined(options); if (Object.keys(renderedOpts).length !== 0) { @@ -181,4 +190,4 @@ export class MetricSet { } } } -} +} \ No newline at end of file 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 5f244f3b11352..7faa83f6b99d8 100644 --- a/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts +++ b/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts @@ -145,6 +145,34 @@ describe('Metric Math', () => { ]); + }); + + test('passing an empty string as the label of a MathExpressions does not emit a label', () => { + const graph = new GraphWidget({ + left: [ + new MathExpression({ + expression: 'a + e', + label: '', + usingMetrics: { + a, + e: new MathExpression({ + expression: 'b + c', + usingMetrics: { b: a, c }, + }), + }, + }), + ], + }); + + graphMetricsAre(graph, [ + [{ expression: 'a + e' }], + ['Test', 'ACount', { visible: false, id: 'a' }], + [{ expression: 'b + c', visible: false, id: 'e' }], + ['Test', 'ACount', { visible: false, id: 'b' }], + ['Test', 'CCount', { visible: false, id: 'c' }], + ]); + + }); test('can reuse identifiers in MathExpressions if metrics are the same', () => { From 147c3994d64d122812ac58f0b31112da8fc50211 Mon Sep 17 00:00:00 2001 From: NetaNir Date: Sat, 27 Nov 2021 15:41:26 -0800 Subject: [PATCH 06/12] revert test --- .../aws-cloudwatch/test/metric-math.test.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) 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 695cb7f78a747..7faa83f6b99d8 100644 --- a/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts +++ b/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts @@ -83,7 +83,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + b' }], + [{ expression: 'a + b', label: 'a + b' }], ['Test', 'ACount', { visible: false, id: 'a' }], ['Test', 'BCount', { visible: false, id: 'b' }], ]); @@ -110,7 +110,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + e' }], + [{ label: 'a + e', expression: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'b + c', visible: false, id: 'e' }], ['Test', 'BCount', { visible: false, id: 'b' }], @@ -137,7 +137,7 @@ describe('Metric Math', () => { }); graphMetricsAre(graph, [ - [{ expression: 'a + e' }], + [{ label: 'a + e', expression: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'b + c', visible: false, id: 'e' }], ['Test', 'ACount', { visible: false, id: 'b' }], @@ -193,7 +193,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + e' }], + [{ label: 'a + e', expression: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'a + c', visible: false, id: 'e' }], ['Test', 'CCount', { visible: false, id: 'c' }], @@ -216,7 +216,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ ['Test', 'ACount', { id: 'a' }], - [{ expression: 'a + b' }], + [{ label: 'a + b', expression: 'a + b' }], ['Test', 'BCount', { visible: false, id: 'b' }], ]); @@ -238,7 +238,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ ['Test', 'ACount', { period: 10 }], - [{ expression: 'a + b' }], + [{ label: 'a + b', expression: 'a + b' }], ['Test', 'ACount', { visible: false, id: 'a' }], ['Test', 'BCount', { visible: false, id: 'b' }], ]); @@ -260,7 +260,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ ['Test', 'ACount'], - [{ expression: 'a + b', period: 60 }], + [{ label: 'a + b', expression: 'a + b', period: 60 }], ['Test', 'ACount', { visible: false, id: 'a', period: 60 }], ['Test', 'BCount', { visible: false, id: 'b', period: 60 }], ]); @@ -288,7 +288,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + e' }], + [{ expression: 'a + e', label: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], [{ expression: 'a + b', visible: false, id: 'e' }], ['Test', 'BCount', { visible: false, id: 'b' }], @@ -309,7 +309,7 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ - [{ expression: 'a + b99' }], + [{ expression: 'a + b99', label: 'a + b99' }], ['Test', 'ACount', { visible: false, id: 'a' }], ['Test', 'BCount', { visible: false, id: 'b99', stat: 'p99' }], ]); @@ -336,8 +336,9 @@ describe('Metric Math', () => { // THEN graphMetricsAre(graph, [ + [{ label: 'a + 1', expression: 'a + 1' }], ['Test', 'ACount', { visible: false, id: 'a' }], - [{ expression: 'a + 2', yAxis: 'right' }], + [{ label: 'a + 2', expression: 'a + 2', yAxis: 'right' }], ]); From b6f08565bfdffcdf3ee1d0c4549b092588744c90 Mon Sep 17 00:00:00 2001 From: NetaNir Date: Sat, 27 Nov 2021 15:52:47 -0800 Subject: [PATCH 07/12] scope test --- packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts | 2 +- packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts b/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts index 538e8544cf487..ec991af3333a3 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts @@ -190,4 +190,4 @@ export class MetricSet { } } } -} \ No newline at end of file +} 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 7faa83f6b99d8..d4220f83d1b50 100644 --- a/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts +++ b/packages/@aws-cdk/aws-cloudwatch/test/metric-math.test.ts @@ -155,10 +155,6 @@ describe('Metric Math', () => { label: '', usingMetrics: { a, - e: new MathExpression({ - expression: 'b + c', - usingMetrics: { b: a, c }, - }), }, }), ], @@ -167,9 +163,6 @@ describe('Metric Math', () => { graphMetricsAre(graph, [ [{ expression: 'a + e' }], ['Test', 'ACount', { visible: false, id: 'a' }], - [{ expression: 'b + c', visible: false, id: 'e' }], - ['Test', 'ACount', { visible: false, id: 'b' }], - ['Test', 'CCount', { visible: false, id: 'c' }], ]); From be3c016832a83a09b03076f3039a628e46b0d0b1 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 1 Apr 2022 16:37:09 +0200 Subject: [PATCH 08/12] Update docs --- .../@aws-cdk/aws-cloudwatch/lib/metric.ts | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts index 2d99cb1b3e1b5..28868b33d9883 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/metric.ts @@ -75,6 +75,18 @@ export interface CommonMetricOptions { /** * Label for this metric when added to a Graph in a Dashboard + * + * You can use [dynamic labels](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/graph-dynamic-labels.html) + * to show summary information about the entire displayed time series + * in the legend. For example, if you use: + * + * ``` + * [max: ${MAX}] MyMetric + * ``` + * + * As the metric label, the maximum value in the visible range will + * be shown next to the time series name in the graph's legend. + * * @default - No label */ readonly label?: string; @@ -127,8 +139,28 @@ export interface MetricOptions extends CommonMetricOptions { */ export interface MathExpressionOptions { /** - * Label for this metric when added to a Graph in a Dashboard - * To have the legend show the original metric labels only, pass an empty string, i.e. label: ''. + * Label for this expression when added to a Graph in a Dashboard + * + * If this expression evaluates to more than one time series (for + * example, through the use of `METRICS()` or `SEARCH()` expressions), + * each time series will appear in the graph using a combination of the + * expression label and the individual metric label. Specify the empty + * string (`''`) to suppress the expression label and only keep the + * metric label. + * + * You can use [dynamic labels](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/graph-dynamic-labels.html) + * to show summary information about the displayed time series + * in the legend. For example, if you use: + * + * ``` + * [max: ${MAX}] MyMetric + * ``` + * + * As the metric label, the maximum value in the visible range will + * be shown next to the time series name in the graph's legend. If the + * math expression produces more than one time series, the maximum + * will be shown for each individual time series produce by this + * math expression. * * @default - Expression value is used as label */ From 5d7c26c8fe5337c971954c9ce1ab43a73369af88 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 1 Apr 2022 17:09:07 +0200 Subject: [PATCH 09/12] Change implementation a bit --- .../aws-cloudwatch/lib/private/rendering.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts b/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts index ec991af3333a3..13a6440f8946c 100644 --- a/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts +++ b/packages/@aws-cdk/aws-cloudwatch/lib/private/rendering.ts @@ -67,12 +67,16 @@ function metricGraphJson(metric: IMetric, yAxis?: string, id?: string) { if (id) { options.id = id; } if (options.visible !== false && options.expression && !options.label) { - if (options.label === undefined) { - options.label = metric.toString(); - } else { - // if an empty string was passed explicitly, set the label to undefined so CloudWatch will use the original metric legend - options.label = undefined; - } + // Label may be '' or undefined. + // + // If undefined, we'll render the expression as the label, to suppress + // the default behavior of CW where it would render the metric + // id as label, which we (inelegantly) generate to be something like "metric_alias0". + // + // For array expressions (returning more than 1 TS) users may sometimes want to + // suppress the label completely. For those cases, we'll accept the empty string, + // and not render a label at all. + options.label = options.label === '' ? undefined : metric.toString(); } const renderedOpts = dropUndefined(options); From 65269f8bde66e4695b571f426ded07bfdf39efd0 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Fri, 1 Apr 2022 17:18:17 +0200 Subject: [PATCH 10/12] Also explain dynamic labels in the README --- packages/@aws-cdk/aws-cloudwatch/README.md | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/packages/@aws-cdk/aws-cloudwatch/README.md b/packages/@aws-cdk/aws-cloudwatch/README.md index 8404c6ce9ad7a..b3804215ade4b 100644 --- a/packages/@aws-cdk/aws-cloudwatch/README.md +++ b/packages/@aws-cdk/aws-cloudwatch/README.md @@ -105,6 +105,12 @@ graph showing the Average statistic with an aggregation period of 5 minutes: ```ts const cpuUtilization = new cloudwatch.MathExpression({ expression: "SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)" + + // Specifying '' as the label suppresses the default behavior + // of using the expression as metric label. This is especially appropriate + // when using expressions that return multiple time series (like SEARCH() + // or METRICS()), to show the labels of the retrieved metrics only. + label: '', }); ``` @@ -157,6 +163,31 @@ useful when embedding them in graphs, see below). > happen to know the Metric you want to alarm on makes sense as a rate > (`Average`) you can always choose to change the statistic. +### Labels + +Metric labels are displayed in the legend of graphs that include the metrics. + +You can use [dynamic labels](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/graph-dynamic-labels.html) +to show summary information about the displayed time series +in the legend. For example, if you use: + +```ts +const minuteErrorRate = fn.metricErrors({ + statistic: 'sum', + period: Duration.hours(1), + + // Show the maximum hourly error count in the legend + label: '[max: ${MAX}] Lambda failure rate' +}); +``` + +As the metric label, the maximum value in the visible range will +be shown next to the time series name in the graph's legend. + +If the metric is a math expression producing more than one time series, the +maximum will be individually calculated and shown for each time series produce +by the math expression. + ## Alarms Alarms can be created on metrics in one of two ways. Either create an `Alarm` From 598b6a878a7dbc2679ea8f54e9fc91ce339bc5b5 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Sat, 2 Apr 2022 18:27:14 +0200 Subject: [PATCH 11/12] Fix compilation of example --- packages/@aws-cdk/aws-cloudwatch/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@aws-cdk/aws-cloudwatch/README.md b/packages/@aws-cdk/aws-cloudwatch/README.md index b3804215ade4b..79b0a25e9bf45 100644 --- a/packages/@aws-cdk/aws-cloudwatch/README.md +++ b/packages/@aws-cdk/aws-cloudwatch/README.md @@ -104,7 +104,7 @@ graph showing the Average statistic with an aggregation period of 5 minutes: ```ts const cpuUtilization = new cloudwatch.MathExpression({ - expression: "SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)" + expression: "SEARCH('{AWS/EC2,InstanceId} MetricName=\"CPUUtilization\"', 'Average', 300)", // Specifying '' as the label suppresses the default behavior // of using the expression as metric label. This is especially appropriate @@ -177,7 +177,7 @@ const minuteErrorRate = fn.metricErrors({ period: Duration.hours(1), // Show the maximum hourly error count in the legend - label: '[max: ${MAX}] Lambda failure rate' + label: '[max: ${MAX}] Lambda failure rate', }); ``` @@ -339,7 +339,7 @@ dashboard.addWidgets(new cloudwatch.GraphWidget({ right: [errorCountMetric.with({ statistic: "average", label: "Error rate", - color: cloudwatch.Color.GREEN + color: cloudwatch.Color.GREEN, })] })); ``` From 5d104f7198c405d2bd14545f66cf5ba0496492c8 Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Sun, 3 Apr 2022 11:22:29 +0200 Subject: [PATCH 12/12] Fix another example compilation failure --- packages/@aws-cdk/aws-cloudwatch/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/@aws-cdk/aws-cloudwatch/README.md b/packages/@aws-cdk/aws-cloudwatch/README.md index 79b0a25e9bf45..af3aaf7c5a765 100644 --- a/packages/@aws-cdk/aws-cloudwatch/README.md +++ b/packages/@aws-cdk/aws-cloudwatch/README.md @@ -172,6 +172,8 @@ to show summary information about the displayed time series in the legend. For example, if you use: ```ts +declare const fn: lambda.Function; + const minuteErrorRate = fn.metricErrors({ statistic: 'sum', period: Duration.hours(1),