Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudwatch): support anomaly detection alarms #19956

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 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,39 @@ 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a reason to do this. Can you explain why you want to deprecate these when they're used more widely than just in this context?

*/
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this move makes sense. This is directly related to creating an alarm.

/**
* 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