Skip to content

Commit

Permalink
fix(cloudwatch): metric label not rendered into Alarms (#13070)
Browse files Browse the repository at this point in the history
Cloudwatch allows adding labels to metrics. These can be seen when viewing alarms.
Currently labels only work for math expression metrics.
This change will fix it to work for single alarm metrics too.


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jonnekaunisto authored Mar 3, 2021
1 parent 6f7cebd commit cbcc712
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 12 deletions.
46 changes: 34 additions & 12 deletions packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,40 @@ export class Alarm extends AlarmBase {
private renderMetric(metric: IMetric) {
const self = this;
return dispatchMetric(metric, {
withStat(st) {
self.validateMetricStat(st, metric);

return dropUndefined({
dimensions: st.dimensions,
namespace: st.namespace,
metricName: st.metricName,
period: st.period?.toSeconds(),
statistic: renderIfSimpleStatistic(st.statistic),
extendedStatistic: renderIfExtendedStatistic(st.statistic),
unit: st.unitFilter,
});
withStat(stat, conf) {
self.validateMetricStat(stat, metric);

if (conf.renderingProperties?.label == undefined) {
return dropUndefined({
dimensions: stat.dimensions,
namespace: stat.namespace,
metricName: stat.metricName,
period: stat.period?.toSeconds(),
statistic: renderIfSimpleStatistic(stat.statistic),
extendedStatistic: renderIfExtendedStatistic(stat.statistic),
unit: stat.unitFilter,
});
}

return {
metrics: [
{
metricStat: {
metric: {
metricName: stat.metricName,
namespace: stat.namespace,
dimensions: stat.dimensions,
},
period: stat.period.toSeconds(),
stat: stat.statistic,
unit: stat.unitFilter,
},
id: stat.metricName,

This comment has been minimized.

Copy link
@robo-sk

robo-sk Mar 8, 2021

Is this OK?

Some of the metric names start with number (4xx, 5xx for api gateway), but the ID has following limitation: "The first character must be a lowercase letter."

label: conf.renderingProperties?.label,
returnData: true,
} as CfnAlarm.MetricDataQueryProperty,
],
};
},

withExpression() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"Resources": {
"Alarm1F9009D71": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"Metrics": [
{
"Id": "Metric",
"Label": "Metric [AVG: ${AVG}]",
"MetricStat": {
"Metric": {
"MetricName": "Metric",
"Namespace": "CDK/Test"
},
"Period": 300,
"Stat": "Average"
},
"ReturnData": true
}
],
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"EvaluationPeriods": 3,
"Threshold": 100
}
},
"Alarm2A7122E13": {
"Type": "AWS::CloudWatch::Alarm",
"Properties": {
"Metrics": [
{
"Id": "Metric",
"Label": "Metric [AVG: ${AVG}]",
"MetricStat": {
"Metric": {
"MetricName": "Metric",
"Namespace": "CDK/Test"
},
"Period": 300,
"Stat": "Average"
},
"ReturnData": true
}
],
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"EvaluationPeriods": 3,
"Threshold": 100
}
}
}
}
32 changes: 32 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/test/integ.alarm-with-label.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { App, Stack, StackProps } from '@aws-cdk/core';
import { Alarm, Metric } from '../lib';

class AlarmWithLabelIntegrationTest extends Stack {

constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);

const testMetric = new Metric({
namespace: 'CDK/Test',
metricName: 'Metric',
label: 'Metric [AVG: ${AVG}]',
});

new Alarm(this, 'Alarm1', {
metric: testMetric,
threshold: 100,
evaluationPeriods: 3,
});

testMetric.createAlarm(this, 'Alarm2', {
threshold: 100,
evaluationPeriods: 3,
});
}
}

const app = new App();

new AlarmWithLabelIntegrationTest(app, 'AlarmWithLabelIntegrationTest');

app.synth();

0 comments on commit cbcc712

Please sign in to comment.