Skip to content

Commit 828ac20

Browse files
authored
feat(cloudwatch): Support 'datapointsToAlarm' on Alarms (#1631)
Adds support for settingt the `dataPointsToAlarm` property of the CloudWatch `Alarm`s (also via the `Metric`s). Fixes #1626
1 parent 5b2de2b commit 828ac20

File tree

5 files changed

+53
-64
lines changed

5 files changed

+53
-64
lines changed

packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts

+3-55
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,20 @@
11
import { Construct, Token } from '@aws-cdk/cdk';
22
import { CfnAlarm } from './cloudwatch.generated';
33
import { HorizontalAnnotation } from './graph';
4-
import { Dimension, Metric, Statistic, Unit } from './metric';
4+
import { Dimension, Metric, MetricAarmProps, Statistic, Unit } from './metric';
55
import { parseStatistic } from './util.statistic';
66

77
/**
88
* Properties for Alarms
99
*/
10-
export interface AlarmProps {
10+
export interface AlarmProps extends MetricAarmProps {
1111
/**
1212
* The metric to add the alarm on
1313
*
1414
* Metric objects can be obtained from most resources, or you can construct
1515
* custom Metric objects by instantiating one.
1616
*/
1717
metric: Metric;
18-
19-
/**
20-
* Name of the alarm
21-
*
22-
* @default Automatically generated name
23-
*/
24-
alarmName?: string;
25-
26-
/**
27-
* Description for the alarm
28-
*
29-
* @default No description
30-
*/
31-
alarmDescription?: string;
32-
33-
/**
34-
* Comparison to use to check if metric is breaching
35-
*
36-
* @default GreaterThanOrEqualToThreshold
37-
*/
38-
comparisonOperator?: ComparisonOperator;
39-
40-
/**
41-
* The value against which the specified statistic is compared.
42-
*/
43-
threshold: number;
44-
45-
/**
46-
* The number of periods over which data is compared to the specified threshold.
47-
*/
48-
evaluationPeriods: number;
49-
50-
/**
51-
* Specifies whether to evaluate the data and potentially change the alarm
52-
* state if there are too few data points to be statistically significant.
53-
*
54-
* Used only for alarms that are based on percentiles.
55-
*/
56-
evaluateLowSampleCountPercentile?: string;
57-
58-
/**
59-
* Sets how this alarm is to handle missing data points.
60-
*
61-
* @default TreatMissingData.Missing
62-
*/
63-
treatMissingData?: TreatMissingData;
64-
65-
/**
66-
* Whether the actions for this alarm are enabled
67-
*
68-
* @default true
69-
*/
70-
actionsEnabled?: boolean;
7118
}
7219

7320
/**
@@ -153,6 +100,7 @@ export class Alarm extends Construct {
153100
// Evaluation
154101
comparisonOperator,
155102
threshold: props.threshold,
103+
datapointsToAlarm: props.datapointsToAlarm,
156104
evaluateLowSampleCountPercentile: props.evaluateLowSampleCountPercentile,
157105
evaluationPeriods: props.evaluationPeriods,
158106
treatMissingData: props.treatMissingData,

packages/@aws-cdk/aws-cloudwatch/lib/metric.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class Metric {
148148
* Combines both properties that may adjust the metric (aggregation) as well
149149
* as alarm properties.
150150
*/
151-
public newAlarm(scope: cdk.Construct, id: string, props: NewAlarmProps): Alarm {
151+
public newAlarm(scope: cdk.Construct, id: string, props: MetricAarmProps): Alarm {
152152
return new Alarm(scope, id, {
153153
metric: this.with({
154154
statistic: props.statistic,
@@ -157,6 +157,7 @@ export class Metric {
157157
alarmName: props.alarmName,
158158
alarmDescription: props.alarmDescription,
159159
comparisonOperator: props.comparisonOperator,
160+
datapointsToAlarm: props.datapointsToAlarm,
160161
threshold: props.threshold,
161162
evaluationPeriods: props.evaluationPeriods,
162163
evaluateLowSampleCountPercentile: props.evaluateLowSampleCountPercentile,
@@ -293,9 +294,9 @@ export interface MetricCustomization {
293294
}
294295

295296
/**
296-
* Properties to make an alarm from a metric
297+
* Properties needed to make an alarm from a metric
297298
*/
298-
export interface NewAlarmProps {
299+
export interface MetricAarmProps {
299300
/**
300301
* The period over which the specified statistic is applied.
301302
*
@@ -372,6 +373,17 @@ export interface NewAlarmProps {
372373
* @default true
373374
*/
374375
actionsEnabled?: boolean;
376+
377+
/**
378+
* The number of datapoints that must be breaching to trigger the alarm. This is used only if you are setting an "M
379+
* out of N" alarm. In that case, this value is the M. For more information, see Evaluating an Alarm in the Amazon
380+
* CloudWatch User Guide.
381+
*
382+
* @default ``evaluationPeriods``
383+
*
384+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html#alarm-evaluation
385+
*/
386+
datapointsToAlarm?: number;
375387
}
376388

377389
function ifUndefined<T>(x: T | undefined, def: T | undefined): T | undefined {

packages/@aws-cdk/aws-cloudwatch/test/integ.alarm-and-dashboard.expected.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
"Properties": {
99
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
1010
"EvaluationPeriods": 3,
11-
"MetricName": "ApproximateNumberOfMessagesVisible",
12-
"Namespace": "AWS/SQS",
13-
"Period": 300,
1411
"Threshold": 100,
12+
"DatapointsToAlarm": 2,
1513
"Dimensions": [
1614
{
1715
"Name": "QueueName",
@@ -23,6 +21,9 @@
2321
}
2422
}
2523
],
24+
"MetricName": "ApproximateNumberOfMessagesVisible",
25+
"Namespace": "AWS/SQS",
26+
"Period": 300,
2627
"Statistic": "Average"
2728
}
2829
},

packages/@aws-cdk/aws-cloudwatch/test/integ.alarm-and-dashboard.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const metric = new cloudwatch.Metric({
2121

2222
const alarm = metric.newAlarm(stack, 'Alarm', {
2323
threshold: 100,
24-
evaluationPeriods: 3
24+
evaluationPeriods: 3,
25+
datapointsToAlarm: 2,
2526
});
2627

2728
const dashboard = new cloudwatch.Dashboard(stack, 'Dash');

packages/@aws-cdk/aws-cloudwatch/test/test.alarm.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,40 @@ export = {
1717
new Alarm(stack, 'Alarm', {
1818
metric: testMetric,
1919
threshold: 1000,
20-
evaluationPeriods: 2
20+
evaluationPeriods: 3,
2121
});
2222

2323
// THEN
2424
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
2525
ComparisonOperator: "GreaterThanOrEqualToThreshold",
26-
EvaluationPeriods: 2,
26+
EvaluationPeriods: 3,
27+
MetricName: "Metric",
28+
Namespace: "CDK/Test",
29+
Period: 300,
30+
Statistic: 'Average',
31+
Threshold: 1000,
32+
}));
33+
34+
test.done();
35+
},
36+
37+
'can set DatapointsToAlarm'(test: Test) {
38+
// GIVEN
39+
const stack = new Stack();
40+
41+
// WHEN
42+
new Alarm(stack, 'Alarm', {
43+
metric: testMetric,
44+
threshold: 1000,
45+
evaluationPeriods: 3,
46+
datapointsToAlarm: 2,
47+
});
48+
49+
// THEN
50+
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
51+
ComparisonOperator: "GreaterThanOrEqualToThreshold",
52+
EvaluationPeriods: 3,
53+
DatapointsToAlarm: 2,
2754
MetricName: "Metric",
2855
Namespace: "CDK/Test",
2956
Period: 300,

0 commit comments

Comments
 (0)