Skip to content

Commit

Permalink
feat(cloudwatch) create AnomalyDetectionAlarm L2 construct
Browse files Browse the repository at this point in the history
Allows customers to create Alarms with anomaly detection enabled.  Supports both direct metrics and math expressions.

Closes aws#10540
  • Loading branch information
Brandon Dahler committed Apr 18, 2022
1 parent 423d72f commit 2e5bb1e
Show file tree
Hide file tree
Showing 11 changed files with 1,663 additions and 26 deletions.
32 changes: 32 additions & 0 deletions packages/@aws-cdk/aws-cloudwatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,38 @@ different between them. This affects both the notifications sent out over SNS,
as well as the EventBridge events generated by this Alarm. If you are writing
code to consume these notifications, be sure to handle both formats.

### Anomaly Detection Alarms
[Anomaly Detection Alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Create_Anomaly_Detection_Alarm.html) can be created on metrics in one of two ways. Either create an `AnomalyDetectionAlarm`
object, passing the `Metric` object to set the alarm on:

```ts
declare const fn: lambda.Function;

new cloudwatch.AnomalyDetectionAlarm(this, 'AnomalyDetectionAlarm', {
metric: fn.metricErrors(),
threshold: 2,
evaluationPeriods: 2,
});
```

Alternatively, you can call `metric.createAnomalyDetectionAlarm()`:

```ts
declare const fn: lambda.Function;

fn.metricErrors().createAnomalyDetectionAlarm(this, 'AnomalyDetectionAlarm', {
threshold: 2,
evaluationPeriods: 2,
});
```

The most important properties to set while creating an AnomalyDetectionAlarm are:

- `threshold`: the number of standard deviations away from the expected value to compare the metric's value against.
- `comparisonOperator`: the comparison operation to use, defaults to enforcing both lower and upper bounds.
- `evaluationPeriods`: how many consecutive periods the metric has to be
breaching the the threshold for the alarm to trigger.

### Composite Alarms

[Composite Alarms](https://aws.amazon.com/about-aws/whats-new/2020/03/amazon-cloudwatch-now-allows-you-to-combine-multiple-alarms/)
Expand Down
31 changes: 6 additions & 25 deletions packages/@aws-cdk/aws-cloudwatch/lib/alarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,24 @@ export enum ComparisonOperator {
/**
* Specified statistic is lower than or greater than the anomaly model band.
* Used only for alarms based on anomaly detection models
*
* @deprecated Use AnomalyDetectionAlarm instead.
*/
LESS_THAN_LOWER_OR_GREATER_THAN_UPPER_THRESHOLD = 'LessThanLowerOrGreaterThanUpperThreshold',

/**
* Specified statistic is greater than the anomaly model band.
* Used only for alarms based on anomaly detection models
*
* @deprecated Use AnomalyDetectionAlarm instead.
*/
GREATER_THAN_UPPER_THRESHOLD = 'GreaterThanUpperThreshold',

/**
* Specified statistic is lower than the anomaly model band.
* Used only for alarms based on anomaly detection models
*
* @deprecated Use AnomalyDetectionAlarm instead.
*/
LESS_THAN_LOWER_THRESHOLD = 'LessThanLowerThreshold',
}
Expand All @@ -74,31 +80,6 @@ const OPERATOR_SYMBOLS: {[key: string]: string} = {
LessThanOrEqualToThreshold: '<=',
};

/**
* Specify how missing data points are treated during alarm evaluation
*/
export enum TreatMissingData {
/**
* Missing data points are treated as breaching the threshold
*/
BREACHING = 'breaching',

/**
* Missing data points are treated as being within the threshold
*/
NOT_BREACHING = 'notBreaching',

/**
* The current alarm state is maintained
*/
IGNORE = 'ignore',

/**
* The alarm does not consider missing data points when evaluating whether to change state
*/
MISSING = 'missing'
}

/**
* An alarm on a CloudWatch metric
*/
Expand Down
Loading

0 comments on commit 2e5bb1e

Please sign in to comment.