From 850f892bcc230d4dbfb59ab76ee502b634b51748 Mon Sep 17 00:00:00 2001 From: Sebastien Corbiere Date: Tue, 27 Aug 2024 16:35:52 -0700 Subject: [PATCH] Cleanup comments and documentation. --- .../test/integ.anomaly-detection-alarm.ts | 15 ----- packages/aws-cdk-lib/aws-cloudwatch/README.md | 57 ++++++------------- .../aws-cdk-lib/aws-cloudwatch/lib/alarm.ts | 2 +- 3 files changed, 19 insertions(+), 55 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.anomaly-detection-alarm.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.anomaly-detection-alarm.ts index c42598e89520d..fb08b3a804dab 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.anomaly-detection-alarm.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.anomaly-detection-alarm.ts @@ -27,18 +27,3 @@ const app = new App(); new IntegTest(app, 'AnomalyDetectionAlarmIntegTest', { testCases: [new AnomalyDetectionAlarmTestStack(app, 'AnomalyDetectionAlarmTestStack')], }); - -// app.synth(); - -/** - * Integration test for anomaly detection alarms - * - * SYNTHESIS VERIFICATION: - * 1. Check for the presence of ANOMALY_DETECTION_BAND in the template - * - The Metrics property should contain an item with Expression: "ANOMALY_DETECTION_BAND(m1, 2)" - * 2. Verify that ThresholdMetricId is set and Threshold is not present - * - The Alarm resource should have a ThresholdMetricId property set to "expr_1" - * - The Alarm resource should not have a Threshold property - * 3. Ensure both metrics have ReturnData set to true - * - Both items in the Metrics array should have "ReturnData": true - */ \ No newline at end of file diff --git a/packages/aws-cdk-lib/aws-cloudwatch/README.md b/packages/aws-cdk-lib/aws-cloudwatch/README.md index 3cc0e3f5272a2..4c198ed13ac9a 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch/README.md +++ b/packages/aws-cdk-lib/aws-cloudwatch/README.md @@ -1,44 +1,5 @@ - -## Anomaly Detection Alarms - -CloudWatch supports creating alarms based on anomaly detection. You can create an anomaly detection alarm using the `createAnomalyDetectionAlarm` method on a `Metric` object. Here's an example: - -```typescript -import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch'; - -const metric = new cloudwatch.Metric({ - namespace: 'AWS/EC2', - metricName: 'CPUUtilization', - dimensions: { InstanceId: 'i-1234567890abcdef0' }, -}); - -const anomalyAlarm = metric.createAnomalyDetectionAlarm(this, 'AnomalyAlarm', { - evaluationPeriods: 3, - comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_UPPER_THRESHOLD, -}); -``` - -This creates an alarm that triggers when the metric exceeds the upper threshold of the anomaly detection band for 3 consecutive evaluation periods. The anomaly detection band is automatically calculated by CloudWatch based on the metric's historical data. - -Note that anomaly detection alarms don't require a threshold value, as the threshold is dynamically determined by the anomaly detection algorithm. The `createAnomalyDetectionAlarm` method handles the necessary configuration, including setting the `thresholdMetricId` to the appropriate value. # Amazon CloudWatch Construct Library -## Anomaly Detection Alarms - -To create an alarm based on anomaly detection, you can use the `createAnomalyDetectionAlarm` method on a `Metric` object: - -```typescript -const queue = new sqs.Queue(this, 'Queue'); -const alarm = queue.metricNumberOfMessagesSent().createAnomalyDetectionAlarm(this, 'AnomalyAlarm', { - evaluationPeriods: 1, - comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_LOWER_OR_GREATER_THAN_UPPER_THRESHOLD, -}); -``` - -This method automatically sets up the necessary ANOMALY_DETECTION_BAND expression and configures the alarm correctly for anomaly detection. - -For more information on anomaly detection in CloudWatch, see the [AWS documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html). - ## Metric objects @@ -426,6 +387,24 @@ only supports filtering by `unit` for Alarms, not in Dashboard graphs. Please see the following GitHub issue for a discussion on real unit calculations in CDK: https://github.com/aws/aws-cdk/issues/5595 +## Anomaly Detection Alarms + +To create an alarm based on anomaly detection, you can use the `createAnomalyDetectionAlarm` method on a `Metric` or `MathExpression` object: + +```typescript +const queue = new sqs.Queue(this, 'Queue'); +const alarm = queue.metricNumberOfMessagesSent().createAnomalyDetectionAlarm(this, 'AnomalyAlarm', { + bounds: 2, + evaluationPeriods: 1, + comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_LOWER_OR_GREATER_THAN_UPPER_THRESHOLD, +}); +``` + +This method automatically sets up the necessary ANOMALY_DETECTION_BAND expression and configures the alarm correctly for anomaly detection. +Note that anomaly detection alarms don't require a threshold value, as the threshold is dynamically determined by the anomaly detection algorithm. The `createAnomalyDetectionAlarm` method handles the necessary configuration, including setting the `thresholdMetricId` to the appropriate value. +For more information on anomaly detection in CloudWatch, see the [AWS documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html). + + ## Dashboards Dashboards are set of Widgets stored server-side which can be accessed quickly diff --git a/packages/aws-cdk-lib/aws-cloudwatch/lib/alarm.ts b/packages/aws-cdk-lib/aws-cloudwatch/lib/alarm.ts index 45d88c68d2fa1..502efdc3b87e1 100644 --- a/packages/aws-cdk-lib/aws-cloudwatch/lib/alarm.ts +++ b/packages/aws-cdk-lib/aws-cloudwatch/lib/alarm.ts @@ -145,7 +145,7 @@ export class Alarm extends AlarmBase { } /** - * Determine whether this alarm uses an anomaly detection operator. + * Determine whether this operator is an anomaly detection operator. * * @param operator the comparison operator for the alarm. * @returns true if the operator is an anomaly detection operator, false otherwise.