Skip to content

Commit

Permalink
feat(cloudwatch): Support 'datapointsToAlarm' on Alarms
Browse files Browse the repository at this point in the history
Adds support for settingt the `dataPointsToAlarm` property of the
CloudWatch `Alarm`s (also via the `Metric`s).

Fixes #1626
  • Loading branch information
RomainMuller committed Jan 29, 2019
1 parent 6a8e010 commit 5b47ebe
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ export interface AlarmProps {
* @default true
*/
actionsEnabled?: boolean;

/**
* The number of datapoints that must be breaching to trigger the alarm. This is used only if you are setting an "M
* out of N" alarm. In that case, this value is the M. For more information, see Evaluating an Alarm in the Amazon
* CloudWatch User Guide.
*
* @default ``evaluationPeriods``
*/
datapointsToAlarm?: number;
}

/**
Expand Down Expand Up @@ -153,6 +162,7 @@ export class Alarm extends Construct {
// Evaluation
comparisonOperator,
threshold: props.threshold,
datapointsToAlarm: props.datapointsToAlarm,
evaluateLowSampleCountPercentile: props.evaluateLowSampleCountPercentile,
evaluationPeriods: props.evaluationPeriods,
treatMissingData: props.treatMissingData,
Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/lib/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export class Metric {
alarmName: props.alarmName,
alarmDescription: props.alarmDescription,
comparisonOperator: props.comparisonOperator,
datapointsToAlarm: props.datapointsToAlarm,
threshold: props.threshold,
evaluationPeriods: props.evaluationPeriods,
evaluateLowSampleCountPercentile: props.evaluateLowSampleCountPercentile,
Expand Down Expand Up @@ -372,6 +373,15 @@ export interface NewAlarmProps {
* @default true
*/
actionsEnabled?: boolean;

/**
* The number of datapoints that must be breaching to trigger the alarm. This is used only if you are setting an "M
* out of N" alarm. In that case, this value is the M. For more information, see Evaluating an Alarm in the Amazon
* CloudWatch User Guide.
*
* @default ``evaluationPeriods``
*/
datapointsToAlarm?: number;
}

function ifUndefined<T>(x: T | undefined, def: T | undefined): T | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
"Properties": {
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"EvaluationPeriods": 3,
"MetricName": "ApproximateNumberOfMessagesVisible",
"Namespace": "AWS/SQS",
"Period": 300,
"Threshold": 100,
"DatapointsToAlarm": 2,
"Dimensions": [
{
"Name": "QueueName",
Expand All @@ -23,6 +21,9 @@
}
}
],
"MetricName": "ApproximateNumberOfMessagesVisible",
"Namespace": "AWS/SQS",
"Period": 300,
"Statistic": "Average"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const metric = new cloudwatch.Metric({

const alarm = metric.newAlarm(stack, 'Alarm', {
threshold: 100,
evaluationPeriods: 3
evaluationPeriods: 3,
datapointsToAlarm: 2,
});

const dashboard = new cloudwatch.Dashboard(stack, 'Dash');
Expand Down
6 changes: 4 additions & 2 deletions packages/@aws-cdk/aws-cloudwatch/test/test.alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ export = {
new Alarm(stack, 'Alarm', {
metric: testMetric,
threshold: 1000,
evaluationPeriods: 2
evaluationPeriods: 3,
datapointsToAlarm: 2,
});

// THEN
expect(stack).to(haveResource('AWS::CloudWatch::Alarm', {
ComparisonOperator: "GreaterThanOrEqualToThreshold",
EvaluationPeriods: 2,
EvaluationPeriods: 3,
DatapointsToAlarm: 2,
MetricName: "Metric",
Namespace: "CDK/Test",
Period: 300,
Expand Down

0 comments on commit 5b47ebe

Please sign in to comment.